private async void DoWork(object state)
        {
            var count = Interlocked.Increment(ref executionCount);

            if (IsRunning == true)
            {
                return;
            }
            IsRunning = true;

            var t = new Task(() =>
            {
                var twitterFriends = new TwitterFriends();

                if (newfollwersList.Where(f => f.result == null).Count() == 0)
                {
                    var newFriends      = twitterFriends.GetNewFriends();
                    var existingFriends = newfollwersList.Select(f => f.followID).ToList();

                    foreach (var newFriend in newFriends.Except(existingFriends))
                    {
                        newfollwersList.Add(new NewFollower {
                            followID = newFriend
                        });
                    }
                }
                else
                {
                    _logger.LogInformation("Need to Add " + newfollwersList.Where(f => f.result == null).Count());

                    NewFollower addFollwer = newfollwersList.Where(f => f.result == null).First();

                    try
                    {
                        twitterFriends.AddFollow(addFollwer.followID);
                        addFollwer.result = true;
                        _logger.LogInformation($"Added {addFollwer.followID}");
                    }
                    catch
                    {
                        _logger.LogInformation($"ERROR {addFollwer.followID}");
                        addFollwer.result = false;
                    }
                }

                IsRunning = false;
            });

            t.Start();

            await t;

            _logger.LogInformation("Timed Hosted Service is working. Count: {Count}", count);
        }
Example #2
0
 /// <summary>
 /// adds a service to the collection
 /// </summary>
 /// <param name="type">type of service</param>
 /// <param name="module">module handling service calls</param>
 public void AddService(string type, IStreamServiceModule module)
 {
     services[type]    = module;
     module.Connected += () => {
         Logger.Info(this, $"{type} connected");
         ServiceConnected?.Invoke(type);
     };
     module.NewFollower += information => {
         Logger.Info(this, $"New follower '{information.Username}' on {type}.");
         if (ValidUser(information.Service, information.Username))
         {
             NewFollower?.Invoke(information);
         }
     };
     module.NewSubscriber += AddSubscriber;
     if (module is IStreamStatsModule)
     {
         ((IStreamStatsModule)module).ViewersChanged += OnViewersChanged;
     }
 }
Example #3
0
        void CheckFollowers()
        {
            if (cooldown > 0)
            {
                --cooldown;
                return;
            }

            cooldown = 5;

            UserModule usermodule = context.GetModule <UserModule>();

            try {
                foreach (UserInformation follower in GetFollowers())
                {
                    usermodule.SetInitialized(TwitchConstants.ServiceKey, follower.Username);

                    if (!string.IsNullOrEmpty(follower.Avatar))
                    {
                        User user = usermodule.GetUser(TwitchConstants.ServiceKey, follower.Username);
                        if (user.Avatar != follower.Avatar)
                        {
                            usermodule.UpdateUserAvatar(user, follower.Avatar);
                        }
                    }

                    if (usermodule.GetUserStatus(TwitchConstants.ServiceKey, follower.Username) >= UserStatus.Follower)
                    {
                        continue;
                    }

                    if (usermodule.SetUserStatus(TwitchConstants.ServiceKey, follower.Username, UserStatus.Follower))
                    {
                        NewFollower?.Invoke(follower);
                    }
                }
            }
            catch (WebException e)
            {
                Logger.Warning(this, "Unable to get followers", e.Message);
                return;
            }
            catch (Exception e)
            {
                Logger.Error(this, "Unable to get followers", e);
                return;
            }

            try
            {
                foreach (SubscriberInformation subscriber in GetSubscribers())
                {
                    usermodule.SetInitialized(TwitchConstants.ServiceKey, subscriber.Username);

                    if (!string.IsNullOrEmpty(subscriber.Avatar))
                    {
                        User user = usermodule.GetUser(TwitchConstants.ServiceKey, subscriber.Username);
                        if (user.Avatar != subscriber.Avatar)
                        {
                            usermodule.UpdateUserAvatar(user, subscriber.Avatar);
                        }
                    }

                    if (usermodule.GetUserStatus(TwitchConstants.ServiceKey, subscriber.Username) >= subscriber.Status)
                    {
                        continue;
                    }

                    if (usermodule.SetUserStatus(TwitchConstants.ServiceKey, subscriber.Username, subscriber.Status))
                    {
                        NewSubscriber?.Invoke(subscriber);
                    }
                }
            }
            catch (WebException e) {
                Logger.Warning(this, "Unable to get subscribers", e.Message);
                return;
            }
            catch (Exception e) {
                Logger.Error(this, "Unable to get subscribers", e);
                return;
            }

            context.GetModule <UserModule>().EndInitialization(TwitchConstants.ServiceKey);
        }