Skip to content

Instantly share code, notes, and snippets.

@bhpayne
Created December 27, 2020 00:41
Show Gist options
  • Save bhpayne/ef48e5bf8519596593cffc81ce4341a1 to your computer and use it in GitHub Desktop.
Save bhpayne/ef48e5bf8519596593cffc81ce4341a1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""
runs via crontab on a DigitalOcean VPS
the sendgrid API key is in a ".env" file
"""
# how to run this script:
# python3 hacker_news_rss_email_alerts.py
import xmltodict
import datetime
import json
import requests
import os
#import smtplib
with open('.env','r') as fil:
api_key = fil.read().strip()
#print(api_key)
r = requests.get('https://news.ycombinator.com/rss')
rss_as_dict = xmltodict.parse(r.text)
found_content = False
str_to_send = "search of HNews RSS feed: \n\n"
for this_dict in rss_as_dict['rss']['channel']['item']:
for k,v in this_dict.items():
if ((("keyword1" in v.lower()) and ("keyword2" in v.lower())) or
(("keyword1" in v.lower()) and ("keyword3" in v.lower()))):
str_to_send+=str(this_dict)+"\n"
found_content = True
# DigitalOcean blocks port 25, so this won't work:
# # http://effbot.org/pyfaq/how-do-i-send-mail-from-a-python-script.htm
#server = smtplib.SMTP("localhost")
#server.sendmail("ben", ["ben@gmail.com"], "hello")
# I set up a SendMail account, so this works:
# curl --request POST --url https://api.sendgrid.com/v3/mail/send --header "Authorization: Bearer $SENDGRID_API_KEY" --header 'Content-Type: application/json' --data '{"personalizations": [{"to": [{"email": "ben@gmail.com"}]}],"from": {"email": "ben@gmail.com"},"subject": "Sending with SendGrid is Fun","content": [{"type": "text/plain", "value": "and easy to do anywhere, even with cURL"}]}'
# using the site https://curl.trillworks.com/#python
# I translated the above command into
headers = {
# 'Authorization': 'Bearer ' + os.environ.get('SENDGRID_API_KEY'),
'Authorization': 'Bearer ' + api_key,
'Content-Type': 'application/json',
}
#print(headers)
if found_content:
# str_to_send = "no matches to existing filters for today"
subject = "keyword match for HNews on "
else:
subject = "no HNews matches to report on "
data_dict = {"personalizations":
[{"to": [{"email": "ben@gmail.com"}],
"from": {"email": "ben@gmail.com"},
"subject": subject + str(datetime.date.today()),
"content": [{"type": "text/plain", "value": str_to_send}]}]}
# send the Email as a POST
data_dict_no_email = {"personalizations":
[{"to": [{"email": "ben@gmail.com"}]}],
"from": {"email": "ben@gmail.com"},
"subject": subject + str(datetime.date.today()),
"content": [{"type": "text/plain", "value": str_to_send}]}
if found_content:
response = requests.post('https://api.sendgrid.com/v3/mail/send',
headers=headers,
data=json.dumps(data_dict))
# if the POST to Sendmail is successful, nothing is produced
else:
response = requests.post('https://api.sendgrid.com/v3/mail/send',
headers=headers,
data=json.dumps(data_dict_no_email))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment