Example #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);
        }
Example #2
0
        /// <summary>
        /// get subscribers of a channel
        /// </summary>
        /// <returns>list of subscribers</returns>
        public IEnumerable <UserInformation> GetFollowers()
        {
            if (channeldata == null)
            {
                Logger.Warning(this, "No channel connected to get subscribers");
                yield break;
            }

            int offset = 0;

            do
            {
                FollowResponse response = null;
                try
                {
                    response = channels.GetChannelFollowers(channeldata.ID, 100, offset);
                }
                catch (Exception e)
                {
                    Logger.Warning(this, $"Unable to get followers for channel '{channeldata.ID}'", e.Message);
                    throw;
                }

                if (response != null)
                {
                    foreach (UserInformation user in response.Follows.Select(s => new UserInformation
                    {
                        Service = TwitchConstants.ServiceKey,
                        Username = s.User.Name,
                        Avatar = s.User.Logo,
                    }))
                    {
                        yield return(user);
                    }
                }

                if (response?.Total < 100)
                {
                    break;
                }
                offset += 100;
            }while (true);
        }
Example #3
0
        public void Connect()
        {
            if (isconnected)
            {
                Logger.Warning(this, "Already connected");
                return;
            }

            string chatchannel = context.GetModule <TwitchChatModule>().Username;
            string usertoken   = context.GetModule <TwitchChatModule>().AccessToken;

            if (string.IsNullOrEmpty(usertoken))
            {
                Logger.Warning(this, "User not connected");
                return;
            }

            twitchapi   = new TwitchApi(TwitchConstants.ClientID, usertoken);
            channels    = new Channels(TwitchConstants.ClientID, usertoken);
            channeldata = channels.GetChannel();

            userdata = twitchapi.GetUsersByLogin(chatchannel).Data.FirstOrDefault();
            if (userdata == null)
            {
                Logger.Warning(this, $"No userdata found for '{chatchannel}'");
            }

            if (string.IsNullOrEmpty(botname) || string.IsNullOrEmpty(accesstoken))
            {
                Logger.Warning(this, "No credentials set");
                return;
            }

            if (!string.IsNullOrEmpty(chatchannel))
            {
                Logger.Info(this, $"Connecting to @{botname} to #{chatchannel}");
                chatclient.Connect(botname, accesstoken);
                chatclient.Join(chatchannel);
            }

            isconnected = true;
            Connected?.Invoke();
        }
Example #4
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;
                }
            }
        }
Example #5
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);
        }