示例#1
0
        public async Task <Tuple <Paginated <Work>, string> > AccessApiAsyncECD()
        {
            HttpClient client = new HttpClient();

            client.DefaultRequestHeaders.Add("Referer", "http://spapi.pixiv.net/");
            client.DefaultRequestHeaders.Add("User-Agent", "PixivIOSApp/5.8.0");

            var response = await client.GetAsync($"https://" +
                                                 $"www.pixiv.net/search.php?word={searchquery}&order=date_d&p={ecdPage}&ecd={activeEcd}");

            if (response.IsSuccessStatusCode == false)
            {
                return(new Tuple <Paginated <Work>, string>(new Paginated <Work>(), "Bad Request"));
            }

            var src = await response.Content.ReadAsStringAsync();

            // parse contents
            HtmlDocument htmlDoc = new HtmlDocument();

            htmlDoc.LoadHtml(src);

            var inputs = from input in htmlDoc.DocumentNode.Descendants("li")
                         where input.Attributes["class"] != null
                         where input.Attributes["class"].Value == "image-item"
                         select input;

            Paginated <Work> foundWorks = new Paginated <Work>();

            // go through all entries

            var tasks = new List <Task>();

            foreach (var i in inputs)
            {
                string content = i.InnerHtml;
                var    workId  = long.Parse(Regex.Match(content, @"data-id=""(\d*?)""").Groups[1].Value);
                Task   t       = new Task(() =>
                {
                    var wrks = GetWorksAsync(workId).Result;

                    lock (foundWorks)
                    {
                        foundWorks.Add(wrks.Item1.First());
                    }
                });
                tasks.Add(t);
                t.Start();
            }

            await Task.WhenAll(tasks);

            return(new Tuple <Paginated <Work>, string>(foundWorks, null)); // set paginated works
        }