Пример #1
0
        public async Task DownloadVideoList()
        {
            try
            {
                TokenDao tokenDao = new TokenDao(connectionString);
                var      token    = tokenDao.GetAvaliableToken();
                if (token == null)
                {
                    logger.LogWarning("没有可用的Token");
                    return;
                }

                using (var client = HttpClientFactory.Create())
                {
                    string url      = $"{domain}/video/list?open_id={token.OpenId}&access_token={token.AccessToken}&cursor={0}&count={pageCount}";
                    var    response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, url));

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

                    var videoListResponse = JsonConvert.DeserializeObject <VideoListResponseModel>(content);
                    if (videoListResponse?.data.error_code != 0)
                    {
                        logger.LogError($"请求Token失败:{videoListResponse?.data?.error_code} {videoListResponse?.data?.description}");
                        return;
                    }

                    List <VideoEntity> entities = new List <VideoEntity>();
                    var dao = new VideoDao(connectionString);
                    videoListResponse?.data?.list?.ForEach(t =>
                    {
                        entities.Add(new VideoEntity()
                        {
                            ItemId        = t.item_id,
                            VideoStatus   = t.video_status,
                            Title         = t.title,
                            CreateTime    = StampToDateTime(t.create_time),
                            Cover         = t.cover,
                            ShareUrl      = t.share_url,
                            CommentCount  = t.statistics.comment_count,
                            DiggCount     = t.statistics.digg_count,
                            DownloadCount = t.statistics.download_count,
                            ForwardCount  = t.statistics.forward_count,
                            PlayCount     = t.statistics.play_count,
                            ShareCount    = t.statistics.share_count,
                            ReceiveTime   = DateTime.Now
                        });
                    });

                    if (entities.Count == 0)
                    {
                        return;
                    }

                    var existIds = dao.GetVideosByItemIds(entities.Select(t => t.ItemId).ToArray())
                                   .Select(t => t.ItemId)
                                   .ToArray();

                    var newEntities = entities
                                      .Where(t => !existIds.Contains(t.ItemId))
                                      .ToList();

                    dao.CreateVideo(newEntities);
                }
            }
            catch (Exception err)
            {
                logger.LogError("获取视频列表失败", err);
            }
        }