Exemplo n.º 1
0
 /// <summary>
 /// 最近の生放送を取得する
 /// </summary>
 /// <returns></returns>
 public static IEnumerable <Live> Fetch()
 {
     using (var client = new WebClientWithCookie())
     {
         client.Encoding = UTF8Encoding.UTF8;
         var requestUrl   = "http://ch.nicovideo.jp/menu/anime/";
         var htmlString   = client.DownloadString(requestUrl);
         var htmlDocument = new HtmlDocument();
         htmlDocument.LoadHtml(htmlString);
         var rootNode = htmlDocument.DocumentNode;
         foreach (var itemNode in rootNode.CssSelect("#sec_live li"))
         {
             var   imageUri = new Uri(itemNode.CssSelect(".symbol img").First().Attributes["src"].Value);
             Image thumbnail;
             using (var stream = HttpWebRequest.Create(imageUri).GetResponse().GetResponseStream())
             {
                 thumbnail = Image.FromStream(stream);
             }
             var id       = Regex.Match(itemNode.CssSelect(".symbol a").First().Attributes["href"].Value, @"(lv[0-9]+)").Groups[1].Value;
             var title    = itemNode.CssSelect(".tit a").First().InnerText;
             var openTime = DateTime.Now;
             if (itemNode.CssSelect(".detail .date strong").Count() > 0)
             {
                 openTime = TimeUtil.ParseAnimeString(itemNode.CssSelect(".detail .date strong").First().InnerText);
             }
             yield return(new Live {
                 Id = id, Title = title, OpenTime = openTime, Thumbnail = thumbnail
             });
         }
     }
 }
Exemplo n.º 2
0
        public FetchResult Fetch(Live live)
        {
            var accessUrl      = string.Format("http://watch.live.nicovideo.jp/api/getplayerstatus?v={0}", live.Id);
            var responseString = client.DownloadString(accessUrl);
            var xml            = XElement.Parse(responseString);
            var status         = xml.Attribute("status").Value;

            if (status != "ok")
            {
                var errorCode = xml.Descendants("code").First().Value;
                status = errorCode;
            }
            var nowTime = TimeUtil.UnixTimeToDateTime(xml.Attribute("time").Value).ToLocalTime();
            var result  = new FetchResult();

            result.Time = nowTime;
            switch (status)
            {
            case "ok":
                result.Status = Status.Ok;
                var seatLabel  = xml.Descendants("room_label").First().Value;
                var seatNumber = int.Parse(xml.Descendants("room_seetno").First().Value);
                var seat       = new Seat {
                    Label = seatLabel, Number = seatNumber
                };
                result.Seat = seat;
                break;

            case "comingsoon":
                result.Status = Status.ComingSoon;
                break;

            case "notlogin":
                result.Status = Status.NotLogin;
                break;

            case "noauth":
                result.Status = Status.NoAuth;
                break;

            case "closed":
                result.Status = Status.Closed;
                break;

            case "require_community_member":
                result.Status = Status.RequireCommunityMember;
                break;

            case "notfound":
                result.Status = Status.NotFound;
                break;

            default:
                throw new Exception("unknown fetch result status: " + status);
            }
            return(result);
        }