This repository was archived by the owner on Jan 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgvnotifier.py
More file actions
executable file
·120 lines (85 loc) · 3.61 KB
/
Copy pathgvnotifier.py
File metadata and controls
executable file
·120 lines (85 loc) · 3.61 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python
__appname__ = 'GVNotifier'
__version__ = '0.2'
# Constants
CFG_FILE = 'settings.cfg'
PWD_FILE = 'passwords.cfg'
import sys, time
import googlevoice
from lxml import etree
from modules import config, smtp
def main():
cfg = config.Config(CFG_FILE, PWD_FILE)
for section in 'google', 'smtp':
cfg.read_password(section)
mailer = smtp.Mailer(cfg)
g = cfg['google']
gv = googlevoice.Voice()
gv.login(g['username'], g['password'])
# Variable to hold notifications sent
notifications = {}
# lxml parsing objects
parser = etree.HTMLParser()
xp_group_div = etree.XPath('//div[@id=$id]')
xp_group_rows = etree.XPath('.//div[@class="gc-message-sms-row"]')
xp_msg_from = etree.XPath('.//span[@class="gc-message-sms-from"]')
xp_msg_text = etree.XPath('.//span[@class="gc-message-sms-text"]')
xp_msg_time = etree.XPath('.//span[@class="gc-message-sms-time"]')
# Mark all current message group ids as notified
tree = etree.fromstring(gv.inbox_html(), parser)
for id, group in gv.inbox()['messages'].iteritems():
if group['isRead'] == False and 'sms' in group['labels']:
group_div = xp_group_div(tree, id = id)[0]
rows = xp_group_rows(group_div)
notifications[id] = len(rows)
print 'Current unread message groups: %s' % len(notifications)
print 'Starting loop.'
while True:
# Why waste cycles unnecessarily?
if gv.inbox()['unreadCounts']['sms'] == 0:
time.sleep(60)
continue
tree = etree.fromstring(gv.inbox_html(), parser)
for id, group in gv.inbox()['messages'].iteritems():
if group['isRead'] == False and 'sms' in group['labels']:
group_div = group_div = xp_group_div(tree, id = id)[0]
rows = xp_group_rows(group_div)
rows_count = len(rows)
if not id in notifications:
notifications[id] = 0
# New un-notified rows in group
if notifications[id] != rows_count:
# Save count for next loop
notifications[id] = rows_count
senders = xp_msg_from(group_div)
messages = xp_msg_text(group_div)
times = xp_msg_time(group_div)
number = group['phoneNumber'].replace('+1', '', 1)
sender = senders[rows_count - 1].text.strip()[:-1]
subject = '%s - %s' % (sender, number)
print 'New SMS message from: %s' % number
# Create email message to send
body = ''
for index in reversed(range(rows_count)):
body += senders[index].text.strip() + ' '
body += messages[index].text.strip() + ' '
body += '(' + times[index].text.strip() + ')'
body += '\n'
mailer.send_email(subject, body)
time.sleep(60)
if __name__ == '__main__':
from httplib import IncompleteRead, BadStatusLine
while True:
try:
print '%s v%s - Starting up.' % (__appname__, __version__)
main()
except KeyboardInterrupt:
print 'Process cancelled. Exiting.'
time.sleep(2)
sys.exit(0)
except (IncompleteRead, BadStatusLine):
print 'Timeout while reading data from Google Voice. Restarting.'
time.sleep(2)
except:
print 'Unexpected error. Exiting.'
raise