Exemple #1
0
        /// <summary>
        /// get followers of the connected channels
        /// </summary>
        /// <returns>enumeration of followers</returns>
        IEnumerable <UserInformation> GetFollowers_Api()
        {
            if (channeldata == null)
            {
                Logger.Warning(this, "No channel connected to get followers");
                yield break;
            }

            string pagination = null;

            do
            {
                GetFollowerResponse response = null;
                try {
                    response = twitchapi.GetFollowers(userdata.ID, 100, pagination);
                }
                catch (Exception e) {
                    Logger.Warning(this, $"Unable to get followers for '{userdata.Login}'", e.Message);
                }

                if (response != null)
                {
                    User[] users = context.GetModule <UserModule>().GetUsersByKey(TwitchConstants.ServiceKey, response.Data.Select(u => u.FromID).ToArray());

                    string[] unknownids = response.Data.Where(u => users.All(us => us.Key != u.FromID)).Select(u => u.FromID).ToArray();

                    try {
                        users = users.Concat(UpdateUserData(unknownids)).ToArray();
                    }
                    catch (Exception e) {
                        Logger.Error(this, "Unable to update unknown userdata", e);
                    }


                    foreach (UserInformation user in users.Select(f => new UserInformation {
                        Service = TwitchConstants.ServiceKey,
                        Username = f.Name,
                        Avatar = f.Avatar
                    }))
                    {
                        yield return(user);
                    }
                }
                if (response?.Data?.Length < 100)
                {
                    break;
                }

                pagination = response?.Pagination?.Cursor;
            }while(true);
        }
Exemple #2
0
 void Reconnect()
 {
     isreconnecting = true;
     Logger.Info(this, "Reconnecting in 5 seconds.");
     Thread.Sleep(5000);
     try
     {
         Connect();
         isreconnecting = false;
     }
     catch (Exception e)
     {
         Logger.Error(this, "Error reconnecting", e);
         new Task(Reconnect).Start();
     }
 }
Exemple #3
0
        void ITimerService.Process(double time)
        {
            if (!isconnected)
            {
                return;
            }

            followercheck -= time;
            if (followercheck <= 0.0)
            {
                try {
                    CheckFollowers();
                }
                catch (Exception e) {
                    Logger.Error(this, "Unable to check for followers", e);
                }

                followercheck = 300.0;
            }

            viewercheck -= time;
            if (viewercheck <= 0.0)
            {
                if (userdata != null)
                {
                    GetStreamsResponse response = twitchapi.GetStreams(userdata.ID);
                    TwitchStream       stream   = response.Data.FirstOrDefault();
                    if (stream == null)
                    {
                        Logger.Warning(this, "There was no active stream found.");
                        viewercheck = 180.0;
                        return;
                    }

                    Viewers     = stream.ViewerCount;
                    viewercheck = 180.0;
                }
            }
        }
Exemple #4
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);
        }