-
Notifications
You must be signed in to change notification settings - Fork 0
/
phoneGIT.py
65 lines (55 loc) · 2.01 KB
/
phoneGIT.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
import datetime
import android, time
import pprint
import json
import requests
import sys
import ast
emails = '[email protected]'
payload = 'payload from config'
postURL = 'http://IPAddress:Port/sms_received/'
getURL = 'http://IPAddress:Port/sms_to_send/'
print "initiating droid"
droid = android.Android()
# pass list of messages
# return messageDict in json
def create_Message_Dict(messages):
messageDict = {}
messageString = str(messages)
messageDict["messages"] = messageString
messageDict["auth"] = str(payload)
print messageDict
return messageDict
print "checking existing records"
# print str(SMS.select().count()) + " messages saved so far."
print "entering loop..."
while True:
try:
print >> sys.stderr, "within try"
messages = droid.smsGetMessages(True).result
if messages:
print >> sys.stderr, "within if"
droid.smsMarkMessageRead(droid.smsGetMessageIds(True).result,True) # mark those messages as read
print str(len(messages)) +" new sms messages!"
messageDict = create_Message_Dict(messages) # create message dict
r = requests.post(postURL, params=messageDict) # pack url
else:
print 'no new messages!'
r = requests.get(getURL)
message = json.loads(r.text)
print >> sys.stderr, message
listStr = message['messages']
messageList = ast.literal_eval(listStr)
print >> sys.stderr, messageList
if messageList:
print >> sys.stderr, 'within messageList for'
for message in messageList:
print message
number = '+' + str(message[0])
droid.smsSend(str(number),str(message[1]))
else:
print 'no outbox messages!'
except:
print >> sys.stderr, "within except"
# droid.sendEmail(emails, 'An exception has Occured', str(sys.exc_type) + '[' + str(sys.exc_value) + ']')
time.sleep(15)