|
#!/usr/bin/python |
|
|
|
import feedparser |
|
import time |
|
import sys |
|
|
|
days_back = 7 |
|
feed_urls = ['http://gbg.hackerspace.se/site/feed', 'http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=gbghackerspace'] |
|
mail_to = '[email protected]' |
|
mail_from = '[email protected]' |
|
mail_subject = 'Senaste %d dagarna' % days_back |
|
|
|
|
|
from smtplib import SMTP |
|
from email.MIMEText import MIMEText |
|
from email.Header import Header |
|
from email.Utils import parseaddr, formataddr |
|
|
|
def send_email(sender, recipient, subject, body, server='localhost'): |
|
"""Send an email. |
|
|
|
All arguments should be Unicode strings (plain ASCII works as well). |
|
|
|
Only the real name part of sender and recipient addresses may contain |
|
non-ASCII characters. |
|
|
|
The email will be properly MIME encoded and delivered though SMTP to |
|
localhost port 25. This is easy to change if you want something different. |
|
|
|
The charset of the email will be the first one out of US-ASCII, ISO-8859-1 |
|
and UTF-8 that can represent all the characters occurring in the email. |
|
""" |
|
|
|
# Header class is smart enough to try US-ASCII, then the charset we |
|
# provide, then fall back to UTF-8. |
|
header_charset = 'ISO-8859-1' |
|
|
|
# We must choose the body charset manually |
|
for body_charset in 'US-ASCII', 'ISO-8859-1', 'UTF-8': |
|
try: |
|
body.encode(body_charset) |
|
except UnicodeError: |
|
pass |
|
else: |
|
break |
|
|
|
# Split real name (which is optional) and email address parts |
|
sender_name, sender_addr = parseaddr(sender) |
|
recipient_name, recipient_addr = parseaddr(recipient) |
|
|
|
# We must always pass Unicode strings to Header, otherwise it will |
|
# use RFC 2047 encoding even on plain ASCII strings. |
|
sender_name = str(Header(unicode(sender_name), header_charset)) |
|
recipient_name = str(Header(unicode(recipient_name), header_charset)) |
|
|
|
# Make sure email addresses do not contain non-ASCII characters |
|
sender_addr = sender_addr.encode('ascii') |
|
recipient_addr = recipient_addr.encode('ascii') |
|
|
|
# Create the message ('plain' stands for Content-Type: text/plain) |
|
msg = MIMEText(body.encode(body_charset), 'plain', body_charset) |
|
msg['From'] = formataddr((sender_name, sender_addr)) |
|
msg['To'] = formataddr((recipient_name, recipient_addr)) |
|
msg['Subject'] = Header(unicode(subject), header_charset) |
|
|
|
# Send the message via SMTP to localhost:25 |
|
smtp = SMTP(server) |
|
smtp.sendmail(sender, recipient, msg.as_string()) |
|
smtp.quit() |
|
|
|
|
|
breakpoint = time.time() - days_back * 60 * 60 * 24 |
|
|
|
feeds = [feedparser.parse(url) for url in feed_urls] |
|
|
|
posts = {} |
|
for feed in feeds: |
|
entries = [e for e in feed.entries if time.mktime(e.updated_parsed) > breakpoint] |
|
if entries: |
|
posts[feed.feed.title] = entries |
|
|
|
if not posts: |
|
sys.exit() |
|
|
|
message = u'' |
|
|
|
for feed_name, entries in posts.iteritems(): |
|
message += u"%s\n" % feed_name |
|
for e in entries: |
|
message += u" * %s, %s\n" % (e.title, e.link) |
|
message += u"\n" |
|
|
|
send_email(sender=mail_from, recipient=[mail_to], subject=mail_subject, body=message, server='127.0.0.1') |