public void Worker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = (BackgroundWorker)sender; bool hasRun = false; while (hasRun == false) { //Check if cancellation was requested if (worker.CancellationPending) { //take any necessary action upon cancelling (rollback, etc.) //notify the RunWorkerCompleted event handler //that the operation was cancelled e.Cancel = true; return; } _Action = _TBSEntities.Action.FirstOrDefault(action => action.PrimaryKey == _Action.PrimaryKey); //report progress; this method has an overload which can also take //custom object (usually representing state) as an argument //worker.ReportProgress(/*percentage*/); if (_Action.Active == false) { e.Cancel = true; return; } //Action if (_Action.Destinations == null) { //There is no destination, therefore, skip return; } List <Status> tweets = new List <Status>(); try { ulong userID = (ulong)_Action.Queries.FirstOrDefault()?.TargetedUsers.FirstOrDefault()?.User.UserID.ToInt64(); tweets = _CommsModule.DoSearchOfUserSync( userID, null, _Action.LastFoundPostID.ToInt64(), true); //WriteLog(Log.LogLevels.Information, $"ActionHandler-{_Action.PrimaryKey}", "ActionHandler Run", $"ActionHandler for action {_Action.Description} ran at {DateTime.UtcNow}"); } catch (TwitterCommunications.Exceptions.RateLimitException ex) { WriteException(ex, ""); Thread.Sleep(900000); return; } catch (Exception ex) { //Log WriteException(ex, $"ActionHandler - {_Action.PrimaryKey.ToString()}:{_Action.Description}"); return; } foreach (var tweet in tweets) { string message = parseMessage(tweet); foreach (Destination destination in _Action.Destinations) { DiscordCommunications.Module.MessageOptions messageParams = new DiscordCommunications.Module.MessageOptions() { WebHookURL = destination.Webhook, Text = message, Username = tweet.User.Name, AvatarUrl = tweet.User.ProfileImageUrl }; DiscordCommunications.Module.SendMessage(messageParams); } } if (tweets.Any() == true) { string latestTweetID = tweets.Max(tweet => tweet.StatusID).ToString(); _Action.LastFoundPostID = latestTweetID.ToString(); } _Action.LastTimeChecked = DateTime.UtcNow; _TBSEntities.SaveChanges(); hasRun = true; } }
public void RunTest() { Console.OutputEncoding = System.Text.Encoding.UTF8; Console.WriteLine("Starting Test"); TwitterCommunications.Module module = new TwitterCommunications.Module(); Console.WriteLine("Module Created"); ulong iris_Official = 779723154; ulong iris_miyu = 2384783184; ulong iris_saki = 2384780834; string searchTerm = "ネットサイン会"; Console.WriteLine($"Searching for \"{searchTerm}\""); Console.WriteLine($"================================================================================"); var result = module.DoSearchOfUserSync(iris_saki, null, null, false); Console.WriteLine($"================================================================================"); Console.WriteLine($"Found {result.Count} tweets"); Console.WriteLine($"================================================================================"); foreach (var tweet in result) { //Console.WriteLine($"Tweet by {tweet.User.Name}: " + // $"{tweet.Text}. " + // $"{((tweet.Entities.MediaEntities.Any()) ? ("Contains an image.") : (""))}"); //Console.WriteLine($"================================================================================"); string message = ""; //message += "============================================================\n"; message += "====================Auto Translate Test=====================\n"; //message += $"{((tweet.Entities.MediaEntities.Any()) ? ("This tweet contains an image.\n") : (""))}"; message += $"{((tweet.Text?.Contains("ネットサイン会") ?? false) || (tweet.FullText?.ToUpper().Contains("NETSIGN") ?? false) ? "**NETSIGN ALERT**\n" : "")}"; message += $"{((tweet.Text?.Contains("抽選") ?? false) || (tweet.FullText?.Contains("抽選") ?? false) ? "**The tweet mentions a lottery**\n" : "")}"; //message += $"-The following was Auto-Translated by Google API-\n"; foreach (string ling in tweet.Text.Split("\n")) { message += $"> {ling}\n"; } message += $"\n"; message += $"Link: https://twitter.com/{tweet.User.ScreenNameResponse}/status/{tweet.StatusID}\n"; //message += $"Tweeted at {tweet.CreatedAt} (Local to User)\n";//(Service is {TimeZoneInfo.Local.DisplayName}) message += $"============================================================"; DiscordCommunications.Module.MessageOptions messageParams = new DiscordCommunications.Module.MessageOptions() { WebHookURL = "https://discordapp.com/api/webhooks/687875775084626059/tjVuIQqf4k_rIP5KXmE7dbuYWIJs7SdFPIgZ26O1YgCPocwY0NFYGPdBDAQsqYk7te7M", Text = message, Username = tweet.User.Name, AvatarUrl = tweet.User.ProfileImageUrl }; //DiscordCommunications.Module.SendMessage(messageParams); } Console.Write("Any key to end"); var name = Console.ReadLine(); }
public void RunTest() { Console.OutputEncoding = System.Text.Encoding.UTF8; Console.WriteLine("Starting Test"); TwitterCommunications.Module module = new TwitterCommunications.Module(); Console.WriteLine("Module Created"); Task <Dictionary <string, List <RateLimits> > > task2 = Task.Run(async() => await module.GetRateLimitStatusAsync()); task2.Wait(); var result2 = task2.Result; var rateLimitEntitySearch = result2.Where(entry => entry.Key.Equals("search")).FirstOrDefault().Value.FirstOrDefault(); var rateLimitEntityUserSearch = result2.Where(entry => entry.Key.Equals("statuses")).FirstOrDefault().Value.Where(value => value.Resource.Equals("/statuses/user_timeline")).FirstOrDefault(); Console.WriteLine($"The application has {rateLimitEntitySearch.Remaining} searches left out of {rateLimitEntitySearch.Limit}: {((decimal)rateLimitEntitySearch.Remaining / (decimal)rateLimitEntitySearch.Limit) * 100}% searches available."); Console.WriteLine($"The application has {rateLimitEntityUserSearch.Remaining} searches left out of {rateLimitEntityUserSearch.Limit}: {((decimal)rateLimitEntityUserSearch.Remaining / (decimal)rateLimitEntityUserSearch.Limit) * 100}% timeline searches available."); if (rateLimitEntitySearch.Remaining > 0 && rateLimitEntityUserSearch.Remaining > 0) { ulong iris_Official = 779723154; ulong iris_miyu = 2384783184; ulong iris_saki = 2384780834; string searchTerm = "ネットサイン会"; Console.WriteLine($"Searching for \"{searchTerm}\""); Console.WriteLine($"================================================================================"); Task <List <Status> > task = Task.Run <List <Status> >(async() => await module.DoSearchOfUserAsync(iris_saki, null, null, false)); task.Wait(); var result = task.Result; Console.WriteLine($"================================================================================"); Console.WriteLine($"Found {result.Count} tweets"); Console.WriteLine($"================================================================================"); foreach (var tweet in result) { //Console.WriteLine($"Tweet by {tweet.User.Name}: " + // $"{tweet.Text}. " + // $"{((tweet.Entities.MediaEntities.Any()) ? ("Contains an image.") : (""))}"); //Console.WriteLine($"================================================================================"); string message = ""; //message += "============================================================\n"; message += "====================Auto Translate Test=====================\n"; //message += $"{((tweet.Entities.MediaEntities.Any()) ? ("This tweet contains an image.\n") : (""))}"; message += $"{((tweet.Text?.Contains("ネットサイン会") ?? false) || (tweet.FullText?.ToUpper().Contains("NETSIGN") ?? false) ? "**NETSIGN ALERT**\n" : "")}"; message += $"{((tweet.Text?.Contains("抽選") ?? false) || (tweet.FullText?.Contains("抽選") ?? false) ? "**The tweet mentions a lottery**\n" : "")}"; //message += $"-The following was Auto-Translated by Google API-\n"; foreach (string ling in tweet.Text.Split("\n")) { message += $"> {ling}\n"; } message += $"\n"; message += $"Link: https://twitter.com/{tweet.User.ScreenNameResponse}/status/{tweet.StatusID}\n"; //message += $"Tweeted at {tweet.CreatedAt} (Local to User)\n";//(Service is {TimeZoneInfo.Local.DisplayName}) message += $"============================================================"; DiscordCommunications.Module.MessageOptions messageParams = new DiscordCommunications.Module.MessageOptions() { WebHookURL = "https://discordapp.com/api/webhooks/687875775084626059/tjVuIQqf4k_rIP5KXmE7dbuYWIJs7SdFPIgZ26O1YgCPocwY0NFYGPdBDAQsqYk7te7M", Text = message, Username = tweet.User.Name, AvatarUrl = tweet.User.ProfileImageUrl }; //DiscordCommunications.Module.SendMessage(messageParams); } } Console.Write("Any key to end"); var name = Console.ReadLine(); }