Esempio n. 1
0
        public override List <Img> GetImages(string pageString, IWebProxy proxy)
        {
            List <Img> imgs = new List <Img>();

            HtmlDocument doc = new HtmlDocument();

            doc.LoadHtml(pageString);

            //retrieve all elements via xpath
            HtmlNodeCollection nodes   = null;
            HtmlNode           tagNode = null;

            try
            {
                if (srcType == PixivSrcType.Tag || srcType == PixivSrcType.TagFull)
                {
                    tagNode = doc.DocumentNode.SelectSingleNode("//input[@id='js-mount-point-search-result-list']");
                    //nodes = doc.DocumentNode.SelectSingleNode("//div[@id='wrapper']/div[2]/div[1]/section[1]/ul").SelectNodes("li");
                    if (tagNode == null)
                    {
                        nodes = doc.DocumentNode.SelectSingleNode("//div[@id='wrapper']/div[1]/div/ul").SelectNodes("li");
                    }
                }
                else if (srcType == PixivSrcType.Author)
                {
                    //181013遗弃的方案
                    //nodes = doc.DocumentNode.SelectSingleNode("//ul[@class='_image-items']").SelectNodes("li");

                    //ROOT ->body -> illusts
                    //ROOT ->body -> manga
                    //获取图片id
                    List <string> illustsList = new List <string>();
                    List <string> mangaList   = new List <string>();
                    if (!string.IsNullOrWhiteSpace(pageString))
                    {
                        JObject jsonObj = JObject.Parse(pageString);
                        JToken  jToken;
                        if (!string.IsNullOrWhiteSpace(jsonObj["body"].ToString()))
                        {
                            jToken = ((JObject)jsonObj["body"])["illusts"];
                            foreach (JProperty jp in jToken)
                            {
                                illustsList.Add(jp.Name);
                            }
                            jToken = ((JObject)jsonObj["body"])["manga"];
                            foreach (JProperty jp in jToken)
                            {
                                mangaList.Add(jp.Name);
                            }
                        }
                    }
                    int ilistcount = illustsList.Count,
                        mlistcount = mangaList.Count,
                        ill_num = 0, mng_num = 0,
                        scount        = ilistcount + mlistcount;
                    List <string> ids = new List <string>();
                    for (int j = 0; j < page * count && scount > 0 && j < scount; j++)
                    {
                        if (j < (page - 1) * count)
                        {
                            if (ill_num < ilistcount && mng_num < mlistcount)
                            {
                                if (int.Parse(illustsList[ill_num]) > int.Parse(mangaList[mng_num]))
                                {
                                    ill_num++;
                                }
                                else
                                {
                                    mng_num++;
                                }
                            }
                            else if (ill_num < ilistcount)
                            {
                                ill_num++;
                            }
                            else if (mng_num < mlistcount)
                            {
                                mng_num++;
                            }
                        }

                        else
                        {
                            if ((j - (page - 1) * count) % 48 == 0)
                            {
                                ids.Add(string.Empty);
                            }
                            if (ill_num < ilistcount && mng_num < mlistcount)
                            {
                                if (int.Parse(illustsList[ill_num]) > int.Parse(mangaList[mng_num]))
                                {
                                    ids[(j - (page - 1) * count) / 48] += $"ids[]={illustsList[ill_num++]}&";
                                }
                                else
                                {
                                    ids[(j - (page - 1) * count) / 48] += $"ids[]={mangaList[mng_num++]}&";
                                }
                            }
                            else if (ill_num < ilistcount)
                            {
                                ids[(j - (page - 1) * count) / 48] += $"ids[]={illustsList[ill_num++]}&";
                            }

                            else if (mng_num < mlistcount)
                            {
                                ids[(j - (page - 1) * count) / 48] += $"ids[]={mangaList[mng_num++]}&";
                            }
                        }
                    }
                    if (!ids.Exists(string.IsNullOrWhiteSpace))
                    {
                        List <string> tempPageString = new List <string>();
                        for (int i = 0; i < ids.Count; i++)
                        {
                            tempPageString.Add(Sweb.Get($"{SiteUrl}/ajax/user/{keyWord}/profile/illusts?{ids[i]}is_manga_top=0", proxy, shc));
                        }
                        if (!tempPageString.Exists(string.IsNullOrWhiteSpace))
                        {
                            //ROOT->body->works
                            //获取图片详细信息
                            foreach (string tempString in tempPageString)
                            {
                                if (!string.IsNullOrWhiteSpace(tempString))
                                {
                                    JObject jsonObj = JObject.Parse(tempString);
                                    JToken  jToken;
                                    if (!string.IsNullOrWhiteSpace(jsonObj["body"].ToString()))
                                    {
                                        jToken = ((JObject)jsonObj["body"])["works"];
                                        foreach (JProperty jp in jToken)
                                        {
                                            JToken nextJToken = (((JObject)jsonObj["body"])["works"])[jp.Name];
                                            Img    img        = GenerateImg(SiteUrl + "/member_illust.php?mode=medium&illust_id=" + jp.Name, (string)nextJToken["url"], (string)nextJToken["id"]);
                                            if (img != null)
                                            {
                                                imgs.Add(img);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    return(imgs);
                }
                //else if (srcType == PixivSrcType.Day || srcType == PixivSrcType.Month || srcType == PixivSrcType.Week) //ranking
                //nodes = doc.DocumentNode.SelectSingleNode("//section[@class='ranking-items autopagerize_page_element']").SelectNodes("div");
                else if (srcType == PixivSrcType.PidPlus)
                {
                    //相关作品json信息
                    string relatePicJson = tempPage;
                    string imagesJson    = string.Empty;
                    //ROOT ->body -> recommendMethods
                    List <string> rmsList = new List <string>();//recommendMethods 数据
                    if (!string.IsNullOrWhiteSpace(relatePicJson))
                    {
                        JObject jsonObj = JObject.Parse(relatePicJson);
                        JToken  jToken;
                        if (!string.IsNullOrWhiteSpace(jsonObj["body"].ToString()))
                        {
                            imagesJson = jsonObj["body"].ToString();
                            jToken     = ((JObject)jsonObj["body"])["recommendMethods"];
                            foreach (JProperty jp in jToken)
                            {
                                rmsList.Add(jp.Name);
                            }
                        }
                    }
                    string ids = string.Empty;
                    ids = (page == 1 ? "ids[]=" + keyWord + "&" : ids);

                    for (int j = (page - 1) * count; j < page * count & rmsList.Count > 0 & j <= rmsList.Count; j++)
                    {
                        ids += "ids[]=" + rmsList[j] + "&";
                    }
                    if (!string.IsNullOrWhiteSpace(ids))
                    {
                        pageString = Sweb.Get($"{SiteUrl}/ajax/user/{keyWord}/profile/illusts?{ids}is_manga_top=0", proxy, shc);
                        if (!string.IsNullOrWhiteSpace(pageString))
                        {
                            JObject jsonObj = JObject.Parse(pageString);
                            JToken  jToken;
                            if (!string.IsNullOrWhiteSpace(jsonObj["body"].ToString()))
                            {
                                jToken = ((JObject)jsonObj["body"])["works"];
                                foreach (JProperty jp in jToken)
                                {
                                    JToken nextJToken = (((JObject)jsonObj["body"])["works"])[jp.Name];
                                    Img    img        = GenerateImg($"{SiteUrl}/member_illust.php?mode=medium&illust_id={ jp.Name}", (string)nextJToken["url"], (string)nextJToken["id"]);
                                    if (keyWord == jp.Name)
                                    {
                                        img.Source = "相关作品";
                                    }
                                    if (img != null)
                                    {
                                        imgs.Add(img);
                                    }
                                }
                            }
                        }
                    }
                    return(imgs);
                }
                else if (srcType == PixivSrcType.Pid)
                {
                    if (!(Regex.Match(pageString, @"<h2.*?/h2>").Value.Contains("错误")))
                    {
                        int    mangaCount = 1;
                        string id, SampleUrl;
                        id = SampleUrl = string.Empty;
                        if (!pageString.Contains("globalInitData"))
                        {
                            //----- 旧版 -----
                            SampleUrl = doc.DocumentNode.SelectSingleNode("/html/head/meta[@property='og:image']").Attributes["content"].Value;
                            id        = SampleUrl.Substring(SampleUrl.LastIndexOf("/") + 1, SampleUrl.IndexOf("_") - SampleUrl.LastIndexOf("/") - 1);

                            string dimension = doc.DocumentNode.SelectSingleNode("//ul[@class='meta']/li[2]").InnerText;
                            if (dimension.EndsWith("P"))
                            {
                                mangaCount = int.Parse(Regex.Match(dimension, @"\d+").Value);
                            }
                        }
                        else
                        {
                            //----- 新版 -----
                            Match strRex = Regex.Match(pageString, @"(?<=(?:,illust\:.{.))\d+(?=(?:\:.))");
                            id = strRex.Value;

                            strRex = Regex.Match(pageString, @"(?<=(?:" + id + ":.)).*?(?=(?:.},user))");

                            JObject jobj = JObject.Parse(strRex.Value);
                            try { mangaCount = int.Parse(jobj["pageCount"].ToSafeString()); } catch { }

                            jobj      = JObject.Parse(jobj["urls"].ToSafeString());
                            SampleUrl = jobj["thumb"].ToSafeString();
                        }
                        string detailUrl = SiteUrl + "/member_illust.php?mode=medium&illust_id=" + id;
                        for (int j = 0; j < mangaCount; j++)
                        {
                            Img img = GenerateImg(detailUrl, SampleUrl.Replace("_p0_", "_p" + j.ToString() + "_"), id);
                            //if (i != 0) img.Source = "相关作品";
                            StringBuilder sb = new StringBuilder();
                            sb.Append("P");
                            sb.Append(j.ToString());
                            img.Dimension = sb.ToString();
                            if (img != null)
                            {
                                imgs.Add(img);
                            }
                        }
                        return(imgs);
                    }
                    else
                    {
                        throw new Exception("该作品已被删除,或作品ID不存在");
                    }
                }
                else
                {
                    //ranking
                    nodes = doc.DocumentNode.SelectNodes("//section[@class='ranking-item']");
                }
            }
            catch (Exception ex)
            {
                SiteManager.echoErrLog(SiteName, ex);
                throw new Exception("没有找到图片哦~ .=ω=");
            }

            if (nodes == null && tagNode == null)
            {
                return(imgs);
            }

            if (nodes != null && nodes.Count > 0)
            {
                foreach (HtmlNode imgNode in nodes)
                {
                    try
                    {
                        HtmlNode anode = imgNode.SelectSingleNode("a");
                        if (srcType == PixivSrcType.Day || srcType == PixivSrcType.Month || srcType == PixivSrcType.Week)
                        {
                            anode = imgNode.SelectSingleNode(".//div[@class='ranking-image-item']").SelectSingleNode("a");
                        }
                        //details will be extracted from here
                        //eg. member_illust.php?mode=medium&illust_id=29561307&ref=rn-b-5-thumbnail
                        //sampleUrl 正则 @"https://i\.pximg\..+?(?=")"
                        string detailUrl = anode.Attributes["href"].Value.Replace("amp;", string.Empty);
                        string sampleUrl = string.Empty;
                        sampleUrl = anode.SelectSingleNode(".//img").Attributes["src"].Value;

                        if (sampleUrl.ToLower().Contains("images/common"))
                        {
                            sampleUrl = anode.SelectSingleNode(".//img").Attributes["data-src"].Value;
                        }

                        if (sampleUrl.Contains('?'))
                        {
                            sampleUrl = sampleUrl.Substring(0, sampleUrl.IndexOf('?'));
                        }

                        //extract id from detail url
                        //string id = detailUrl.Substring(detailUrl.LastIndexOf('=') + 1);
                        string id = Regex.Match(detailUrl, @"illust_id=\d+").Value;
                        id = id.Substring(id.IndexOf('=') + 1);

                        Img img = GenerateImg(detailUrl, sampleUrl, id);
                        if (img != null)
                        {
                            imgs.Add(img);
                        }
                    }
                    catch
                    {
                        //int i = 0;
                    }
                }
            }
            else if (srcType == PixivSrcType.Tag || srcType == PixivSrcType.TagFull)
            {//Tag search js-mount-point-search-related-tags Json
                string   jsonData = tagNode.Attributes["data-items"].Value.Replace("&quot;", "\"");
                object[] array    = (new JavaScriptSerializer()).DeserializeObject(jsonData) as object[];
                foreach (object o in array)
                {
                    Dictionary <string, object> obj = o as Dictionary <string, object>;
                    string
                        detailUrl = "",
                        SampleUrl = "",
                        id        = "";
                    if (obj["illustId"] != null)
                    {
                        id        = obj["illustId"].ToString();
                        detailUrl = SiteUrl + "/member_illust.php?mode=medium&illust_id=" + id;
                    }
                    if (obj["url"] != null)
                    {
                        SampleUrl = obj["url"].ToString();
                    }
                    Img img = GenerateImg(detailUrl, SampleUrl, id);
                    if (img != null)
                    {
                        imgs.Add(img);
                    }
                }
            }

            return(imgs);
        }
Esempio n. 2
0
        public override List <Img> GetImages(string pageString, IWebProxy proxy)
        {
            List <Img> imgs = new List <Img>();

            HtmlDocument doc = new HtmlDocument();

            doc.LoadHtml(pageString);

            //retrieve all elements via xpath
            HtmlNodeCollection nodes   = null;
            HtmlNode           tagNode = null;

            try
            {
                if (srcType == PixivSrcType.Tag || srcType == PixivSrcType.TagFull)
                {
                    tagNode = doc.DocumentNode.SelectSingleNode("//input[@id='js-mount-point-search-result-list']");
                    //nodes = doc.DocumentNode.SelectSingleNode("//div[@id='wrapper']/div[2]/div[1]/section[1]/ul").SelectNodes("li");
                }
                else if (srcType == PixivSrcType.Author)
                {
                    nodes = doc.DocumentNode.SelectSingleNode("//ul[@class='_image-items']").SelectNodes("li");
                }
                //else if (srcType == PixivSrcType.Day || srcType == PixivSrcType.Month || srcType == PixivSrcType.Week) //ranking
                //nodes = doc.DocumentNode.SelectSingleNode("//section[@class='ranking-items autopagerize_page_element']").SelectNodes("div");
                else if (srcType == PixivSrcType.Pid)
                {
                    if (!(Regex.Match(pageString, @"<h2.*?/h2>").Value.Contains("错误")))
                    {
                        tempPage = pageString;
                        int    mangaCount = 1;
                        string id, SampleUrl;
                        id = SampleUrl = string.Empty;

                        if (!pageString.Contains("globalInitData"))
                        {
                            //----- 旧版 -----
                            SampleUrl = doc.DocumentNode.SelectSingleNode("/html/head/meta[@property='og:image']").Attributes["content"].Value;
                            id        = SampleUrl.Substring(SampleUrl.LastIndexOf("/") + 1, SampleUrl.IndexOf("_") - SampleUrl.LastIndexOf("/") - 1);

                            string dimension = doc.DocumentNode.SelectSingleNode("//ul[@class='meta']/li[2]").InnerText;
                            if (dimension.EndsWith("P"))
                            {
                                mangaCount = int.Parse(Regex.Match(dimension, @"\d+").Value);
                            }
                        }
                        else
                        {
                            //----- 新版 -----
                            Match strRex = Regex.Match(pageString, @"(?<=(?:,illust\:.{.))\d+(?=(?:\:.))");
                            id = strRex.Value;

                            strRex = Regex.Match(pageString, @"(?<=(?:" + id + ":.)).*?(?=(?:.},user))");

                            JObject jobj = JObject.Parse(strRex.Value);
                            try { mangaCount = int.Parse(jobj["pageCount"].ToSafeString()); } catch { }

                            jobj      = JObject.Parse(jobj["urls"].ToSafeString());
                            SampleUrl = jobj["thumb"].ToSafeString();
                        }

                        string detailUrl = SiteUrl + "/member_illust.php?mode=medium&illust_id=" + id;
                        for (int i = 0; i < mangaCount; i++)
                        {
                            Img           img = GenerateImg(detailUrl, SampleUrl.Replace("_p0_", "_p" + i.ToString() + "_"), id);
                            StringBuilder sb  = new StringBuilder();
                            sb.Append("P");
                            sb.Append(i.ToString());
                            img.Dimension = sb.ToString();
                            if (img != null)
                            {
                                imgs.Add(img);
                            }
                        }
                        return(imgs);
                    }
                    else
                    {
                        throw new Exception("该作品已被删除,或作品ID不存在");
                    }
                }
                else
                {
                    //ranking
                    nodes = doc.DocumentNode.SelectNodes("//section[@class='ranking-item']");
                }
            }
            catch
            {
                throw new Exception("没有找到图片哦~ .=ω=");
            }

            if (srcType == PixivSrcType.Tag || srcType == PixivSrcType.TagFull)
            {
                if (tagNode == null)
                {
                    return(imgs);
                }
            }
            else if (nodes == null)
            {
                return(imgs);
            }

            //Tag search js-mount-point-search-related-tags Json
            if (srcType == PixivSrcType.Tag || srcType == PixivSrcType.TagFull)
            {
                string   jsonData = tagNode.Attributes["data-items"].Value.Replace("&quot;", "\"");
                object[] array    = (new JavaScriptSerializer()).DeserializeObject(jsonData) as object[];
                foreach (object o in array)
                {
                    Dictionary <string, object> obj = o as Dictionary <string, object>;
                    string
                        detailUrl = "",
                        SampleUrl = "",
                        id        = "";
                    if (obj["illustId"] != null)
                    {
                        id        = obj["illustId"].ToString();
                        detailUrl = SiteUrl + "/member_illust.php?mode=medium&illust_id=" + id;
                    }
                    if (obj["url"] != null)
                    {
                        SampleUrl = obj["url"].ToString();
                    }
                    Img img = GenerateImg(detailUrl, SampleUrl, id);
                    if (img != null)
                    {
                        imgs.Add(img);
                    }
                }
            }
            else
            {
                foreach (HtmlNode imgNode in nodes)
                {
                    try
                    {
                        HtmlNode anode = imgNode.SelectSingleNode("a");
                        if (srcType == PixivSrcType.Day || srcType == PixivSrcType.Month || srcType == PixivSrcType.Week)
                        {
                            anode = imgNode.SelectSingleNode(".//div[@class='ranking-image-item']").SelectSingleNode("a");
                        }
                        //details will be extracted from here
                        //eg. member_illust.php?mode=medium&illust_id=29561307&ref=rn-b-5-thumbnail
                        //sampleUrl 正则 @"https://i\.pximg\..+?(?=")"
                        string detailUrl = anode.Attributes["href"].Value.Replace("amp;", "");
                        string sampleUrl = "";
                        sampleUrl = anode.SelectSingleNode(".//img").Attributes["src"].Value;

                        if (sampleUrl.ToLower().Contains("images/common"))
                        {
                            sampleUrl = anode.SelectSingleNode(".//img").Attributes["data-src"].Value;
                        }

                        if (sampleUrl.Contains('?'))
                        {
                            sampleUrl = sampleUrl.Substring(0, sampleUrl.IndexOf('?'));
                        }

                        //extract id from detail url
                        //string id = detailUrl.Substring(detailUrl.LastIndexOf('=') + 1);
                        string id = Regex.Match(detailUrl, @"illust_id=\d+").Value;
                        id = id.Substring(id.IndexOf('=') + 1);

                        Img img = GenerateImg(detailUrl, sampleUrl, id);
                        if (img != null)
                        {
                            imgs.Add(img);
                        }
                    }
                    catch
                    {
                        //int i = 0;
                    }
                }
            }

            return(imgs);
        }