Exemple #1
0
        public ActionHandler(Guid actionGUID)
        {
            //Get Action Entity
            var action = _TBSEntities.Action.FirstOrDefault(a => a.PrimaryKey == actionGUID);

            _Action = action;

            _worker = new BackgroundWorker()
            {
                WorkerSupportsCancellation = true,
                WorkerReportsProgress      = true
            };

            _CommsModule = new TwitterCommunications.Module();

            _worker.DoWork             += Worker_DoWork;
            _worker.ProgressChanged    += worker_ProgressChanged;
            _worker.RunWorkerCompleted += worker_RunWorkerCompleted;

            _timer          = new System.Timers.Timer(_Action.PriorityLevel.Delay);
            _timer.Elapsed += Timer_Elapsed;
            _timer.Start();
            WriteLog(Log.LogLevels.Information, $"ActionHandler-{actionGUID}", "ActionHandler Started", $"ActionHandler for action {action.Description} started at {DateTime.UtcNow}");
        }
Exemple #2
0
        protected override void Seed(TBSContext context)
        {
            context.PriorityLevel.Add(Constants.PriorityLevel.ONCEPER12HOURS);
            context.PriorityLevel.Add(Constants.PriorityLevel.ONCEPER15MINUTES);
            context.PriorityLevel.Add(Constants.PriorityLevel.ONCEPER15SECONDS);
            context.PriorityLevel.Add(Constants.PriorityLevel.ONCEPER1HOUR);
            context.PriorityLevel.Add(Constants.PriorityLevel.ONCEPER1MINUTE);
            context.PriorityLevel.Add(Constants.PriorityLevel.ONCEPER2HOURS);
            context.PriorityLevel.Add(Constants.PriorityLevel.ONCEPER2MINUTES);
            context.PriorityLevel.Add(Constants.PriorityLevel.ONCEPER30MINUTES);
            context.PriorityLevel.Add(Constants.PriorityLevel.ONCEPER30SECONDS);
            context.PriorityLevel.Add(Constants.PriorityLevel.ONCEPER3HOURS);
            context.PriorityLevel.Add(Constants.PriorityLevel.ONCEPER3MINUTES);
            context.PriorityLevel.Add(Constants.PriorityLevel.ONCEPER5MINUTES);
            context.PriorityLevel.Add(Constants.PriorityLevel.ONCEPER6HOURS);
            context.PriorityLevel.Add(Constants.PriorityLevel.ONCEPERDAY);

            IList <DestinationType> defaultDestinationType = new List <DestinationType>();

            defaultDestinationType.Add(new DestinationType()
            {
                Description = "Discord"
            });
            context.DestinationType.AddRange(defaultDestinationType);

            Models.Action action = new Models.Action()
            {
                PrimaryKey      = Guid.NewGuid(),
                Description     = "Check latest from @iRis_Official_",
                LastTimeChecked = Convert.ToDateTime("2020-03-13 04:39:37.013"),
                LastFoundPostID = "1238315309997502466",
                LastModified    = DateTime.UtcNow,
                Active          = false,
                PriorityLevel   = Constants.PriorityLevel.ONCEPER30SECONDS,
                Destinations    = new List <Destination>()
                {
                    new Destination()
                    {
                        DestinationType = defaultDestinationType.FirstOrDefault(),
                        Description     = "TestURL - Hue's Server #test",
                        Webhook         = "https://discordapp.com/api/webhooks/685263828027441266/eR05hV78N22tcZrvHDqqQ6DwhNvxodSOH6NgpEKcD2SWVXjJmN_c3U9yFtI51FfrRv6I"
                    }
                },
                Queries = new List <Query>()
                {
                    new Query()
                    {
                        //QueryType = Constants.QueryType.TIMELINESEARCH,
                        ShouldTranslate = true,
                        TargetedUsers   = new List <QueryTargetUser>()
                        {
                            new QueryTargetUser()
                            {
                                User = new User()
                                {
                                    UserID   = "779723154",
                                    UserName = "******"
                                }
                            }
                        }
                    }
                }
            };
            context.Action.Add(action);

            context.SaveChanges();
        }
Exemple #3
0
        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;
            }
        }