Last active
August 29, 2015 14:04
-
-
Save blw9u2012/fc5193d00d488e4e6bea to your computer and use it in GitHub Desktop.
Just a simple Python script showing how easy it is to use feedparser
This file contains hidden or 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
| import feedparser | |
| from email.MIMEMultipart import MIMEMultipart | |
| from email.MIMEText import MIMEText | |
| import smtplib | |
| import re | |
| def getFeeds(rssfeed, regex=None): | |
| feed_dict = {} | |
| for feed in rssfeed.entries: | |
| if(regex is None): | |
| feed_dict[feed.title] = feed.link | |
| else: | |
| match = re.search(regex, feed.title) | |
| if(match): | |
| feed_dict[feed.title] = feed.link | |
| return feed_dict | |
| def generateHtml(feed_dict): | |
| html = """\ | |
| <html> | |
| <head></head> | |
| <body> | |
| <ul> | |
| """ | |
| # Iterate through the dictionary | |
| for key in feed_dict.keys(): | |
| try: | |
| html += "<li><b>{0}</b>: {1}</li>".format(key, feed_dict[key]) | |
| except Exception, e: | |
| pass | |
| html += "</ul></body></html>" | |
| return html | |
| # Configure email server | |
| server = smtplib.SMTP('smtp.gmail.com', 587) | |
| server.ehlo() | |
| server.starttls() | |
| server.ehlo() | |
| server.login("gmail username", "gmail app key") | |
| # Feeds | |
| python_reddit = 'http://www.reddit.com/r/python/.rss' | |
| python_node_reddit = 'http://www.reddit.com/r/node/.rss' | |
| python_slickdeals = 'http://feeds.feedburner.com/SlickdealsnetFP' | |
| python_cowboom = 'http://blog.cowboom.com/feed/rss2/' | |
| python_hackernews = 'https://news.ycombinator.com/rss' | |
| # The rssfeed objects.. | |
| pythonReddit = feedparser.parse(python_reddit) | |
| nodeReddit = feedparser.parse(python_node_reddit) | |
| slickdeals = feedparser.parse(python_slickdeals) | |
| cowboom = feedparser.parse(python_cowboom) | |
| hackernews = feedparser.parse(python_hackernews) | |
| # Get all of the feeds | |
| email_dict = getFeeds(pythonReddit) | |
| email_dict.update(getFeeds(nodeReddit)) | |
| email_dict.update(getFeeds(slickdeals, r'HDTV|Xbox One|PS4|Playstation')) | |
| email_dict.update(getFeeds(cowboom, r'HDTV|Xbox One|PS4|Playstation')) | |
| email_dict.update(getFeeds(hackernews)) | |
| # Create the html | |
| html = generateHtml(email_dict) | |
| # Configure email message... | |
| fromaddr = "youremailaddress@gmail.com" | |
| toaddr = "whomever@gmail.com" | |
| msg = MIMEMultipart() | |
| msg['From'] = fromaddr | |
| msg['To'] = toaddr | |
| msg['Subject'] = "Updates" | |
| # Send the email | |
| msg.attach(MIMEText(html, 'html')) | |
| text = msg.as_string() | |
| server.sendmail(fromaddr, toaddr, text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment