Example #1
0
        private async void OnWatchFollowers(object state)
        {
            // async void as TimerCallback delegate
            try
            {
                // Turn off timer, in case runs longer than interval
                _FollowersTimer?.Change(Timeout.Infinite, Timeout.Infinite);
                try
                {
                    var foundFollowerCount = await GetFollowerCountAsync();

                    if (foundFollowerCount != _WatchedFollowerCount)
                    {
                        _WatchedFollowerCount = foundFollowerCount;
                        NewFollowers?.Invoke(this, new NewFollowersEventArgs(foundFollowerCount));
                    }
                }
                finally
                {
                    // Turn on timer
                    var intervalMs = Math.Max(1000, _WatchFollowersIntervalMs);
                    _FollowersTimer?.Change(intervalMs, intervalMs);
                }
            }
            catch (Exception ex)
            {
                // Don't let exception escape from async void
                Logger.LogError($"{DateTime.UtcNow}: OnWatchFollowers - Error {Environment.NewLine}{ex}");
            }
        }
Example #2
0
        /// <summary>
        /// Call the server and get the first 25 followers. After that we call every once in a while to watch for new ones
        /// </summary>
        private void _callTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            // This isn't necessary for the program to keep running. If it fails, catch the error and just keep going
            try
            {
                string followers = GetFollowers(@"https://api.twitch.tv/kraken/channels/" + Channel + @"/follows?limit=10");

                JsonObject root    = (JsonObject)SimpleJson.SimpleJson.DeserializeObject(followers);
                JsonArray  follows = (JsonArray)root["follows"];

                List <User> newFollowers = new List <User>();

                // Add all the followers, and record the new ones in newFollowers to be passed through the event
                foreach (object obj in follows)
                {
                    JsonObject follow = (JsonObject)obj;
                    JsonObject user   = (JsonObject)follow["user"];

                    DateTime followDateTime;
                    if (DateTime.TryParse(follow["created_at"].ToString(), out followDateTime))
                    {
                        var newFollower = new User()
                        {
                            UserName         = user["display_name"].ToString(),
                            FollowedDateTime = followDateTime,
                        };

                        if (Followers.Contains(newFollower))
                        {
                            continue;
                        }

                        Followers.Add(newFollower);
                        newFollowers.Add(newFollower);
                    }
                    else
                    {
                        //DebugOutputPanel.AddMessage(PluginManager.MessageType.Message, "Can't parse " + follow["created_at"].ToString() + " to a DateTime");
                    }
                }

                if (newFollowers.Count > 0 && !_isFirstCall)
                {
                    NewFollowers?.Invoke(this, new NewFollowersEventArgs(newFollowers));
                }

                _isFirstCall = false;
            }
            catch (Exception ex)
            {
                Logger.AddEntry(ex);
            }
        }