// 搜索视频 public static Task <VideoSearchResult> SearchVideo(string key, int pageIndex = 1, int pageSize = 10) { var headers = new Dictionary <string, string> { { "Host", "so.yinyuetai.com" }, { "Referer", "http://m2.yinyuetai.com/so.html" }, { "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" }, { "Cookie", "tid=beu1ZFb61bolKqehSgbcU9EI; yyt_pref=2; yinyuetai_uid=ahba6T2u1boDoDHucReAoG3J; Hm_lvt_5885921252431e994a0a9617e39e9d73=1545751287; Hm_lpvt_5885921252431e994a0a9617e39e9d73=1545751287" }, { "Accept-Language", "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,ja;q=0.6" }, { "Accept-Encoding", "gzip, deflate" }, { "Accept", "*/*" } }; var parameters = new Dictionary <string, string> { { "callback", "jsonpHd7t3Z" }, { "_api", "get.videoList" }, { "_mock", "false" }, { "keyword", key }, { "offset", "0" }, { "pageIndex", pageIndex.ToString() }, { "pageSize", pageSize.ToString() } }; return(Task <VideoSearchResult> .Run(() => { var videoSearchResult = new VideoSearchResult(); try { var task = Util.Get("http://so.yinyuetai.com/search/video-search", headers, parameters); string result = task.Result; result = result.Substring(12, result.Length - 13); JObject resultJson = JObject.Parse(result); videoSearchResult = resultJson.ToObject <VideoSearchResult>(); if (videoSearchResult.PageInfo.TotalCount == 0) { throw new Exception("none"); } return videoSearchResult; } catch (NullReferenceException e) { throw e; } })); }
// 搜索视频 private void VideoSearch(int pageIndex, int pageSize = 10) { string key = tboxKeyword.Text; try { // 搜索视频 Cursor.Current = Cursors.WaitCursor; videoSearchResult = Action.SearchVideo(key, pageIndex, pageSize).Result; Cursor.Current = Cursors.Default; pageInfo = videoSearchResult.PageInfo; currentPageIndex = pageInfo.PageNum; string totalInfoText = "共 {0} 条搜索结果"; lblTotalInfo.Text = String.Format(totalInfoText, pageInfo.TotalCount); string pageInfoText = "第 {0} / {1} 页"; lblPageInfo.Text = String.Format(pageInfoText, currentPageIndex, pageInfo.PageCount); // 分页判断 if (currentPageIndex > 1) { btnPrevPage.Enabled = true; } else { btnPrevPage.Enabled = false; } if (pageInfo.PageCount > currentPageIndex) { btnNextPage.Enabled = true; } else { btnNextPage.Enabled = false; } // 清空列表 lboxVideoList.Items.Clear(); // 重新填充 foreach (var item in videoSearchResult.Videos.Data) { lboxVideoList.Items.Add(item.Title); videoInfos.Add(null); } // 默认选中第一个 lboxVideoList.SelectedIndex = 0; } catch (NullReferenceException) { MessageBox.Show("网络连接超时,请重试!"); } catch (Exception) { currentPageIndex = 0; string pageInfoText = "第 {0} / {1} 页"; lblPageInfo.Text = String.Format(pageInfoText, 0, 0); lblTotalInfo.Text = "未找到相关视频"; // 清空列表 lboxVideoList.Items.Clear(); panel2.Visible = true; MessageBox.Show("未找到相关视频,请更换关键词!"); } }