forked from vinod85/pygmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getwords.py
executable file
·80 lines (67 loc) · 2.14 KB
/
getwords.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
73
74
75
76
77
78
79
80
#!/usr/bin/env python
import pygmail
import ConfigParser
import re
import datetime
CONFIG_FILE = 'gmail.cfg'
WORDS_FILE = 'words.txt'
sources = ['mw', 'dictionary', 'oxford', 'wordsmith']
emails = {'mw': 'word@m-w.com', 'dictionary': 'doctor@dictionary.com', 'oxford': 'wordoftheday.odo_us@oup.com', 'wordsmith': 'wsmith@wordsmith.org'}
def init():
"""create gmail object and login"""
config = ConfigParser.ConfigParser()
config.read(CONFIG_FILE)
username = config.get('gmail', 'username')
password = config.get('gmail', 'password')
gmail = pygmail.pygmail()
gmail.login(username, password)
return gmail
def get_word(gmail, source):
email = emails[source]
try:
mail_id = gmail.get_mails_from(email)[-1] # most recent message
except IndexError: # no mail from this source
return ''
function_for_source = eval('get_word_%s' % source)
word = function_for_source(gmail, mail_id)
return word
def get_word_mw(gmail, mail_id):
subject = gmail.get_mail_subject_from_id(mail_id)
m = re.match("Subject: ([\w '\-]+) - ", subject)
if m:
return m.groups(1)[0]
else:
return ''
def get_word_dictionary(gmail, mail_id):
subject = gmail.get_mail_subject_from_id(mail_id)
m = re.match("Subject: ([\w '\-]+):", subject)
if m:
return m.groups(1)[0]
else:
return ''
def get_word_oxford(gmail, mail_id):
body = gmail.get_mail_body_from_id(mail_id)
m = re.search("Your word for today is: <a[^>]*>([\w '\-]+)</a>", body)
if m:
return m.groups(1)[0]
else:
return ''
def get_word_wordsmith(gmail, mail_id):
subject = gmail.get_mail_subject_from_id(mail_id)
m = re.match("Subject: A.Word.A.Day--([\w '\-]+)", subject)
if m:
return m.groups(1)[0]
else:
return ''
def save_words(words):
date = str(datetime.date.today())
output = '%s,%s' % (date, ','.join(words))
print output
with open(WORDS_FILE, 'a') as f:
print >> f, output
def main():
gmail = init()
words = [get_word(gmail, source) for source in sources]
save_words(words)
if __name__ == '__main__':
main()