女朋友经常问影视剧, 答不上来怎么办?朋友介绍了一个好站(www.5ddy.net), 全是豆瓣高分影视。 非常666. 今日特写一个查询小工具, 根据名称快速得到它的剧集。
设计思路
1. 获取它接口的JSON数据。
2. 解析JSON, 获取电影名和链接。
3. 发给女朋友。
核心代码。
1 /// <summary> 2 /// 获取HTML源文件 3 /// </summary> 4 /// <param name="url"></param> 5 /// <returns></returns> 6 public static string HttpClientGetHtmls(string url) 7 { 8 try 9 { 10 if (url == null || url.Length == 0) 11 { 12 return null; 13 } 14 string result = null; 15 HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url); 16 wbRequest.Method = "GET"; 17 wbRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3"; 18 wbRequest.UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36";//模拟user 19 wbRequest.Timeout = 3000;//设计超时时间 20 try 21 { 22 HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse(); 23 using (Stream responseStream = wbResponse.GetResponseStream()) 24 { 25 using (StreamReader sReader = new StreamReader(responseStream)) 26 { 27 result = sReader.ReadToEnd(); 28 } 29 } 30 } 31 catch (Exception e) 32 { 33 Console.WriteLine(e.StackTrace); 34 } 35 36 return result; 37 } 38 catch (Exception exception) 39 { 40 } 41 return ""; 42 }
解析JSON格式。
1 public string searchvod(string msg) 2 { 3 StringBuilder result = new StringBuilder(); 4 msg = msg.Replace("搜", ""); 5 msg = Regex.Replace(msg, "[ \[ \] \^ \-_*×――(^)(^)$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;"‘’“”-]", ""); 6 string html = HttpClientGetHtmls("http://www.5ddy.net/api.php/provide/vod/?ac=list&wd=" + msg); 7 JObject model = JObject.Parse(html); //解析json 8 if (model != null) 9 foreach (var item in model["list"]) 10 { 11 var id = item["vod_id"]; 12 var dyname = item["vod_name"]; 13 string url = "https://www.5ddy.net/voddetail/" + id + ".html"; 14 var s = SinaShortUrl(url); 15 result.Append(dyname + ":" + s + " "); 16 } 17 return result.ToString(); 18 }
只要传电影或电视剧名称, 就能快速找到它的链接了。 配合自己写的桌面工具, 爽得不要不要的。