예제 #1
0
파일: Mushi.cs 프로젝트: sokushu/lilys-moe
        /// <summary>
        /// 得到指定动画的剧集信息
        /// </summary>
        /// <param name="season_id"></param>
        /// <param name="_MoeMushiDB">数据库连接</param>
        /// <returns></returns>
        public Task <List <AnimeInfoSea> > Bilibili_Animeinfo(int season_id, MoeMushiContext _MoeMushiDB)
        {
            return(Task.Run(() =>
            {
                HttpHelpers httpHelpers = new HttpHelpers();
                HttpItems httpItems = new HttpItems
                {
                    Url = BILIBILI.BILIBILI_AnimeNumInfo(season_id: season_id),
                    Method = HttpMethod.Get.Method
                };
                HttpResults results = httpHelpers.GetHtml(httpItems);
                List <AnimeInfoSea> list = new List <AnimeInfoSea>();
                if (results.StatusCodeNum != 200)
                {
                    _MoeMushiDB.LogInfos.Add(new _LogInfo
                    {
                        Info = $"{season_id} results.StatusCodeNum 出现问题 返回代码:{results.StatusCodeNum}",
                        Time = DateTime.Now
                    });
                    _MoeMushiDB.SaveChanges();
                }
                else
                {
                    JObject Json = JsonConvert.DeserializeObject <JObject>(results.Html);

                    var j = Json["result"]?["main_section"]?["episodes"];
                    if (j != null)
                    {
                        int AnimeNumber = 1;
                        foreach (var Info in j)
                        {
                            AnimeInfoSea animeInfo = new AnimeInfoSea
                            {
                                AnimeNum = AnimeNumber,
                                Aid = (int)(Info["aid"] ?? -1),
                                Cid = (int)(Info["cid"] ?? -1),
                                Cover = (string)Info["cover"],
                                Id = (int)(Info["id"] ?? -1),
                                Long_title = (string)Info["long_title"],
                                Share_url = (string)Info["share_url"],
                                Season_id = season_id,
                                Stat_play = (int)(Info["stat"]?["play"] ?? -1),
                                Title = (string)Info["title"]
                            };
                            list.Add(animeInfo);
                            AnimeNumber++;
                        }
                    }
                    else
                    {
                        _MoeMushiDB.LogInfos.Add(new _LogInfo
                        {
                            Info = $"{season_id} Json['result']?['main_section']?['episodes'] 出现问题",
                            Time = DateTime.Now
                        });
                        _MoeMushiDB.SaveChanges();
                    }
                }
                return list;
            }));
        }
예제 #2
0
파일: Mushi.cs 프로젝트: sokushu/lilys-moe
        /// <summary>
        /// 得到B站的新番时间表
        /// </summary>
        /// <param name="_MoeMushiDB">数据库连接</param>
        /// <returns></returns>
        private Task BILIBILI_Timeline_Global(MoeMushiContext _MoeMushiDB)
        {
            return(Task.Run(async() =>
            {
                IsEnd = false;
                HttpHelpers httpHelpers = new HttpHelpers();

                HttpItems httpItems = new HttpItems
                {
                    Url = BILIBILI.BILIBILI_Timeline_Global(),
                    Method = HttpMethod.Get.Method
                };

                HttpResults results = httpHelpers.GetHtml(httpItems);

                HttpStatusCode code = results.StatusCode;

                if (code != HttpStatusCode.OK)
                {
                    //这里放个提醒,嘿,链接可能失效
                    //这里写入数据库中比较好
                }
                else
                {
                    //将得到的数据转换
                    JObject json = JsonConvert.DeserializeObject <JObject>(results.Html);
                    HashSet <int> hash = new HashSet <int>();
                    Dictionary <string, List <TimeLineInfo> > Data = new Dictionary <string, List <TimeLineInfo> >();

                    //是否成功?(success)
                    string IsSuccess = json["message"].ToString();
                    //提取数据
                    JToken jToken = json["result"];
                    //开始遍历天
                    foreach (var Day in jToken)
                    {
                        List <TimeLineInfo> list = new List <TimeLineInfo>();
                        //开始遍历动画
                        foreach (var animeInfo in Day["seasons"])
                        {
                            int season_id = (int)animeInfo["season_id"];
                            hash.Add(season_id);
                            TimeLineInfo timeLineAnimeInfo = new TimeLineInfo
                            {
                                Date = DateTime.Parse((string)Day["date"]),
                                Cover = (string)animeInfo["cover"],
                                Ep_id = (int)animeInfo["ep_id"],
                                Favorites = (int)animeInfo["favorites"],
                                Is_published = (int)animeInfo["is_published"] > 0 ? true : false,
                                Pub_index = (string)animeInfo["pub_index"],
                                Pub_time = (string)animeInfo["pub_time"],
                                Season_id = season_id,
                                Square_cover = (string)animeInfo["square_cover"],
                                Title = (string)animeInfo["title"],
                                Delay = (int)animeInfo["delay"] > 0 ? true : false,
                                Delay_index = (string)animeInfo["delay_index"],
                                Delay_reason = (string)animeInfo["delay_reason"]
                            };
                            list.Add(timeLineAnimeInfo);
                        }
                        Data.Add((string)Day["date"], list);
                    }

                    //先清除旧数据
                    _MoeMushiDB.TimeLines.FromSql("DELETE FROM TimeLineAnimeInfos");
                    foreach (var item in Data.Keys)
                    {
                        _MoeMushiDB.TimeLines.UpdateRange(Data[item]);
                    }
                    _MoeMushiDB.SaveChanges();

                    //先清除旧数据
                    _MoeMushiDB.AnimeInfoSeas.FromSql("DELETE FROM AnimeInfos");
                    //获取season_id的数据
                    foreach (int season_id in hash)
                    {
                        List <AnimeInfoSea> animeInfos = await Bilibili_Animeinfo(season_id, _MoeMushiDB);
                        _MoeMushiDB.AnimeInfoSeas.UpdateRange(animeInfos);
                        await Task.Delay(5000);//停5秒,防止过快被反爬虫
                    }
                    _MoeMushiDB.SaveChanges();
                }
            }));
        }