static void Main(string[] args) { Pixiv_HotSearch hotSearch = JsonConvert.DeserializeObject <Pixiv_HotSearch>(File.ReadAllText(@"E:\test.txt")); var info = hotSearch.data.Where(x => x.tags.Any(y => y.name.Contains("114514"))) .OrderBy(x => Guid.NewGuid().ToString()).FirstOrDefault(); Console.ReadKey(); }
public static IllustInfo GetHotSearch(string keyword) { using (HttpWebClient http = new HttpWebClient() { TimeOut = 10000, Encoding = Encoding.UTF8, Proxy = MainSave.Proxy, AllowAutoRedirect = true, Referer = "https://pixivic.net/" }) { string url = $"https://pix.ipv4.host/illustrations?illustType=illust&searchType=original&maxSanityLevel=6&page={new Random().Next(1, 6)}&keyword={HttpTool.UrlEncode(keyword)}&pageSize=10"; string returnstr = string.Empty; try { string authCode = PublicVariables.PixivicAuth; if (string.IsNullOrEmpty(authCode)) { MainSave.CQLog.Info("未填写授权码", "搜图需要在数据目录的Config.ini文件内,Config字段的PixivicAuth值内填入获取到的授权码"); throw new Exception(); } http.Encoding = Encoding.UTF8; http.Headers.Add("authorization", authCode); returnstr = http.DownloadString(url); Pixiv_HotSearch hotSearch = JsonConvert.DeserializeObject <Pixiv_HotSearch>(returnstr); IllustInfo illustInfo = new IllustInfo(); Datum info; if (hotSearch.data.Count != 0) { if (PublicVariables.R18_Flag is false) { var result = hotSearch.data.Where(x => !x.tags.Any(y => y.name.Contains("R-18"))) .OrderBy(x => Guid.NewGuid().ToString()); info = result.FirstOrDefault(); if (info != null) { if (result.Count() != hotSearch.data.Count) { if (hotSearch.data.Count != 0) { MainSave.CQLog.Info("R18拦截", $"拦截了 {hotSearch.data.Count - result.Count()} 个搜索结果"); } } illustInfo = new IllustInfo() { IllustText = Pixiv_HotSearch.GetSearchText(info), IllustCQCode = Pixiv_HotSearch.GetSearchPic(info), IllustUrl = info.imageUrls[0].original.Replace("pximg.net", "pixiv.cat") }; } else { if (hotSearch.data.Count != 0) { MainSave.CQLog.Info("R18拦截", $"拦截了 {hotSearch.data.Count} 个搜索结果"); } illustInfo = new IllustInfo() { IllustText = "设置内限制级图片,不予显示", IllustCQCode = new CQCode(CQFunction.Image, new KeyValuePair <string, string>("file", "Error.jpg")) }; return(illustInfo); } } else { info = hotSearch.data.OrderBy(x => Guid.NewGuid().ToString()).First(); illustInfo = new IllustInfo() { IllustText = Pixiv_HotSearch.GetSearchText(info), IllustCQCode = Pixiv_HotSearch.GetSearchPic(info), IllustUrl = info.imageUrls[0].original.Replace("pximg.net", "pixiv.cat"), R18_Flag = info.tags.Any(x => x.name.Contains("R-18")) }; } } else { illustInfo = new IllustInfo() { IllustText = "搜索结果为空", IllustCQCode = CQApi.CQCode_Image("Error.jpg") }; } return(illustInfo); } catch (Exception e) { if (!Directory.Exists(MainSave.AppDirectory + "error\\" + "hotsearch\\")) { Directory.CreateDirectory(MainSave.AppDirectory + "error\\" + "hotsearch\\"); } IniConfig ini = new IniConfig(MainSave.AppDirectory + "error\\" + "hotsearch\\" + $"{DateTime.Now:yyyyMMddHHss}.log"); ini.Object["Error"]["Message"] = e.Message; ini.Object["Error"]["StackTrace"] = e.StackTrace; ini.Object["Error"]["Object"] = returnstr; ini.Save(); MainSave.CQLog.Info("搜索详情", $"解析失败,错误信息:{e.Message}"); IllustInfo illustInfo = new IllustInfo() { IllustText = "解析失败,无法获取热门搜索", IllustCQCode = CQApi.CQCode_Image("Error.jpg") }; return(illustInfo); } } }