Пример #1
0
        public async Task <NicoNicoNicoRepoResult> GetNicoRepoAsync(string type, string nextPage)
        {
            try {
                //ニコレポのhtmlを取得
                var a = await App.ViewModelRoot.CurrentUser.Session.GetAsync("http://www.nicovideo.jp/my/top/" + type + "?innerPage=1&mode=next_page" + ((nextPage == null) ? "" : "&last_timeline=" + nextPage));


                var doc = new HtmlDocument();
                doc.LoadHtml(a);

                var timeline = doc.DocumentNode.SelectNodes("//div[@class='timeline']/div");

                //無いとき
                if (timeline == null)
                {
                    return(null);
                }

                var ret = new NicoNicoNicoRepoResult();
                ret.Items = new List <NicoNicoNicoRepoResultEntry>();

                //ニコレポタイムラインを走査
                foreach (var entry in timeline)
                {
                    var item   = new NicoNicoNicoRepoResultEntry();
                    var author = entry.SelectSingleNode("div[contains(@class, 'log-author ')]");

                    if (author != null)
                    {
                        item.NicoRepoThumbNail = author.SelectSingleNode("a/img").Attributes["data-original"].Value;
                        item.AuthorUrl         = author.SelectSingleNode("a").Attributes["href"].Value;
                    }

                    var content = entry.SelectSingleNode("div[@class='log-content']");

                    if (content != null)
                    {
                        var body = content.SelectSingleNode("div[@class='log-body']");
                        if (body != null)
                        {
                            item.Title = body.InnerHtml;
                        }

                        var detail = content.SelectSingleNode("div[contains(@class, 'log-details')]");

                        if (detail != null)
                        {
                            item.Time = detail.SelectSingleNode("div/div/span/time")?.InnerText.Trim() ?? "";

                            item.ContentThumbNail = detail.SelectSingleNode("div[@class='log-target-thumbnail']/a/img")?.Attributes["data-original"].Value ?? "";

                            var target = detail.SelectSingleNode("div[@class='log-target-info']/a");
                            if (target != null)
                            {
                                item.HasContent   = true;
                                item.ContentTitle = HttpUtility.HtmlDecode(target.InnerText.Trim());
                                item.ContentUrl   = target.Attributes["href"].Value;

                                NicoNicoUtil.ApplyLocalHistory(item);
                            }
                        }
                    }

                    if (item.ContentUrl == null)
                    {
                        item.ContentUrl = item.AuthorUrl;
                    }

                    ret.Items.Add(item);
                }

                var next = doc.DocumentNode.SelectSingleNode("//a[@class='next-page-link']");

                if (next != null)
                {
                    ret.NextPage = Regex.Match(next.Attributes["href"].Value, @"\d+$").Value;
                }
                else
                {
                    ret.IsEnd    = true;
                    ret.NextPage = null;
                }
                return(ret);
            } catch (RequestFailed e) {
                if (e.FailedType == FailedType.Failed)
                {
                    Owner.Status = "ニコレポの取得に失敗しました";
                }
                else
                {
                    Owner.Status = "ニコレポの取得がタイムアウトになりました";
                }

                return(null);
            }
        }
Пример #2
0
        //一度nullを返してきたら二度と呼ばない
        public async Task <NicoNicoNicoRepoResult> GetUserNicoRepoAsync(string offset)
        {
            var url = Owner.UserPageUrl + "/top?innerPage=1&offset=" + offset;

            Owner.Status = "ユーザーニコレポ取得中";

            var ret = new NicoNicoNicoRepoResult();

            ret.Items = new List <NicoNicoNicoRepoResultEntry>();

            try {
                var a = await App.ViewModelRoot.CurrentUser.Session.GetAsync(url);

                var doc = new HtmlDocument();
                doc.LoadHtml(a);

                var timeline = doc.DocumentNode.SelectNodes("//div[@class='timeline']/div");

                //タイムラインを取得できなかったら終了
                if (timeline == null)
                {
                    Owner.Status = "";
                    return(null);
                }

                //ニコレポタイムライン走査
                foreach (var entry in timeline)
                {
                    var item = new NicoNicoNicoRepoResultEntry();

                    var author = entry.SelectSingleNode("div[contains(@class, 'log-author ')]");

                    if (author != null)
                    {
                        item.NicoRepoThumbNail = author.SelectSingleNode("a/img").Attributes["data-original"].Value;
                        item.AuthorUrl         = author.SelectSingleNode("a").Attributes["href"].Value;
                    }

                    var content = entry.SelectSingleNode("div[@class='log-content']");

                    if (content != null)
                    {
                        var body = content.SelectSingleNode("div[@class='log-body']");
                        if (body != null)
                        {
                            item.Title = body.InnerHtml;
                        }

                        var detail = content.SelectSingleNode("div[contains(@class, 'log-details')]");

                        if (detail != null)
                        {
                            item.Time = detail.SelectSingleNode("div/div/a/time")?.InnerText.Trim() ?? "";

                            item.ContentThumbNail = detail.SelectSingleNode("div[@class='log-target-thumbnail']/a/img")?.Attributes["data-original"].Value ?? "";

                            var target = detail.SelectSingleNode("div[@class='log-target-info']/a");
                            if (target != null)
                            {
                                item.HasContent   = true;
                                item.ContentTitle = HttpUtility.HtmlDecode(target.InnerText.Trim());
                                item.ContentUrl   = target.Attributes["href"].Value;

                                NicoNicoUtil.ApplyLocalHistory(item);
                            }
                        }
                    }

                    if (item.ContentUrl == null)
                    {
                        item.ContentUrl = item.AuthorUrl;
                    }

                    ret.Items.Add(item);

                    var next = doc.DocumentNode.SelectSingleNode("//a[@class='next-page-link']");

                    if (next != null)
                    {
                        ret.NextPage = Regex.Match(next.Attributes["href"].Value, @"\d+$").Value;
                    }
                    else
                    {
                        ret.IsEnd    = true;
                        ret.NextPage = null;
                    }
                }
                Owner.Status = "";
                return(ret);
            } catch (RequestFailed) {
                Owner.Status = "ユーザーニコレポの取得に失敗しました";
                return(null);
            }
        }