/// <summary> /// 构造函数 /// </summary> /// <param name="img">图片</param> /// <param name="index">缩略图位置索引</param> /// <param name="site">图片站点</param> public ImgControl(Img img, int index, ImageSite site) { InitializeComponent(); this.site = site; Image = img; this.index = index; shc.Add("Accept-Ranges", "bytes"); shc.Accept = null; shc.Referer = site.Referer; shc.Timeout = 8000; shc.ContentType = SessionHeadersValue.ContentTypeAuto; if (img.IsViewed) { //statusBorder.Background = new SolidColorBrush(Color.FromArgb(0xCC, 0xFE, 0xE2, 0xE2)); statusBorder.Background = new SolidColorBrush(Color.FromArgb(0xEE, 0xE9, 0x93, 0xAA)); } //try //{ //string s = .Substring(img.Score.IndexOf(' '), img.Score.Length - img.Score.IndexOf(' ')); //score.Text = img.Score.ToString(); //} //catch { } /* * if (!supportScore) * { * brdScr.Visibility = System.Windows.Visibility.Hidden; * }*/ //chk.Text = img.Dimension; //RenderOptions.SetBitmapScalingMode(preview, BitmapScalingMode.Fant); preview.DataContext = img; preview.SizeChanged += new SizeChangedEventHandler(preview_SizeChanged); preview.ImageFailed += new EventHandler <ExceptionRoutedEventArgs>(preview_ImageFailed); preview.MouseUp += new MouseButtonEventHandler(preview_MouseUp); statusBorder.MouseUp += new MouseButtonEventHandler(preview_MouseUp); chk.MouseUp += new MouseButtonEventHandler(preview_MouseUp); txtDesc.MouseUp += new MouseButtonEventHandler(preview_MouseUp); downBtn.MouseUp += new MouseButtonEventHandler(Border_MouseUp); magBtn.MouseUp += new MouseButtonEventHandler(preview_Click); //chk.Click += chk_Checked; //ToolTip tip = preview.ToolTip as ToolTip; //tip.PlacementTarget = preview.Parent as UIElement; //TextBlock desc = (tip.Content as Border).Child as TextBlock; //下载缩略图 //DownloadImg(); if (img.DownloadDetail != null) { //need detail LayoutRoot.IsEnabled = false; //isRetrievingDetail = true; chk.Text = "信息未加载"; } else { ShowImgDetail(); } }
/// <summary> /// 添加预览 /// </summary> /// <param name="img"></param> /// <param name="parentIndex"></param> /// <param name="needReferer"></param> public void AddPreview(Img img, int parentIndex, string needReferer) { if (!imgs.ContainsKey(img.Id)) { imgs.Add(img.Id, index++); oriIndex.Add(img.Id, parentIndex); descs.Add(img.Id, img); //添加预览图分页按钮 ToggleButton btn = new ToggleButton { Content = img.Id, Margin = new Thickness(3, 1, 3, 1) }; btn.Checked += new RoutedEventHandler(btn_Click); btns.Children.Add(btn); //初始化预览图 PreviewImg prei = new PreviewImg(this, img); prei.MouseLeftButtonUp += new MouseButtonEventHandler(delegate(object s1, MouseButtonEventArgs ea) { preMX = 0; preMY = 0; }); prei.MouseLeftButtonDown += new MouseButtonEventHandler(delegate(object s1, MouseButtonEventArgs ea) { preMX = 0; preMY = 0; }); prei.MouseDown += new MouseButtonEventHandler(delegate(object s1, MouseButtonEventArgs ea) { //中键缩放 if (ea.MiddleButton == MouseButtonState.Pressed) { Button_Click_2(null, null); } }); prei.MouseMove += new MouseEventHandler(delegate(object s1, MouseEventArgs ea) { //拖动 if (ea.LeftButton == MouseButtonState.Pressed) { if (preMY != 0 && preMX != 0) { int offX = (int)(ea.GetPosition(LayoutRoot).X) - preMX; int offY = (int)(ea.GetPosition(LayoutRoot).Y) - preMY; ScrollViewer sc = (imgGrid.Children[imgs[selectedId]] as ScrollViewer); sc.ScrollToHorizontalOffset(sc.HorizontalOffset - offX); sc.ScrollToVerticalOffset(sc.VerticalOffset - offY); } preMX = (int)(ea.GetPosition(LayoutRoot).X); preMY = (int)(ea.GetPosition(LayoutRoot).Y); } }); //加入预览图控件 imgGrid.Children.Add(new ScrollViewer() { Content = prei, Visibility = Visibility.Hidden, HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled, VerticalScrollBarVisibility = ScrollBarVisibility.Disabled }); //开始下载图片 prei.DownloadImg(needReferer); if (selectedId == 0) { (btns.Children[btns.Children.Count - 1] as ToggleButton).IsChecked = true; } ChangePreBtnText(); } }
/// <summary> /// JSON format /// </summary> /// <param name="url"></param> /// <param name="pageString"></param> /// <param name="imgs"></param> /// <param name="sub">站点标记</param> private void ProcessJSON(string url, string pageString, List <Img> imgs, string sub) { object[] array = (new JavaScriptSerializer()).DeserializeObject(pageString) as object[]; foreach (object o in array) { Dictionary <string, object> obj = o as Dictionary <string, object>; string id = "", tags = "", host = "", score = "N/A", source = "", sample = "", jpeg_url = "", file_url = "", created_at = "N/A", preview_url = "", author = ""; int width = 0, height = 0, file_size = 0; bool skin_parm; //域名 host = url.Substring(0, url.IndexOf('/', 8)); //图片ID if (obj["id"] != null) { id = obj["id"].ToString(); } //投稿者 if (obj.ContainsKey("author") && obj["author"] != null) { author = obj["author"].ToString(); } //图片来源 if (obj.ContainsKey("source") && obj["source"] != null) { source = obj["source"].ToString(); } //原图宽高 width height try { width = int.Parse(obj["width"].ToString().Trim()); height = int.Parse(obj["height"].ToString().Trim()); } catch { } //文件大小 try { if (obj.ContainsKey("file_size") && obj["file_size"] != null) { file_size = int.Parse(obj["file_size"].ToString()); } } catch { } //上传时间 if (obj.ContainsKey("created_at") && obj["created_at"] != null) { if (sub == "sku") { Dictionary <string, object> objs = (Dictionary <string, object>)obj["created_at"]; if (objs.ContainsKey("s")) { created_at = objs["s"].ToString(); } } else { created_at = obj["created_at"].ToString(); } } //评级和评分 if (obj.ContainsKey("rating") && obj["rating"] != null) { score = "Safe "; if (obj["rating"].ToString() == "e") { score = "Explicit "; } else { score = "Questionable "; } if (obj.ContainsKey("score")) { score += obj["score"].ToString(); } else if (obj.ContainsKey("total_score")) { score += obj["total_score"].ToString(); } } //缩略图 if (obj.ContainsKey("sample_url") && obj["sample_url"] != null) { sample = obj["sample_url"].ToString(); } //预览图 if (obj.ContainsKey("preview_url") && obj["preview_url"] != null) { preview_url = obj["preview_url"].ToString(); } //原图 if (obj.ContainsKey("file_url") && obj["file_url"] != null) { file_url = obj["file_url"].ToString(); jpeg_url = file_url; } //JPG if (obj.ContainsKey("jpeg_url") && obj["jpeg_url"] != null) { jpeg_url = obj["jpeg_url"].ToString(); } //Formatted skin_parm = sub.Contains("sku"); sample = FormattedImgUrl(host, sample, skin_parm); preview_url = FormattedImgUrl(host, preview_url, skin_parm); file_url = FormattedImgUrl(host, file_url, skin_parm); jpeg_url = FormattedImgUrl(host, jpeg_url, skin_parm); //标签 if (obj.ContainsKey("tags") && obj["tags"] != null) { if (sub == "sku") { object ov = obj["tags"]; StringBuilder ovsb = new StringBuilder(); if (ov.GetType().FullName.Contains("Object[]")) { (new JavaScriptSerializer()).Serialize(ov, ovsb); string ovsbstr = ovsb.ToString(); object[] ovarr = (new JavaScriptSerializer()).DeserializeObject(ovsbstr) as object[]; for (int i = 0; i < ovarr.Count(); i++) { obj = ovarr[i] as Dictionary <string, object>; if (obj.ContainsKey("name")) { tags += i < ovarr.Count() - 1 ? obj["name"] + " " : obj["name"]; } } } } else { tags = obj["tags"].ToString(); } } bool noVerify = sub.Length == 2 && sub.Contains("nv"); Img img = GenerateImg(url, id, author, source, width, height, file_size, created_at, score, sample, preview_url, file_url, jpeg_url, tags, noVerify); if (img != null) { imgs.Add(img); } } }
/// <summary> /// 生成 Img 对象 /// </summary> /// <param name="url"></param> /// <param name="id"></param> /// <param name="author"></param> /// <param name="src"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="file_size"></param> /// <param name="created_at"></param> /// <param name="score"></param> /// <param name="sample"></param> /// <param name="preview_url"></param> /// <param name="file_url"></param> /// <param name="jpeg_url"></param> /// <param name="tags"></param> /// <returns></returns> private Img GenerateImg(string url, string id, string author, string src, int width, int height, int file_size, string created_at, string score, string sample, string preview_url, string file_url, string jpeg_url, string tags, bool noVerify) { int scoreInt = 0, intId = 0; try { intId = int.Parse(id); } catch { } try { scoreInt = int.Parse(score.Substring(score.IndexOf(' '), score.Length - score.IndexOf(' '))); } catch { } #region DateTime Convert //eg. Fri Aug 28 20:05:57 -0600 2009 or 1291280246 try { //1291280246 == 2010/12/2 16:57 long sec = long.Parse(created_at); DateTime startDate = new DateTime(1970, 1, 1, 8, 0, 0, 0); created_at = startDate.AddSeconds(sec).ToString(); } catch { //Thu Dec 31 06:54:54 +0000 2009 //2012/01/28 01:59:10 -0500 //1323123123 //Dec Nov Oct Sep Aug Jul Jun May Apr Mar Feb Jan try { created_at = DateTime.Parse(created_at).ToString(); } catch { } } #endregion string host = url.Substring(0, url.IndexOf('/', 8)); string detailUrl = host + "/post/show/" + id; if (url.Contains("index.php")) { detailUrl = host + "/index.php?page=post&s=view&id=" + id; } Img img = new Img() { Date = created_at, Desc = tags, FileSize = file_size > 1048576 ? (file_size / 1048576.0).ToString("0.00MB") : (file_size / 1024.0).ToString("0.00KB"), Height = height, Id = intId, Author = author == "" ? "UnkwnAuthor" : author, IsExplicit = score.StartsWith("E"), JpegUrl = jpeg_url, OriginalUrl = file_url, PreviewUrl = preview_url, SampleUrl = sample, Score = scoreInt, Source = src, Tags = tags, Width = width, DetailUrl = detailUrl, NoVerify = noVerify }; return(img); }
/// <summary> /// XML 格式信息 /// </summary> /// <param name="url"></param> /// <param name="pageString"></param> /// <param name="imgs"></param> /// <param name="sub">标记 (nv 不验证完整性)</param> private void ProcessXML(string url, string pageString, List <Img> imgs, string sub) { XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.LoadXml(pageString); } catch { xmlDoc.LoadXml(HttpUtility.HtmlDecode(pageString)); } XmlElement root = null; if (xmlDoc.SelectSingleNode("posts") == null) { root = (XmlElement)(xmlDoc.SelectSingleNode("IbSearch/response")); //root } else { root = (XmlElement)(xmlDoc.SelectSingleNode("posts")); //root } foreach (XmlNode postN in root.ChildNodes) { XmlElement post = (XmlElement)postN; int file_size = 0; try { if (post.HasAttribute("file_size")) { file_size = int.Parse(post.GetAttribute("file_size")); } } catch { } string created_at = "N/A"; if (post.HasAttribute("created_at")) { created_at = post.GetAttribute("created_at"); } string preview_url = post.GetAttribute("preview_url"); string file_url = post.GetAttribute("file_url"); string jpeg_url = file_url; if (post.HasAttribute("jpeg_url")) { jpeg_url = post.GetAttribute("jpeg_url"); } string sample = file_url; if (post.HasAttribute("sample_url")) { sample = post.GetAttribute("sample_url"); } string tags = post.GetAttribute("tags"); string id = post.GetAttribute("id"); string author = post.GetAttribute("author"); string source = post.GetAttribute("source"); //string width = post.GetAttribute("width"); //string height = post.GetAttribute("height"); int width = 0; int height = 0; try { width = int.Parse(post.GetAttribute("width").Trim()); height = int.Parse(post.GetAttribute("height").Trim()); } catch { } string score = "N/A"; if (post.HasAttribute("rating")) { score = "Safe "; if (post.GetAttribute("rating") == "e") { score = "Explicit "; } else { score = "Questionable "; } if (post.HasAttribute("score")) { score += post.GetAttribute("score"); } } string host = url.Substring(0, url.IndexOf('/', 8)); preview_url = FormattedImgUrl(host, preview_url); file_url = FormattedImgUrl(host, file_url); sample = FormattedImgUrl(host, sample); jpeg_url = FormattedImgUrl(host, jpeg_url); //if (!UseJpeg) //jpeg_url = file_url; bool noVerify = sub.Length == 2 && sub.Contains("nv"); Img img = GenerateImg(url, id, author, source, width, height, file_size, created_at, score, sample, preview_url, file_url, jpeg_url, tags, noVerify); if (img != null) { imgs.Add(img); } } }
/// <summary> /// HTML 格式信息 /// </summary> /// <param name="url"></param> /// <param name="pageString"></param> /// <param name="imgs"></param> /// <param name="sub">标记 (nv 不验证完整性)</param> private void ProcessHTML(string url, string pageString, List <Img> imgs, string sub) { /* Post.register({"jpeg_height":1200,"sample_width":1333,"md5":"1550bb8d9fa4e1ee7903ee103459f69a","created_at":{"n":666146000,"json_class":"Time","s":1290715184}, * "status":"active","jpeg_file_size":215756,"sample_height":1000,"score":4,"sample_url":"http://yuinyan.imouto.org/sample/1550bb8d9fa4e459f69a/moe%20163698%20sample.jpg", * "actual_preview_height":225,"author":"paku-paku","has_children":false,"change":758975,"height":1200,"sample_file_size":142868, * "preview_url":"http://mio3.imouto.org/data/preview/15/50/1550bb8d9fa4e1ee7903ee103459f69a.jpg","tags":"akiyama_mio bikini k-on! swimsuits transparent_png vector_trace", * "source":"","width":1600,"rating":"s","jpeg_url":"http://yuinyan.imouto.org/jpeg/1550bb8d9fa4e1ee7903ee103459f69a/moe%20163698%20msuitst_png%20vector_trace.jpg", * "preview_width":150,"file_size":113055,"jpeg_width":1600,"preview_height":113,"is_shown_in_index":true, * "file_url":"http://yuinyan.imouto.org/image/1550bb8d9fa4e1ee7903ee103459f69a/moe%20163698%20amio%20swimsctor_trace.png", * "id":163698,"parent_id":null,"actual_preview_width":300,"creator_id":70875}) */ //当前字符串位置 int index = 0; while (index < pageString.Length) { index = pageString.IndexOf("Post.register({", index); if (index == -1) { break; } string item = pageString.Substring(index + 14, pageString.IndexOf("})", index) - index - 13); #region Analyze json //替换有可能干扰分析的 [ ] " //item = item.Replace('[', '1').Replace(']', '1').Replace("\\\"", ""); //JSONObject obj = JSONConvert.DeserializeObject(item); Dictionary <string, object> obj = (new JavaScriptSerializer()).DeserializeObject(item) as Dictionary <string, object>; string sample = ""; if (obj.ContainsKey("sample_url")) { sample = obj["sample_url"].ToString(); } int file_size = 0; try { if (obj.ContainsKey("file_size")) { file_size = int.Parse(obj["file_size"].ToString()); } } catch { } string created_at = "N/A"; if (obj.ContainsKey("created_at")) { created_at = obj["created_at"].ToString(); } string preview_url = obj["preview_url"].ToString(); string file_url = obj["file_url"].ToString(); string jpeg_url = file_url; if (obj.ContainsKey("jpeg_url")) { jpeg_url = obj["jpeg_url"].ToString(); } string tags = obj["tags"].ToString(); string id = obj["id"].ToString(); string author = obj["author"].ToString(); string source = obj["source"].ToString(); //string width = obj["width"].ToString(); //string height = obj["height"].ToString(); int width = 0; int height = 0; try { width = int.Parse(obj["width"].ToString().Trim()); height = int.Parse(obj["height"].ToString().Trim()); } catch { } string score = "N/A"; if (obj.ContainsKey("rating")) { score = "Safe "; if (obj["rating"].ToString() == "e") { score = "Explicit "; } else { score = "Questionable "; } if (obj.ContainsKey("score")) { score += obj["score"].ToString(); } } string host = url.Substring(0, url.IndexOf('/', 8)); preview_url = FormattedImgUrl(host, preview_url); file_url = FormattedImgUrl(host, file_url); sample = FormattedImgUrl(host, sample); jpeg_url = FormattedImgUrl(host, jpeg_url); //if (!UseJpeg) //jpeg_url = file_url; bool noVerify = sub.Length == 2 && sub.Contains("nv"); Img img = GenerateImg(url, id, author, source, width, height, file_size, created_at, score, sample, preview_url, file_url, jpeg_url, tags, noVerify); if (img != null) { imgs.Add(img); } #endregion index += 15; } }
/// <summary> /// JSON format /// </summary> /// <param name="siteUrl"></param> /// <param name="url"></param> /// <param name="pageString"></param> /// <param name="imgs"></param> /// <param name="sub">站点标记</param> private void ProcessJSON(string siteUrl, string url, string pageString, List <Img> imgs, string sub) { if (string.IsNullOrWhiteSpace(pageString)) { return; } object[] array = (new JavaScriptSerializer()).DeserializeObject(pageString) as object[]; foreach (object o in array) { Dictionary <string, object> obj = o as Dictionary <string, object>; string id = "", tags = "", host = "", score = "N/A", source = "", sample = "", jpeg_url = "", file_url = "", created_at = "N/A", preview_url = "", author = ""; int width = 0, height = 0, file_size = 0; bool skin_parm; //域名 host = url.Substring(0, url.IndexOf('/', 8)); //图片ID if (obj["id"] != null) { id = obj["id"].ToString(); } //投稿者 if (obj.ContainsKey("author") && obj["author"] != null) { author = obj["author"].ToString(); } else if (obj.ContainsKey("uploader_name") && obj["uploader_name"] != null) { author = obj["uploader_name"].ToString(); } //图片来源 if (obj.ContainsKey("source") && obj["source"] != null) { source = obj["source"].ToString(); } //原图宽高 width height try { if (obj.ContainsKey("width") && obj["width"] != null) { width = int.Parse(obj["width"].ToString().Trim()); height = int.Parse(obj["height"].ToString().Trim()); } else if (obj.ContainsKey("image_width") && obj["image_width"] != null) { width = int.Parse(obj["image_width"].ToString().Trim()); height = int.Parse(obj["image_height"].ToString().Trim()); } } catch { } //文件大小 try { if (obj.ContainsKey("file_size") && obj["file_size"] != null) { file_size = int.Parse(obj["file_size"].ToString()); } } catch { } //上传时间 if (obj.ContainsKey("created_at") && obj["created_at"] != null) { if (sub == "sku") { Dictionary <string, object> objs = (Dictionary <string, object>)obj["created_at"]; if (objs.ContainsKey("s")) { created_at = objs["s"].ToString(); } } else { created_at = obj["created_at"].ToString(); } } //评级和评分 if (obj.ContainsKey("rating") && obj["rating"] != null) { score = "Safe "; if (obj["rating"].ToString() == "e") { score = "Explicit "; } else { score = "Questionable "; } if (obj.ContainsKey("score")) { score += obj["score"].ToString(); } else if (obj.ContainsKey("total_score")) { score += obj["total_score"].ToString(); } } //缩略图 if (obj.ContainsKey("preview_url") && obj["preview_url"] != null) { sample = obj["preview_url"].ToString(); } else if (obj.ContainsKey("preview_file_url") && obj["preview_file_url"] != null) { sample = obj["preview_file_url"].ToString(); } //预览图 if (obj.ContainsKey("sample_url") && obj["sample_url"] != null) { preview_url = obj["sample_url"].ToString(); } else if (obj.ContainsKey("large_file_url") && obj["large_file_url"] != null) { preview_url = obj["large_file_url"].ToString(); } //原图 if (obj.ContainsKey("file_url") && obj["file_url"] != null) { file_url = obj["file_url"].ToString(); } //JPG jpeg_url = preview_url.Length > 0 ? preview_url : file_url; if (obj.ContainsKey("jpeg_url") && obj["jpeg_url"] != null) { jpeg_url = obj["jpeg_url"].ToString(); } //Formatted skin_parm = sub.Contains("sku"); sample = FormattedImgUrl(host, sample, skin_parm); preview_url = FormattedImgUrl(host, preview_url, skin_parm); file_url = FormattedImgUrl(host, file_url, skin_parm); jpeg_url = FormattedImgUrl(host, jpeg_url, skin_parm); //标签 if (obj.ContainsKey("tags") && obj["tags"] != null) { if (sub == "sku") { #region sankaku tags rule try { JavaScriptSerializer jss = new JavaScriptSerializer(); string stags = jss.Serialize(obj["tags"]); JArray jary = JArray.Parse(stags); if (jary.Count > 0) { JArray ResultTags = new JArray(); JArray TagGroup = new JArray(); string TagTypeGroup = "3,2,4,1,8,0,9"; string[] TTGarr = TagTypeGroup.Split(','); foreach (string tts in TTGarr) { foreach (JToken tjt in jary) { if (((JObject)tjt)["type"].ToString().Equals(tts, StringComparison.CurrentCultureIgnoreCase)) { TagGroup.Add(tjt); } } if (tts.Equals("0")) { ResultTags.Add(TagGroup.OrderBy(jo => (string)jo["name"])); } else { ResultTags.Add(TagGroup.OrderByDescending(jo => Convert.ToInt32((string)jo["count"]))); } TagGroup.Clear(); } int i = 0, RTcount = ResultTags.Count(); foreach (JToken tjt in ResultTags) { i++; tags += i < RTcount ? ((JObject)tjt)["name"].ToString() + " " : ((JObject)tjt)["name"].ToString(); } } } catch { object ov = obj["tags"]; StringBuilder ovsb = new StringBuilder(); if (ov.GetType().FullName.Contains("Object[]")) { (new JavaScriptSerializer()).Serialize(ov, ovsb); string ovsbstr = ovsb.ToString(); object[] ovarr = (new JavaScriptSerializer()).DeserializeObject(ovsbstr) as object[]; for (int i = 0; i < ovarr.Count(); i++) { obj = ovarr[i] as Dictionary <string, object>; if (obj.ContainsKey("name")) { tags += i < ovarr.Count() - 1 ? obj["name"] + " " : obj["name"]; } } } } #endregion } else { tags = obj["tags"].ToString(); } } else if (obj.ContainsKey("tag_string") && obj["tag_string"] != null) { tags = obj["tag_string"].ToString(); } //恢复konachan删除的图片信息 if (sub.Contains("konachan") & !obj.ContainsKey("file_url")) { tags = "deleted " + tags; string md5 = string.Empty; if (obj.ContainsKey("md5")) { md5 = obj["md5"].ToString(); } sample = $"{siteUrl}/data/preview/{md5.Substring(0, 2)}/{md5.Substring(2, 2)}/{md5}.jpg"; preview_url = $"{siteUrl}/sample/{md5}/{sub} - {id} sample.jpg"; file_url = $"{siteUrl}/image/{md5}/{sub} - {id} image.#ext"; //下载时会自动判断格式 jpeg_url = $"{siteUrl}/jpeg/{md5}/{sub} - {id} jpeg.jpg"; } bool noVerify = sub.Length == 2 && sub.Contains("nv"); Img img = GenerateImg(siteUrl, url, id, author, source, width, height, file_size, created_at, score, sample, preview_url, file_url, jpeg_url, tags, noVerify); if (img != null) { imgs.Add(img); } } }
/// <summary> /// XML 格式信息 /// </summary> /// <param name="siteUrl"></param> /// <param name="url"></param> /// <param name="pageString"></param> /// <param name="imgs"></param> /// <param name="sub">标记 (nv 不验证完整性)</param> private void ProcessXML(string siteUrl, string url, string pageString, List <Img> imgs, string sub, string shortName) { if (string.IsNullOrWhiteSpace(pageString)) { return; } XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.LoadXml(pageString); } catch { xmlDoc.LoadXml(HttpUtility.HtmlDecode(pageString)); } XmlElement root = null; if (xmlDoc.SelectSingleNode("posts") == null) { root = (XmlElement)(xmlDoc.SelectSingleNode("IbSearch/response")); //root } else { root = (XmlElement)(xmlDoc.SelectSingleNode("posts")); //root } foreach (XmlNode postN in root.ChildNodes) { XmlElement post = (XmlElement)postN; int file_size = 0; try { if (post.HasAttribute("file_size")) { file_size = int.Parse(post.GetAttribute("file_size")); } } catch { } string created_at = "N/A"; if (post.HasAttribute("created_at")) { created_at = post.GetAttribute("created_at"); } string preview_url = post.GetAttribute("sample_url"); //预览图 string file_url = post.GetAttribute("file_url"); string jpeg_url = preview_url.Length > 0 ? preview_url : file_url; if (post.HasAttribute("jpeg_url")) { jpeg_url = post.GetAttribute("jpeg_url"); } string sample = file_url; if (post.HasAttribute("preview_url")) { sample = post.GetAttribute("preview_url"); //缩略图 } string tags = post.GetAttribute("tags"); string id = post.GetAttribute("id"); string author = post.GetAttribute("author"); string source = post.GetAttribute("source"); //string width = post.GetAttribute("width"); //string height = post.GetAttribute("height"); int width = 0; int height = 0; try { width = int.Parse(post.GetAttribute("width").Trim()); height = int.Parse(post.GetAttribute("height").Trim()); } catch { } string score = "N/A"; if (post.HasAttribute("rating")) { score = "Safe "; if (post.GetAttribute("rating") == "e") { score = "Explicit "; } else { score = "Questionable "; } if (post.HasAttribute("score")) { score += post.GetAttribute("score"); } } string host = url.Substring(0, url.IndexOf('/', 8)); //恢复yande删除的图片信息 if (sub.Contains("yande") & !post.HasAttribute("file_url")) { tags = "deleted " + tags; string md5 = string.Empty; if (post.HasAttribute("md5")) { md5 = post.GetAttribute("md5"); } sample = $"{siteUrl}/data/preview/{md5.Substring(0, 2)}/{md5.Substring(2, 2)}/{md5}.jpg"; preview_url = $"{siteUrl}/sample/{md5}/{shortName} {id} sample.jpg"; string file_ext = post.HasAttribute("file_ext") ? post.GetAttribute("file_ext") : "#ext"; file_url = $"{siteUrl}/image/{md5}/{shortName} {id} image.{file_ext}"; jpeg_url = $"{siteUrl}/jpeg/{md5}/{shortName} {id} jpeg.jpg"; } preview_url = FormattedImgUrl(host, preview_url); file_url = FormattedImgUrl(host, file_url); sample = FormattedImgUrl(host, sample); jpeg_url = FormattedImgUrl(host, jpeg_url); //if (!UseJpeg) //jpeg_url = file_url; bool noVerify = sub.Length > 1 && sub.Contains("nv"); Img img = GenerateImg(siteUrl, url, id, author, source, width, height, file_size, created_at, score, sample, preview_url, file_url, jpeg_url, tags, noVerify); if (img != null) { imgs.Add(img); } } }
/// <summary> /// JSON format /// </summary> /// <param name="siteUrl"></param> /// <param name="url"></param> /// <param name="pageString"></param> /// <param name="imgs"></param> /// <param name="sub">站点标记</param> private void ProcessJSON(string siteUrl, string url, string pageString, List <Img> imgs, string sub) { if (string.IsNullOrWhiteSpace(pageString)) { return; } JArray jArray = JArray.Parse(pageString); foreach (JToken obj in jArray) { string id = "", tags = "", host = "", score = "N/A", source = "", sample = "", jpeg_url = "", file_url = "", created_at = "N/A", preview_url = "", author = ""; int width = 0, height = 0, file_size = 0; bool skin_parm; //域名 host = url.Substring(0, url.IndexOf('/', 8)); //图片ID if (obj["id"] != null) { id = obj["id"].ToString(); } //投稿者 if (obj["author"] != null) { if (sub.Contains("csku")) { JToken jt = JToken.Parse(obj["author"].ToString()); if (jt["name"] != null) { author = jt["name"].ToString(); } } else { author = obj["author"].ToString(); } } else if (obj["uploader_name"] != null) { author = obj["uploader_name"].ToString(); } else if (obj["owner"] != null && sub.Contains("gelbooru")) { author = obj["owner"].ToString(); } //图片来源 if (obj["source"] != null) { source = obj["source"].ToString(); } //原图宽高 width height try { if (obj["width"] != null) { width = int.Parse(obj["width"].ToString().Trim()); height = int.Parse(obj["height"].ToString().Trim()); } else if (obj["image_width"] != null) { width = int.Parse(obj["image_width"].ToString().Trim()); height = int.Parse(obj["image_height"].ToString().Trim()); } } catch { } //文件大小 try { if (obj["file_size"] != null) { file_size = int.Parse(obj["file_size"].ToString()); } } catch { } //上传时间 if (obj["created_at"] != null) { if (sub.Contains("sku")) { JToken objs = JObject.Parse(obj["created_at"].ToString()); if (!string.IsNullOrWhiteSpace(objs["s"].ToString())) { created_at = objs["s"].ToString(); } } else { created_at = obj["created_at"].ToString(); } } //评级和评分 if (obj["rating"] != null) { score = "Safe "; if (obj["rating"].ToString() == "e") { score = "Explicit "; } else { score = "Questionable "; } if (obj["score"] != null) { score += obj["score"].ToString(); } else if (obj["total_score"] != null) { score += obj["total_score"].ToString(); } } //缩略图,小图 if (obj["preview_url"] != null) { sample = obj["preview_url"].ToString(); } else if (obj["preview_file_url"] != null) { sample = obj["preview_file_url"].ToString(); } //预览图,中图 if (obj["sample_url"] != null) { preview_url = obj["sample_url"].ToString(); } else if (obj["large_file_url"] != null) { preview_url = obj["large_file_url"].ToString(); } //原图,大图 if (obj["file_url"] != null) { file_url = obj["file_url"].ToString(); } // gelbooru 图片信息处理 string hash = string.Empty; string directory = string.Empty; if (sub.Contains("gelbooru")) { if (obj["hash"] != null) { hash = obj["hash"].ToString(); // e.g. "hash": "17584fdf56612edc08592c47f5ea8343", } if (obj["directory"] != null) { directory = obj["directory"].ToString().Replace("\\", ""); // e.g. "directory": "17\/58", } if (file_url != "") { file_url = file_url.Replace("\\", ""); } sample = $"{siteUrl}/thumbnails/{directory}/thumbnail_{hash}.jpg"; preview_url = $"{file_url.Substring(0, file_url.IndexOf(".com") + 4)}/samples/{directory}/sample_{hash}.jpg"; } //JPG jpeg_url = preview_url.Length > 0 ? preview_url : file_url; if (obj["jpeg_url"] != null) { jpeg_url = obj["jpeg_url"].ToString(); } //Formatted skin_parm = sub.Contains("sku"); sample = FormattedImgUrl(host, sample, skin_parm); preview_url = FormattedImgUrl(host, preview_url, skin_parm); file_url = FormattedImgUrl(host, file_url, skin_parm); jpeg_url = FormattedImgUrl(host, jpeg_url, skin_parm); //标签 if (obj["tags"] != null) { if (sub.Contains("sku")) { #region sankaku tags rule try { JArray jary = (JArray)obj["tags"]; if (jary.Count > 0) { JArray ResultTags = new JArray(); JArray TagGroup = new JArray(); string TagTypeGroup = "3,2,4,1,8,0,9"; string[] TTGarr = TagTypeGroup.Split(','); foreach (string tts in TTGarr) { foreach (JToken tjt in jary) { if (((JObject)tjt)["type"].ToString().Equals(tts, StringComparison.CurrentCultureIgnoreCase)) { TagGroup.Add(tjt); } } if (tts.Equals("0")) { ResultTags.Add(TagGroup.OrderBy(jo => (string)jo["name"])); } else { ResultTags.Add(TagGroup.OrderByDescending(jo => Convert.ToInt32((string)jo["count"]))); } TagGroup.Clear(); } int i = 0, RTcount = ResultTags.Count(); foreach (JToken tjt in ResultTags) { i++; tags += i < RTcount ? ((JObject)tjt)["name"].ToString() + " " : ((JObject)tjt)["name"].ToString(); } } } catch {} #endregion } else { tags = obj["tags"].ToString(); } } else if (obj["tag_string"] != null) { tags = obj["tag_string"].ToString(); } //恢复konachan删除的图片信息 if (sub.Contains("konachan") && obj["file_url"] == null) { tags = "deleted " + tags; string md5 = string.Empty; if (obj["md5"] != null) { md5 = obj["md5"].ToString(); } sample = $"{siteUrl}/data/preview/{md5.Substring(0, 2)}/{md5.Substring(2, 2)}/{md5}.jpg"; preview_url = $"{siteUrl}/sample/{md5}/{sub} - {id} sample.jpg"; file_url = $"{siteUrl}/image/{md5}/{sub} - {id} image.#ext"; //下载时会自动判断格式 jpeg_url = $"{siteUrl}/jpeg/{md5}/{sub} - {id} jpeg.jpg"; } bool noVerify = sub.Length == 2 && sub.Contains("nv"); Img img = GenerateImg(siteUrl, url, id, author, source, width, height, file_size, created_at, score, sample, preview_url, file_url, jpeg_url, tags, noVerify); if (img != null) { imgs.Add(img); } } }