Created
August 3, 2017 21:50
-
-
Save isaacrlevin/a23c9f40b7c2cb4237978450562d92e9 to your computer and use it in GitHub Desktop.
Polling with Reddit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| |
public static class RedditPoller | |
{ | |
[FunctionName("TimerTriggerCSharp")] | |
public static void Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, TraceWriter log) | |
{ | |
string url = "https://www.reddit.com/r/azure/new/.json"; | |
List<Child> children = new List<Child>(); | |
WebClient client = new WebClient(); | |
string text1 = client.DownloadString(url); | |
RedditObject redditObject = JsonConvert.DeserializeObject<RedditObject>(text1); | |
foreach (var post in redditObject.Data.Children) | |
{ | |
if (DateTime.UtcNow.AddMinutes(-1) < post.Data.TimeStampDate) | |
{ | |
children.Add(post); | |
log.Info($"Reddit Post {post.Data.title} found"); | |
} | |
} | |
if (children.Count > 0) | |
{ | |
var apiKey = (ConfigurationManager.AppSettings["SendGridApiKey"]); | |
var sgClient = new SendGridClient(apiKey); | |
var from = new EmailAddress(ConfigurationManager.AppSettings["EmailAddress"]); | |
var subject = "New Reddit Post"; | |
var to = new EmailAddress(ConfigurationManager.AppSettings["EmailAddress"]); | |
var plainTextContent = new StringBuilder(); | |
foreach (var post in children) | |
{ | |
plainTextContent.AppendLine($"<a href='{post.Data.Url}'>{post.Data.title}</a><br />"); | |
} | |
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent.ToString(), plainTextContent.ToString()); | |
sgClient.SendEmailAsync(msg).Wait(); | |
} | |
log.Info($"C# Timer trigger function executed at: {DateTime.Now}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment