/// <summary> /// Constructor /// </summary> /// <param name="clientid"> Bot id, as seen on "https://www.reddit.com/prefs/apps/" </param> /// <param name="clientsecret"> Bot id, as seen on "https://www.reddit.com/prefs/apps/" </param> /// <param name="redditusername"> Reddit username for login and authentication </param> /// <param name="redditpassword"> Reddit password for login and authentication </param> /// <param name="clientversion"> Client Version, personal to create an unique identity </param> public RedditBot(string clientid, string clientsecret, string redditusername, string redditpassword, string clientversion) { _clientid = clientid; _clientsecret = clientsecret; _redditusername = redditusername; _redditpassword = redditpassword; _clientversion = clientversion; _tb = new TokenBucket(60, 60); _client = new HttpClient(); SetBasicAuthenticationHeader(); SetCustomUserAgent(); Authenticate(); _handler = new MessageHandler(_client, _tb); /// Infinite loop /// check if _handler.Run takes more than if 10 seconds to execute. /// If so, sleep for 10000 millisecond (10 seconds) minus time of execution /// If not, run again while (true) { var watch = System.Diagnostics.Stopwatch.StartNew(); _handler.Run(); watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; var timeToSleep = 10000 - elapsedMs; if (timeToSleep > 0) { System.Threading.Thread.Sleep((int)timeToSleep); } } }
public MessageHandler(HttpClient client, TokenBucket tb) { _client = client; _tb = tb; }