Exemplo n.º 1
0
        public override List<TagItem> GetTags(string word, IWebProxy proxy)
        {
            List<TagItem> re = new List<TagItem>();

            if (srcType == PixivSrcType.Tag || srcType == PixivSrcType.TagFull)
            {

                Login(proxy);
                Dictionary<string, object> tags = new Dictionary<string, object>();
                Dictionary<string, object> tag = new Dictionary<string, object>();

                string url = string.Format(SiteUrl + "/rpc/cps.php?keyword={0}", word);

                shc.Referer = referer;
                shc.ContentType = SessionHeadersValue.AcceptAppJson;
                shc.Add("X-Requested-With", "XMLHttpRequest");
                shc.Remove("Accept-Ranges");
                string json = Sweb.Get(url, proxy, "UTF-8", shc);

                object[] array = (new JavaScriptSerializer()).DeserializeObject(json) as object[];

                tags = (new JavaScriptSerializer()).DeserializeObject(json) as Dictionary<string, object>;
                if (tags.ContainsKey("candidates"))
                {
                    foreach (object obj in tags["candidates"] as object[])
                    {
                        tag = obj as Dictionary<string, object>;
                        re.Add(new TagItem() { Name = tag["tag_name"].ToString() });
                    }
                }
            }
            return re;
        }
