Commit ed952c6f authored by Murukesh Mohanan's avatar Murukesh Mohanan

bug fixes

parent 98ea20ab
...@@ -13,7 +13,7 @@ from bs4 import BeautifulSoup ...@@ -13,7 +13,7 @@ from bs4 import BeautifulSoup
import rssemailconfig import rssemailconfig
feed = {'file': 'feed.pickle'} feed = {'file': 'feed.pickle'}
mail = {'from': 'Murukesh Mohanan <murukesh@cse.iitb.ac.in>'} mail = {'from': 'Murukesh Mohanan <murukesh@cse.iitb.ac.in>', 'title': ''}
smtp = {} smtp = {}
old_entries = set() old_entries = set()
...@@ -27,8 +27,9 @@ def get_new_posts (): ...@@ -27,8 +27,9 @@ def get_new_posts ():
post_ids = [post.id for post in current_entries] post_ids = [post.id for post in current_entries]
try: try:
old_entries = pickle.load (open(feed['file'], 'br')) old_entries = pickle.load (open(feed['file'], 'rb'))
except (EOFError, FileNotFoundError): except (EOFError, FileNotFoundError):
print ("No old posts.\n")
old_entries = set() old_entries = set()
new_post_ids = set (id for id in post_ids if id not in old_entries) new_post_ids = set (id for id in post_ids if id not in old_entries)
...@@ -37,33 +38,34 @@ def get_new_posts (): ...@@ -37,33 +38,34 @@ def get_new_posts ():
def convert_post_to_email (post): def convert_post_to_email (post):
msg = MIMEMultipart('alternative') msg = MIMEMultipart('alternative')
msg['Subject'] = mail['prefix'] + post.title msg['Subject'] = ': '.join([mail['prefix'], post.title])
msg['From'] = mail['from'] msg['From'] = mail['from']
msg['To'] = mail['to'] msg['To'] = mail['to']
part1 = MIMEText(BeautifulSoup(post['summary']).get_text(), 'plain') part1 = MIMEText(BeautifulSoup(post['summary']).get_text(), 'plain')
html_email = post['summary'] + '<br><hr><a target="_blank" href="' + post['id'] + '">' + post['id'] + '</a>' html_email = '{:s}<br><hr><a,target="_blank",href="{:s}">{:s}</a>'.format (post['summary'], post['id'], post['id'])
part2 = MIMEText(html_email, 'html') part2 = MIMEText(html_email, 'html')
msg.attach(part1) msg.attach(part1)
msg.attach(part2) msg.attach(part2)
return msg return msg
def mail_feed (feed): def mail_posts (posts):
smtp = SMTP(smtp_url) smtp_server = SMTP(smtp['url'])
smtp.starttls() smtp_server.starttls()
smtp.login(*smtp_creds) smtp_server.login(*smtp['creds'])
bad_posts = set() bad_posts = set()
for post in feed: for post in posts:
msg = convert_post_to_email (post) msg = convert_post_to_email (post)
try: try:
smtp.send_message(msg) smtp_server.send_message(msg)
except: except:
bad_posts.add(post.id) bad_posts.add(post.id)
return bad_posts return bad_posts
def save_sent_posts (feed, bad_posts): def save_sent_posts (new_posts, bad_posts):
old_entries.update(post.id for post in feed if post.id not in bad_posts) old_entries.update (post.id for post in new_posts if post.id not in bad_posts)
with open (feed_file, 'bw') as f: with open (feed['file'], 'wb') as f:
pickle.dump (old_entries, f) pickle.dump (old_entries, f)
print (old_entries)
return return
if __name__ == "__main__": if __name__ == "__main__":
...@@ -71,5 +73,5 @@ if __name__ == "__main__": ...@@ -71,5 +73,5 @@ if __name__ == "__main__":
if len(new_posts) == 0: if len(new_posts) == 0:
print ("No new posts.") print ("No new posts.")
sys.exit(0) sys.exit(0)
bad_posts = mail_feed (new_posts) bad_posts = mail_posts (new_posts)
save_sent_posts (new_posts, bad_posts) save_sent_posts (new_posts, bad_posts)
...@@ -14,4 +14,4 @@ smtp['url'] = 'smtp-auth.iitb.ac.in' ...@@ -14,4 +14,4 @@ smtp['url'] = 'smtp-auth.iitb.ac.in'
smtp['creds'] = ['murukesh', 'password'] smtp['creds'] = ['murukesh', 'password']
mail['to'] = 'murukesh.mohanan@gmail.com' mail['to'] = 'murukesh.mohanan@gmail.com'
mail['prefix'] = 'IITB Placement Blog'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment