Exemplo n.º 1
0
        public static async Task Run([TimerTrigger("00 */10 * * * *")] TimerInfo myTimer, ILogger log)
        {
            if (!_hasRunInit)
            {
                Init();
            }


            CloudTable oauthTable = CloudStorageAccount
                                    .Parse(_storage)
                                    .CreateCloudTableClient()
                                    .GetTableReference("oauth");

            if (checkTableExists)
            {
                oauthTable.CreateIfNotExists();
            }

            CloudTable articleTable = CloudStorageAccount
                                      .Parse(_storage)
                                      .CreateCloudTableClient()
                                      .GetTableReference("article");

            if (checkTableExists)
            {
                articleTable.CreateIfNotExists();
                checkTableExists = true;
            }

            RedditOAuth result = (RedditOAuth)oauthTable
                                 .Execute(
                TableOperation.Retrieve <RedditOAuth>("reddit", _user)
                ).Result;

            //https://blog.maartenballiauw.be/post/2012/10/08/what-partitionkey-and-rowkey-are-for-in-windows-azure-table-storage.html
            //https://www.red-gate.com/simple-talk/cloud/cloud-data/an-introduction-to-windows-azure-table-storage/

            if (result?.GetNewToken < DateTimeOffset.Now)
            {
                result = null;
                log.LogInformation("need a new token");
            }

            Reddit      r                = null;
            BotWebAgent agent            = null;
            bool        saveToken        = false;
            bool        tryLogin         = false;
            int         tryLoginAttempts = 2;

            do
            {
                tryLoginAttempts--;
                tryLogin = false;
                if (result == null)
                {
                    agent  = new BotWebAgent(_user, _pass, _clientId, _secret, "https://www.reddit.com/user/somekindofbot0000/");
                    result = new RedditOAuth()
                    {
                        Token = agent.AccessToken, GetNewToken = DateTimeOffset.Now.AddMinutes(57), PartitionKey = "reddit", RowKey = _user
                    };
                    r         = new Reddit(agent, true);
                    saveToken = true;
                }
                else
                {
                    try
                    {
                        r = new Reddit(result.Token);
                    }
                    catch (AuthenticationException a)
                    {
                        result   = null;
                        tryLogin = true;
                    }
                    catch (WebException w)
                    {
                        if (w.Status == WebExceptionStatus.ProtocolError &&
                            (w.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.Unauthorized)
                        {
                            result   = null;
                            tryLogin = true;
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            } while (tryLogin && tryLoginAttempts > 0);

            if (r == null)
            {
                throw new Exception("couldn't get logged in");
            }

            if (saveToken)
            {
                oauthTable
                .Execute(
                    TableOperation.InsertOrReplace(result));
                log.LogInformation("saving token");
            }

            NewsBankClient newsBankClient = new NewsBankClient(
                new EnvironmentVariableEZProxySignInUriProvider(),
                EditForNewsbank._credProvider,
                new EnvironmentVariableProductBaseUriProvider(),
                new BasicCanLog(log));



            Task <HttpResponseMessage> checkMailTask = CheckMail(r, log, client);

            //https://www.reddit.com/r/SeattleWA/search?q=%28+site%3Aseattletimes.com+Subreddit%3ASeattleWA+%29&sort=new&t=day
            Listing <Post> posts =
                r.AdvancedSearch(x => x.Subreddit == "SeattleWA" &&
                                 x.Site == "seattletimes.com"
                                 , Sorting.New, TimeSorting.Day);

            bool allPostsSuccess = true;

            using (WaybackClient waybackMachine = new WaybackClient())
            {
                foreach (Post p in posts.TakeWhile(x => !x.IsHidden))
                {
                    allPostsSuccess &= await ProcessPost(p, waybackMachine, log, articleTable, newsBankClient);
                }
            }
            if (checkMailTask.Status < TaskStatus.RanToCompletion)
            {
                log.LogInformation("waiting for checkmail");
                await checkMailTask;
            }
            else
            {
                log.LogInformation(checkMailTask.Status.ToString());
            }


            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            if (!allPostsSuccess)
            {
                throw new ApplicationException("Not all Posts were processed successfully");
            }
        }
Exemplo n.º 2
0
        public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "CheckBotMail/name/{name}")] HttpRequestMessage req, string name, ILogger log)
        {
            string storage = Environment.GetEnvironmentVariable("AzureWebJobsStorage");

            string user     = name;
            string pass     = Environment.GetEnvironmentVariable("BotPass");
            string secret   = Environment.GetEnvironmentVariable("BotSecret");
            string clientId = Environment.GetEnvironmentVariable("botClientId");

            CloudTable oauthTable = CloudStorageAccount
                                    .Parse(storage)
                                    .CreateCloudTableClient()
                                    .GetTableReference("oauth");

            RedditOAuth result = (RedditOAuth)oauthTable
                                 .Execute(
                TableOperation.Retrieve <RedditOAuth>("reddit", user)
                ).Result;


            Reddit      r                = null;
            BotWebAgent agent            = null;
            bool        tryLogin         = false;
            int         tryLoginAttempts = 2;

            do
            {
                tryLoginAttempts--;
                tryLogin = false;
                if (result == null)
                {
                    agent  = new BotWebAgent(user, pass, clientId, secret, "https://www.reddit.com/user/somekindofbot0000/");
                    result = new RedditOAuth()
                    {
                        Token = agent.AccessToken, GetNewToken = DateTimeOffset.Now.AddMinutes(57), PartitionKey = "reddit", RowKey = user
                    };
                    r = new Reddit(agent, true);
                }
                else
                {
                    try
                    {
                        //agent = new BotWebAgent(result.RefreshToken, clientId, secret, "https://www.reddit.com/user/somekindofbot0000/");
                        r = new Reddit(result.Token);
                    }
                    catch (AuthenticationException)
                    {
                        result   = null;
                        tryLogin = true;
                    }
                    catch (WebException w)
                    {
                        if (w.Status == WebExceptionStatus.ProtocolError &&
                            (w.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.Unauthorized)
                        {
                            result   = null;
                            tryLogin = true;
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            } while (tryLogin && tryLoginAttempts > 0);


            if (r == null)
            {
                throw new Exception("couldn't get logged in");
            }

            //This save might be affecting the archive function
            //oauthTable
            //    .Execute(
            //        TableOperation.InsertOrReplace(result));


            foreach (Thing t in r.User.UnreadMessages.Where(x => x is Comment))
            {
                Comment c                = t as Comment;
                string  cleanBody        = c.Body.ToLower().Trim().Replace(".", "");
                string  replyMsg         = null;
                bool    isUnknownMessage = false;
                if (string.Equals(cleanBody, "good bot"))
                {
                    replyMsg = GoodBotReplyMsg;
                }
                else if (string.Equals(cleanBody, "bad bot"))
                {
                    replyMsg = BadBotReplyMsg;
                }
                else
                {
                    isUnknownMessage = true;
                    replyMsg         = $"unknown reply: {c.Body}";
                }



                ReplyAction(replyMsg, c, log, isUnknownMessage);

                //else
                //{
                //    if (string.Equals(cleanBody, "good bot"))
                //    {
                //        log.Info(replyMsg);
                //    }
                //    else
                //    {
                //        log.Info("unknown reply: " + c.Body);
                //    }
                //}
            }

            log.LogInformation("C# HTTP trigger function processed a request.");

            // Fetching the name from the path parameter in the request URL
            return(req.CreateResponse(HttpStatusCode.OK));
        }
Exemplo n.º 3
0
        public static async Task Run([TimerTrigger("0 0 07 * * *")] TimerInfo myTimer, ILogger log)
        {
            NewsBankClient newsBankClient = new NewsBankClient(
                new EnvironmentVariableEZProxySignInUriProvider(),
                _credProvider,
                new EnvironmentVariableProductBaseUriProvider(),
                new BasicCanLog(log)
                );


            CloudTable articleTable = CloudStorageAccount
                                      .Parse(_storage)
                                      .CreateCloudTableClient()
                                      .GetTableReference("article");

            if (checkTableExists)
            {
                articleTable.CreateIfNotExists();
            }

            DateTime today = new DateTime(DateTime.Today.Ticks, DateTimeKind.Utc);
            TableQuery <ArticlePost> articlesPublishedBeforeToday = articleTable.CreateQuery <ArticlePost>().Where(x => x.ArticleDate <today && x.ArticleDate> today.AddDays(-27)).AsTableQuery();



            CloudTable oauthTable = CloudStorageAccount
                                    .Parse(_storage)
                                    .CreateCloudTableClient()
                                    .GetTableReference("oauth");

            if (checkTableExists)
            {
                checkTableExists = false;
                oauthTable.CreateIfNotExists();
            }



            RedditOAuth result = (RedditOAuth)oauthTable
                                 .Execute(
                TableOperation.Retrieve <RedditOAuth>("reddit", _user)
                ).Result;

            //https://blog.maartenballiauw.be/post/2012/10/08/what-partitionkey-and-rowkey-are-for-in-windows-azure-table-storage.html
            //https://www.red-gate.com/simple-talk/cloud/cloud-data/an-introduction-to-windows-azure-table-storage/

            if (result?.GetNewToken < DateTimeOffset.Now)
            {
                result = null;
                log.LogInformation("need a new token");
            }

            Reddit      r                = null;
            BotWebAgent agent            = null;
            bool        saveToken        = false;
            bool        tryLogin         = false;
            int         tryLoginAttempts = 2;

            do
            {
                tryLoginAttempts--;
                tryLogin = false;
                if (result == null)
                {
                    agent  = new BotWebAgent(_user, _pass, _clientId, _secret, "https://www.reddit.com/user/somekindofbot0000/");
                    result = new RedditOAuth()
                    {
                        Token = agent.AccessToken, GetNewToken = DateTimeOffset.Now.AddMinutes(57), PartitionKey = "reddit", RowKey = _user
                    };
                    r         = new Reddit(agent, true);
                    saveToken = true;
                }
                else
                {
                    try
                    {
                        r = new Reddit(result.Token);
                    }
                    catch (AuthenticationException a)
                    {
                        result   = null;
                        tryLogin = true;
                    }
                    catch (WebException w)
                    {
                        if (w.Status == WebExceptionStatus.ProtocolError &&
                            (w.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.Unauthorized)
                        {
                            result   = null;
                            tryLogin = true;
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            } while (tryLogin && tryLoginAttempts > 0);

            if (r == null)
            {
                throw new Exception("couldn't get logged in");
            }

            if (saveToken)
            {
                oauthTable
                .Execute(
                    TableOperation.InsertOrReplace(result));
                log.LogInformation("saving token");
            }

            List <Task> updateCommentTasks = new List <Task>();

            foreach (ArticlePost ap in articlesPublishedBeforeToday)
            {
                Task updateComment = GetCommentLine(ap, log, newsBankClient)
                                     .ContinueWith(async(commentLine) =>
                {
                    bool retry = false;
                    do
                    {
                        Comment c = r.GetComment(new Uri("https://www.reddit.com" + ap.CommentUri));
                        if (!String.IsNullOrEmpty(commentLine.Result))
                        {
                            EditComment(commentLine.Result, c);
                            articleTable.Execute(TableOperation.Delete(ap));
                            retry = false;
                        }
                        else
                        {
                            log.LogInformation("Empty CommentLine, will check headline.");
                            retry = await TryUpdateArticleData(ap, r, articleTable, log);
                            if (retry)
                            {
                                log.LogInformation($"author: {ap.ArticleAuthor} ---- headline: {ap.ArticleHeadline}");
                            }
                        }
                    } while (retry);
                }
                                                   , TaskContinuationOptions.OnlyOnRanToCompletion).Unwrap();

                updateCommentTasks.Add(updateComment);
            }

            await Task.WhenAll(updateCommentTasks.ToArray());

            log.LogInformation("AwaitWhenALL " + updateCommentTasks.Count.ToString());
        }