Commit 28efa45b authored by Murukesh Mohanan's avatar Murukesh Mohanan

initial commit

parents
rssemailconfig.py
*.pyc
*.pyo
#! /usr/bin/env python3
import os
import sys
import subprocess
import feedparser
import pickle
from smtplib import SMTP
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from bs4 import BeautifulSoup
feed = {'file': 'feed.pickle'}
mail = {'from': 'Murukesh Mohanan <murukesh@cse.iitb.ac.in>'}
smtp = {}
old_entries = set()
from rssemailconfig import *
def get_new_posts ():
current_entries = feedparser.parse(subprocess.Popen(['curl', '-u', ':'.join(feed['creds']), feed['url']], stdout=subprocess.PIPE).communicate()[0]).entries
post_ids = [post.id for post in current_entries]
try:
old_entries = pickle.load (open(feed['file'], 'br'))
except (EOFError, FileNotFoundError):
old_entries = set()
new_post_ids = set (id for id in post_ids if id not in old_entries)
new_posts = [post for post in current_entries if post.id in new_post_ids]
return new_posts
def convert_post_to_email (post):
msg = MIMEMultipart('alternative')
msg['Subject'] = mail['prefix'] + post.title
msg['From'] = mail['from']
msg['To'] = mail['to']
part1 = MIMEText(BeautifulSoup(post['summary']).get_text(), 'plain')
html_email = post['summary'] + '<br><hr><a target="_blank" href="' + post['id'] + '">' + post['id'] + '</a>'
part2 = MIMEText(html_email, 'html')
msg.attach(part1)
msg.attach(part2)
return msg
def mail_feed (feed):
smtp = SMTP(smtp_url)
smtp.starttls()
smtp.login(*smtp_creds)
bad_posts = set()
for post in feed:
msg = convert_post_to_email (post)
try:
smtp.send_message(msg)
except:
bad_posts.add(post.id)
return bad_posts
def save_sent_posts (feed, bad_posts):
old_entries.update(post.id for post in feed if post.id not in bad_posts)
with open (feed_file, 'bw') as f:
pickle.dump (old_entries, f)
return
if __name__ == "__main__":
new_posts = get_new_posts ()
if len(new_posts) == 0:
print ("No new posts.")
sys.exit(0)
bad_posts = mail_feed (new_posts)
save_sent_posts (new_posts, bad_posts)
feed['url'] = 'http://placements.iitb.ac.in/blog/?feed=rss2'
feed['name'] = 'IITB Placement Blog'
feed['creds'] = ['murukesh', 'password']
feed['file'] = 'feed.pickle'
smtp['url'] = 'smtp-auth.iitb.ac.in'
smtp['creds'] = ['murukesh', 'password']
mail['to'] = 'murukesh.mohanan@gmail.com'
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