-
Notifications
You must be signed in to change notification settings - Fork 0
/
redminereader.py
72 lines (63 loc) · 2.33 KB
/
redminereader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/python
#
# redminereader.py -- by nathan (period) dotz (at) gmail (period) com
#
# redmine reader isn't actually specific to redmine, but it is specific to
# awesome. redmine reader's job is to take an ATOM feed generated by redmine
# and clear out all the crap they usually display with (poorly!), then package
# that up nicely and shoot it out to an email address.
#
# there's a config file you can include. but it has passwords in it, so you'll
# have to make it yourself
#
import feedparser, re, smtplib, unicodedata
class PostTracker:
mostrecent = 1
onlaunch = 1
def set(num):
mostrecent = num
def strip_tags(value):
return re.sub(r'<[^>]*?>', '', value)
def send_mail(msg):
from config import username,password,fromad,fromname,toad,subject
fullmsg = "From: "+fromname+" <" +fromad+ ">\nTo: "+toad+\
"\nSubject: "+subject+"\n\n\n"+ msg
# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.set_debuglevel(1)
server.starttls()
server.login(username,password)
server.sendmail(fromad, toad, unicodedata.normalize('NFKD', fullmsg).encode('ascii','ignore'))
server.quit()
def read(url):
feedObj = feedparser.parse(url)
msgbody = {}
for i in feedObj['entries']:
x = feedparser.time.mktime( i['updated_parsed'] )
msgbody[x] = i['title'] + ' -- ' + i['id'] + \
"\nFrom: " + i['author'] + "\n"+ '-'*72 +"\n" \
+ re.sub(r"(\s)+", r"\1", strip_tags(i['subtitle'])) + "\n\n"
return msgbody
def prune(entrydict):
k = entrydict.keys()
k.sort()
for key in k:
if key > PostTracker.onlaunch:
#print key, 'is greater than',PostTracker.onlaunch
PostTracker.mostrecent = key
yield entrydict[key]
if __name__ == '__main__':
from config import feedurl
import os
readercountFile = os.path.expanduser("~")+'/.redminereader'
try:
handle = open(readercountFile,'r')
PostTracker.onlaunch = handle.read()
PostTracker.onlaunch = float(PostTracker.onlaunch)
except IOError:
open(readercountFile,'w').write(PostTracker.onlaunch)
r = read(feedurl)
msg = "\n".join([i for i in prune(r)])
send_mail(msg)
print 'Up to timesramp: ',PostTracker.mostrecent
open(readercountFile,'w').write(str( PostTracker.mostrecent ))