Exemplo n.º 2
0
        /// <summary>
        /// 下载图片
        /// </summary>
        /// <param name="reTry">重试</param>
        public void DownloadImg(string needReferer, bool reTry)
        {
            try
            {
                #region 创建请求数据
                shc.Add("Accept-Ranges", "bytes");
                shc.Referer                = needReferer;
                shc.ContentType            = SessionHeadersValue.ContentTypeAuto;
                shc.AcceptEncoding         = SessionHeadersValue.AcceptEncodingGzip;
                shc.AutomaticDecompression = DecompressionMethods.GZip;
                HttpWebRequest req = Sweb.CreateWebRequest(reTry ? img.OriginalUrl : img.SampleUrl, MainWindow.WebProxy, shc);

                //将请求加入请求组
                reqs.Add(img.Id, req);
                #endregion

                //异步下载开始
                req.BeginGetResponse(new AsyncCallback(RespCallback), new KeyValuePair <int, HttpWebRequest>(img.Id, req));
            }
            catch (Exception ex)
            {
                Program.Log(ex, "Download sample failed");
                StopLoadImg(img.Id, true, "创建下载失败");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 下载图片
        /// </summary>
        /// <param name="reTry">重试:1~9</param>
        public void DownloadImg(string needReferer, int reTry = 0)
        {
            try
            {
                #region 创建请求数据
                reTry = (reTry < 0 ? 0 : reTry) > 9 ? 9 : reTry;
                shc.Add("Accept-Ranges", "bytes");
                shc.Referer                = needReferer;
                shc.ContentType            = SessionHeadersValue.ContentTypeAuto;
                shc.AcceptEncoding         = SessionHeadersValue.AcceptEncodingGzip;
                shc.AutomaticDecompression = DecompressionMethods.GZip;
                string[] requrls =
                {
                    img.PreviewUrl,
                    img.JpegUrl,
                    img.OriginalUrl.Replace(".#ext",             ".jpg"),
                    img.OriginalUrl.Replace(".#ext",             ".png"),
                    img.OriginalUrl.Replace(".#ext",             ".gif"),
                    WebUrlEncode(img.PreviewUrl),
                    WebUrlEncode(img.JpegUrl),
                    WebUrlEncode(img.OriginalUrl.Replace(".#ext",".jpg")),
                    WebUrlEncode(img.OriginalUrl.Replace(".#ext",".png")),
                    WebUrlEncode(img.OriginalUrl.Replace(".#ext",".gif"))
                };

                string         requrl = requrls[reTry];
                HttpWebRequest req    = Sweb.CreateWebRequest(requrl, MainWindow.WebProxy, shc);

                //将请求加入请求组
                Reqs.Add(img.Id, req);
                #endregion

                //异步下载开始
                req.BeginGetResponse(new AsyncCallback(RespCallback), new KeyValuePair <int, HttpWebRequest>(img.Id, req));
            }
            catch (Exception ex)
            {
                Program.Log(ex, "Download sample failed");
                StopLoadImg(img.Id, true, "创建下载失败");
            }
        }
Exemplo n.º 4
0
        private Img GenerateImg(string detailUrl, string sample_url, string id)
        {
            shc.Add("Accept-Ranges", "bytes");
            shc.ContentType = SessionHeadersValue.ContentTypeAuto;

            int intId = int.Parse(id);

            if (!detailUrl.StartsWith("http") && !detailUrl.StartsWith("/"))
            {
                detailUrl = "/" + detailUrl;
            }

            //convert relative url to absolute
            if (detailUrl.StartsWith("/"))
            {
                detailUrl = SiteUrl + detailUrl;
            }
            if (sample_url.StartsWith("/"))
            {
                sample_url = SiteUrl + sample_url;
            }

            referer = detailUrl;
            //string fileUrl = preview_url.Replace("_s.", ".");
            //string sampleUrl = preview_url.Replace("_s.", "_m.");

            //http://i1.pixiv.net/img-inf/img/2013/04/10/00/11/37/34912478_s.png
            //http://i1.pixiv.net/img03/img/tukumo/34912478_m.png
            //http://i1.pixiv.net/img03/img/tukumo/34912478.png

            Img img = new Img()
            {
                //Date = "N/A",
                //FileSize = file_size.ToUpper(),
                //Desc = intId + " ",
                Id = intId,
                //JpegUrl = fileUrl,
                //OriginalUrl = fileUrl,
                //PreviewUrl = preview_url,
                SampleUrl = sample_url,
                //Score = 0,
                //Width = width,
                //Height = height,
                //Tags = tags,
                DetailUrl = detailUrl
            };

            img.DownloadDetail = new DetailHandler((i, p) =>
            {
                int pageCount = 1;
                string page, dimension, Pcount;
                page = dimension = string.Empty;
                //retrieve details
                page = Sweb.Get(i.DetailUrl, p, shc);

                Regex reg        = new Regex(@"】「(?<Desc>.*?)」.*?/(?<Author>.*?)\s\[pixi");
                HtmlDocument doc = new HtmlDocument();
                HtmlDocument ds  = new HtmlDocument();
                doc.LoadHtml(page);
                Pcount = Regex.Match(i.SampleUrl, @"(?<=_p)\d+(?=_)").Value;

                //=================================================
                //[R-XX] 【XX】「Desc」插画/Author [pixiv]
                //标题中取名字和作者
                try
                {
                    MatchCollection mc = reg.Matches(doc.DocumentNode.SelectSingleNode("//title").InnerText);
                    if (srcType == PixivSrcType.Pid)
                    {
                        i.Desc = mc[0].Groups["Desc"].Value + "P" + Pcount;
                    }
                    else
                    {
                        i.Desc = mc[0].Groups["Desc"].Value;
                    }
                    i.Author = mc[0].Groups["Author"].Value;
                }
                catch { }
                //------------------------
                if (!page.Contains("globalInitData"))
                {
                    //++++旧版详情页+++++
                    //04/16/2012 17:44|600×800|SAI  or 04/16/2012 17:44|600×800 or 04/19/2012 22:57|漫画 6P|SAI
                    i.Date = doc.DocumentNode.SelectSingleNode("//ul[@class='meta']/li[1]").InnerText;
                    //总点数
                    i.Score = int.Parse(doc.DocumentNode.SelectSingleNode("//dd[@class='rated-count']").InnerText);

                    //URLS
                    //http://i2.pixiv.net/c/600x600/img-master/img/2014/10/08/06/13/30/46422743_p0_master1200.jpg
                    //http://i2.pixiv.net/img-original/img/2014/10/08/06/13/30/46422743_p0.png
                    Regex rx     = new Regex(@"/\d+x\d+/");
                    i.PreviewUrl = rx.Replace(i.SampleUrl, "/1200x1200/");
                    i.JpegUrl    = i.PreviewUrl;
                    try
                    {
                        i.OriginalUrl = doc.DocumentNode.SelectSingleNode("//*[@id='wrapper']/div[2]/div").SelectSingleNode(".//img").Attributes["data-src"].Value;
                    }
                    catch { }
                    i.OriginalUrl = string.IsNullOrWhiteSpace(i.OriginalUrl) ? i.JpegUrl : i.OriginalUrl;

                    //600×800 or 漫画 6P
                    dimension = doc.DocumentNode.SelectSingleNode("//ul[@class='meta']/li[2]").InnerText;
                    try
                    {
                        //706×1000
                        i.Width  = int.Parse(dimension.Substring(0, dimension.IndexOf('×')));
                        i.Height = int.Parse(Regex.Match(dimension.Substring(dimension.IndexOf('×') + 1), @"\d+").Value);
                    }
                    catch { }
                }
                else
                {
                    //+++++新版详情页+++++
                    Match strRex = Regex.Match(page, @"(?<=(?:" + i.Id + ":.)).*?(?=(?:.},user))");

                    JObject jobj = JObject.Parse(strRex.Value);

                    i.Date = jobj["uploadDate"].ToSafeString();

                    try
                    {
                        i.Score   = int.Parse(jobj["likeCount"].ToSafeString());
                        i.Width   = int.Parse(jobj["width"].ToSafeString());
                        i.Height  = int.Parse(jobj["height"].ToSafeString());
                        pageCount = int.Parse(jobj["pageCount"].ToSafeString());
                    }
                    catch { }

                    jobj          = JObject.Parse(jobj["urls"].ToSafeString());
                    Regex rex     = new Regex(@"(?<=.*)p\d+(?=[^/]*[^\._]*$)");
                    i.PreviewUrl  = rex.Replace(jobj["regular"].ToSafeString(), "p" + Pcount);
                    i.JpegUrl     = rex.Replace(jobj["small"].ToSafeString(), "p" + Pcount);
                    i.OriginalUrl = rex.Replace(jobj["original"].ToSafeString(), "p" + Pcount);
                }
                //----------------------------

                try
                {
                    if (pageCount > 1 || i.Width == 0 && i.Height == 0)
                    {
                        //i.OriginalUrl = i.SampleUrl.Replace("600x600", "1200x1200");
                        //i.JpegUrl = i.OriginalUrl;
                        //manga list
                        //漫画 6P
                        string oriul   = "";
                        int mangaCount = pageCount;
                        if (pageCount > 1)
                        {
                            mangaCount = pageCount;
                        }
                        else
                        {
                            int index        = dimension.IndexOf(' ') + 1;
                            string mangaPart = dimension.Substring(index, dimension.IndexOf('P') - index);
                            mangaCount       = int.Parse(mangaPart);
                        }
                        if (srcType == PixivSrcType.Pid)
                        {
                            try
                            {
                                page = Sweb.Get(i.DetailUrl.Replace("medium", "manga_big") + "&page=" + Pcount, p, shc);
                                ds.LoadHtml(page);
                                i.OriginalUrl = ds.DocumentNode.SelectSingleNode("/html/body/img").Attributes["src"].Value;
                            }
                            catch { }
                        }
                        else
                        {
                            i.Dimension = "Manga " + mangaCount + "P";
                            for (int j = 0; j < mangaCount; j++)
                            {
                                //保存漫画时优先下载原图 找不到原图则下jpg
                                try
                                {
                                    page = Sweb.Get(i.DetailUrl.Replace("medium", "manga_big") + "&page=" + j, p, shc);
                                    ds.LoadHtml(page);
                                    oriul = ds.DocumentNode.SelectSingleNode("/html/body/img").Attributes["src"].Value;
                                    img.OrignalUrlList.Add(oriul);
                                    if (j == 0)
                                    {
                                        img.OriginalUrl = oriul;
                                    }
                                }
                                catch
                                {
                                    //oriUrl = "http://img" + imgsvr + ".pixiv.net/img/" + items[6].Split('/')[4] + "/" + id + "_p0." + ext;
                                    img.OrignalUrlList.Add(i.OriginalUrl.Replace("_p0", "_p" + j));
                                }
                            }
                        }
                    }
                    else if (i.OriginalUrl.Contains("ugoira"))//动图 ugoira
                    {
                        //以上面的漫画解析为蓝本修改而来
                        if (!i.OriginalUrl.Contains("_ugoira0."))
                        {
                            //为预防Pixiv在未来修改动图页面机制,若连接格式有变则直接抛出异常。
                            throw new ArgumentException();
                        }

                        try
                        {
                            i.PixivUgoira      = true;//标记动图类型
                            int mangaCount     = pageCount;
                            string ugoira_meta = Sweb.Get("https://www.pixiv.net/ajax/illust/" + i.Id + "/ugoira_meta", p, shc);

                            if (!string.IsNullOrWhiteSpace(ugoira_meta))
                            {
                                ugoira_meta = (Convert.ToString(((JObject)JObject.Parse(ugoira_meta)["body"])["frames"]));

                                //直接统计“{”的个数即可知道动图帧数
                                mangaCount = ugoira_meta.Count(c => c == '{');

                                i.Dimension = "Ugoira " + mangaCount + "P";
                                for (int j = 0; j < mangaCount; j++)
                                {
                                    //Generate urls for each frame
                                    i.OrignalUrlList.Add(i.OriginalUrl.Replace("_ugoira0.", "_ugoira" + j.ToString() + "."));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            });

            return(img);
        }
Exemplo n.º 5
0
        public override List <Img> GetImages(string pageString, IWebProxy proxy)
        {
            shc.Add("Accept-Ranges", "bytes");
            shc.ContentType = SessionHeadersValue.ContentTypeAuto;
            List <Img> list = new List <Img>();

            HtmlDocument dococument = new HtmlDocument();

            dococument.LoadHtml(pageString);
            HtmlNodeCollection imageItems = dococument.DocumentNode.SelectNodes("//*[@class='image-list cl']");

            if (imageItems == null)
            {
                return(list);
            }
            foreach (HtmlNode imageItem in imageItems)
            {
                HtmlNode imgNode = imageItem.SelectSingleNode("./div[1]/img");
                string   tags    = imgNode.Attributes["alt"].Value;
                Img      item    = new Img()
                {
                    Height     = Convert.ToInt32(imageItem.SelectSingleNode(".//div[@class='image']").Attributes["data-height"].Value),
                    Width      = Convert.ToInt32(imageItem.SelectSingleNode(".//div[@class='image']").Attributes["data-width"].Value),
                    Author     = imageItem.SelectSingleNode("//small/a").InnerText,
                    IsExplicit = false,
                    Tags       = tags,
                    Desc       = tags,
                    SampleUrl  = imgNode.Attributes["data-original"].Value.Replace("!single", "!320px"),
                    //JpegUrl = SiteUrl + imgNode.Attributes["data-viewersss"].Value,
                    Id        = StringToInt(imgNode.Attributes["id"].Value),
                    DetailUrl = SiteUrl + imgNode.Attributes["data-href"].Value,
                    Score     = Convert.ToInt32(imageItem.SelectSingleNode(".//span[@class='num']").InnerText)
                };

                item.DownloadDetail = (i, p) =>
                {
                    string html = Sweb.Get(i.DetailUrl, proxy, shc);

                    HtmlDocument doc = new HtmlDocument();
                    doc.LoadHtml(html);
                    HtmlNode showIndexs  = doc.DocumentNode.SelectSingleNode("//div[@class='logo']");
                    HtmlNode imgDownNode = showIndexs.SelectSingleNode("//div[@class='img-control']");
                    string   nodeHtml    = showIndexs.OuterHtml;
                    i.Date = TimeConvert(nodeHtml);

                    if (nodeHtml.Contains("pixiv page"))
                    {
                        i.Source = showIndexs.SelectSingleNode(".//a[@target='_blank']").Attributes["href"].Value;
                    }
                    else
                    {
                        i.Source = Regex.Match(nodeHtml, @"(?<=源地址).*?(?=</p>)").Value.Trim();
                    }
                    i.PreviewUrl = doc.DocumentNode.SelectSingleNode("//figure[@class=\'show-image\']/img").Attributes["src"].Value;
                    if (Regex.Matches(imgDownNode.OuterHtml, "href").Count > 1)
                    {
                        i.OriginalUrl = SiteUrl + imgDownNode.SelectSingleNode("./a[1]").Attributes["href"].Value;
                        i.FileSize    = Regex.Match(imgDownNode.SelectSingleNode("./a[1]").InnerText, @"(?<=().*?(?=))").Value;
                    }
                    else
                    {
                        i.OriginalUrl = SiteUrl + imgDownNode.SelectSingleNode("./a").Attributes["href"].Value;
                        i.FileSize    = Regex.Match(imgDownNode.SelectSingleNode("./a").InnerText, @"(?<=().*?(?=))").Value;
                    }
                    i.JpegUrl = i.PreviewUrl.Length > 0 ? i.PreviewUrl : i.OriginalUrl;
                };
                list.Add(item);
            }

            return(list);
        }