예제 #1
0
        private async Task StartLikesAnalizingAsync(
            ApplicationUser observer, ObservableUser observable, ObservableUser targetUser)
        {
            IInstagramAPI instagramAPI = new InstagramAPI.InstagramAPI();

            instagramAPI.SetCookies(observer.StateData);

            List <InstagramPost> posts =
                await GetProfileStateAsync(instagramAPI, targetUser);

            var cancellationTokenSource = new CancellationTokenSource();

            Task likesAnalizingTask = Task.Run(
                () =>
            {
                List <InstagramPost> newPostsState = new List <InstagramPost>();

                do
                {
                    newPostsState =
                        instagramAPI.GetUserPostsByPrimaryKey(targetUser.InstagramPK);

                    if (AreDifferencesPresent(posts, newPostsState))
                    {
                        var postsWithDifference = GetDistinctivePosts(posts, newPostsState);

                        foreach (var distinctivePost in postsWithDifference)
                        {
                            CheckForUserLike(observer, observable, targetUser, distinctivePost.Key, distinctivePost.Value);
                        }
                    }

                    posts = newPostsState;

                    try
                    {
                        Sleep(SLEEP_TIMEOUT, cancellationTokenSource.Token);
                    }
                    catch (OperationCanceledException ex)
                    {
                        break;
                    }
                } while (!cancellationTokenSource.Token.IsCancellationRequested);

                SaveProfileStateAsync(targetUser, newPostsState);
            });

            TryAddToTasks(observer, observable, targetUser, likesAnalizingTask, cancellationTokenSource);
        }
예제 #2
0
        public void Dispose()
        {
            foreach (var analizationTask in _tasks)
            {
                analizationTask.CancellationTokenSource.Cancel();

                IInstagramAPI instagramAPI = new InstagramAPI.InstagramAPI();
                instagramAPI.SetCookies(analizationTask.Observer.StateData);

                foreach (var task in analizationTask.Tasks)
                {
                    SaveProfileStateAsync(
                        analizationTask.TargetUser,
                        instagramAPI.GetUserPostsByPrimaryKey(analizationTask.TargetUser.InstagramPK));
                    task.Wait(TASK_WAITING_TIMEOUT);
                }
            }
        }