Esempio n. 1
0
        async Task <SubscriptionFeedUpdateResult> GetFeedResultAsync(SubscriptionSourceEntity entity)
        {
            try
            {
                var videos = entity.SourceType switch
                {
                    SubscriptionSourceType.Mylist => await GetMylistFeedResult(entity.SourceParameter, _mylistProvider),
                    SubscriptionSourceType.User => await GetUserVideosFeedResult(entity.SourceParameter, _userProvider),
                    SubscriptionSourceType.Channel => await GetChannelVideosFeedResult(entity.SourceParameter, _channelProvider, _nicoVideoProvider),
                    SubscriptionSourceType.Series => await GetSeriesVideosFeedResult(entity.SourceParameter, _seriesRepository),
                    SubscriptionSourceType.SearchWithKeyword => await GetKeywordSearchFeedResult(entity.SourceParameter, _searchProvider),
                    SubscriptionSourceType.SearchWithTag => await GetTagSearchFeedResult(entity.SourceParameter, _searchProvider),
                    _ => throw new NotSupportedException(entity.SourceType.ToString())
                };

                return(new SubscriptionFeedUpdateResult()
                {
                    IsSuccessed = true,
                    Videos = videos,
                    Entity = entity
                });
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());

                return(new SubscriptionFeedUpdateResult()
                {
                    IsSuccessed = false,
                    Videos = new List <NicoVideo>(),
                    Entity = entity
                });
            }
        }
Esempio n. 2
0
        public SubscriptionFeedResult MargeFeedResult(SubscriptionFeedResult target, SubscriptionSourceEntity source, IList <NicoVideo> videos)
        {
            var result = target ?? _collection.FindOne(x => x.SourceType == source.SourceType && x.SourceParamater == source.SourceParameter);

            if (result == null)
            {
                result = new SubscriptionFeedResult()
                {
                    Id              = ObjectId.NewObjectId(),
                    SourceType      = source.SourceType,
                    SourceParamater = source.SourceParameter,
                    Videos          = videos.Select(ToFeedResultVideoItem).ToList(),
                    LastUpdatedAt   = DateTime.Now
                };

                _collection.Insert(result);

                return(result);
            }
            else
            {
                // 前回更新分までのIdsと新規分のIdの差集合を取る
                var ids          = result.Videos.Select(x => x.VideoId).ToHashSet();
                var exceptVideos = videos.Where(x => false == ids.Contains(x.Id));
                result.Videos        = Enumerable.Concat(exceptVideos.Select(ToFeedResultVideoItem), result.Videos).Take(FeedResultVideosCapacity).ToList();
                result.LastUpdatedAt = DateTime.Now;

                _collection.Update(result);

                return(result);
            }
        }
Esempio n. 3
0
        SubscriptionSourceEntity AddSubscription_Internal(SubscriptionSourceEntity newEntity)
        {
            _subscriptionRegistrationRepository.CreateItem(newEntity);

            Added?.Invoke(this, newEntity);

            return(newEntity);
        }
Esempio n. 4
0
        public bool DeleteItem(SubscriptionSourceEntity source)
        {
            var result = GetFeedResult(source);

            if (result != null)
            {
                return(DeleteItem(result.Id));
            }
            else
            {
                return(false);
            }
        }
Esempio n. 5
0
        public void RemoveSubscription(SubscriptionSourceEntity entity)
        {
            var registrationRemoved = _subscriptionRegistrationRepository.DeleteItem(entity.Id);

            Debug.WriteLine("[SubscriptionSource Remove] registration removed: " + registrationRemoved);

            var feedResultRemoved = _subscriptionFeedResultRepository.DeleteItem(entity.Id);

            Debug.WriteLine("[SubscriptionSource Remove] feed result removed: " + feedResultRemoved);

            if (registrationRemoved || feedResultRemoved)
            {
                Removed?.Invoke(this, entity);
            }
        }
Esempio n. 6
0
        public async Task <bool> RefreshFeedUpdateResultAsync(SubscriptionSourceEntity entity, CancellationToken cancellationToken = default)
        {
            var prevResult = _subscriptionFeedResultRepository.GetFeedResult(entity);

            if (prevResult != null && !IsExpiredFeedResultUpdatedTime(prevResult.LastUpdatedAt))
            {
                // 前回更新から時間経っていない場合はスキップする
                Debug.WriteLine("[FeedUpdate] update skip: " + entity.Label);
                return(false);
            }

            Debug.WriteLine("[FeedUpdate] start: " + entity.Label);

            // オンラインソースから情報を取得して
            var result = await GetFeedResultAsync(entity);

            cancellationToken.ThrowIfCancellationRequested();

            // 新規動画を抽出する
            // 初回更新時は新着抽出をスキップする
            if (prevResult != null)
            {
                var prevContainVideoIds = prevResult.Videos.Select(x => x.VideoId).ToHashSet();
                var newVideos           = result.Videos.TakeWhile(x => !prevContainVideoIds.Contains(x.Id));
                result.NewVideos = newVideos.ToList();
            }
            else
            {
                result.NewVideos = new List <NicoVideo>();
            }

            // 成功したら前回までの内容に追記して保存する
            if (result.IsSuccessed && (result.Videos?.Any() ?? false))
            {
                var updatedResult = _subscriptionFeedResultRepository.MargeFeedResult(prevResult, entity, result.Videos);
            }

            // 更新を通知する
            Updated?.Invoke(this, result);


            Debug.WriteLine("[FeedUpdate] complete: " + entity.Label);

            return(true);
        }
Esempio n. 7
0
 public void UpdateSubscription(SubscriptionSourceEntity entity)
 {
     _subscriptionRegistrationRepository.UpdateItem(entity);
 }
Esempio n. 8
0
 public SubscriptionFeedResult GetFeedResult(SubscriptionSourceEntity source)
 {
     return(_collection.FindOne(x => x.SourceType == source.SourceType && x.SourceParamater == source.SourceParameter));
 }