-
Notifications
You must be signed in to change notification settings - Fork 6
/
communication.py
executable file
·140 lines (116 loc) · 3.89 KB
/
communication.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python
# for send_to_server
import urllib
# for upload_to_server
from models import *
from util import *
import datetime, time, calendar
import vendor.rfc3339
import urllib2
import time
import uuid
def send_to_server(time, source, message):
"""Useless, because we're already on the server"""
# make a dictionary of search parameters
q = {'source': source,
'message': message,
'time': time}
# encode the http string
query = urllib.urlencode(q)
url="http://smspersonfinder.appspot.com/create?%s" % query
print "Opening %s" % url
resp = urllib.urlopen(url)
html = resp.read()
if html:
print "Success"
print html
else:
print "Failure"
def build_personfinder_url(action, domain, key):
"""
Action is write/note/person
"""
url = "https://%s.googlepersonfinder.appspot.com/api/%s?key=%s" % (domain, action, key)
return url
def split_message_to_fields(message):
"""Message is in format
message: formatted with last#first#status_of_person#description
Returns a dictionary
"""
fields = message.split('#');
d = {'person_last_name': fields[0],
'person_first_name': fields[1],
'note_text': fields[2],
'person_other': fields[3],
}
return d
def convert_datetime_to_rfc3339(dt):
"""Returns RFC 3339 from timestr"""
time_rfc3339 = vendor.rfc3339.datetimetostr(dt)
return time_rfc3339
def parse_formatted_message(timestr, source, message):
"""Parse formatted message
timestr: timestring in the format 2011-05-05 19:30:55
source: typically a 10 digital phone number
message: formatted with last#first#description#note
If note contains alive, dead, missing, seeking, or author,
the status is updated accordingly.
"""
#message = 'Koff#Jonathan#Status#Description'
timestr = str(timestr)
source = str(source)
message = str(message)
fields = split_message_to_fields(message)
namespace = "rhok1.com"
unique_id = uuid.uuid1()
p = Person()
p.person_record_id = '%s/person.%s' % (namespace, unique_id)
dt = datetime.datetime.strptime(timestr, "%Y-%m-%d %H:%M:%S")
time_rfc3339 = convert_datetime_to_rfc3339(dt)
p.entry_date = time_rfc3339
p.expiry_date = vendor.rfc3339.datetimetostr( dt + datetime.timedelta(12 * 30))
#dt = datetime.datetime.strptime(timestr, "%Y-%m-%d %H:%M:%S")
#p.entry_date = vendor.rfc3339.datetimetostr(dt)
p.author_name = source
p.source_name = source
p.first_name = fields['person_first_name']
p.last_name = fields['person_last_name']
p.other = fields['person_other']
n = Note()
n.note_record_id = '%s/note.%s' % (namespace, unique_id)
n.author_name = source
p.source_name = source
n.source_date = p.entry_date
n.text = fields['note_text']
if 'alive' in n.text:
n.status = 'believed_alive'
elif 'missing' in n.text:
n.status = 'believed_missing'
elif 'dead' in n.text:
n.status = 'believed_dead'
elif 'seeking' in n.text:
n.status = 'information_sought'
elif 'author' in n.text:
n.status = 'is_note_author'
p.add_note(n)
return p
def upload_to_personfinder(person):
"""Upload to Google Person Finder """
action = "write"
domain = "rhok"
key = "punsOMMYMAI27tkr"
url = build_personfinder_url(action, domain, key)
logging.debug('URL: %s' % url)
data = to_xml(persons=[person]).toxml().encode('utf8')
logging.debug('data: %s' % data)
req = urllib2.Request(
url, data, { 'Content-Type': 'application/xml' })
result = None
result = urllib2.urlopen(req).read()
logging.debug('return: %s' % result)
return result
if __name__ == "__main__":
time = "2011-06-03 19:55:30"
source = "14154885884"
message = 'Something#Jonathan#No comments.#Note.'
upload_to_personfinder(time, source, message)