-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitter_listener.py
More file actions
373 lines (308 loc) · 13.2 KB
/
twitter_listener.py
File metadata and controls
373 lines (308 loc) · 13.2 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/python
#
# Copyright (c) 2011 Mark D. Pesce
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import twitter
import os, sys, time, yaml, uuid, socket
import json
#import rsa
from yaml import Loader, Dumper
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Every module has a UUID associated with it, which becomes the item encoded in the signature
# Thus uniquely identifing both the module and the sender
listener_uuid = 'e2726948-68dd-5272-a437-0265f76f53a9' # uuid.uuid5(uuid.NAMESPACE_DNS, 'plexus.relationalspace.org')
dest_id = "e2eb0d8b-b82c-5c2f-b6f1-dee1038fd9ae" # uuid.uuid5(uuid.NAMESPACE_DNS, 'mpesce.plexus.relationalspace.org')
mypod_ip = "74.207.224.149"
kuanyin_ip ="192.168.0.37"
android_ip = "192.168.0.148"
set_ip = "localhost"
# Returns True if we are running on Android - use absolute paths
def isAndroid():
try:
import android
#print "Android!"
return True
except:
return False
def getAndroidPath():
if isAndroid():
return "/sdcard/sl4a/scripts/v2/"
else:
return ""
# Get your own IP address. This is the IPV4 address, so when we go to IPV6, that'll need fixing...
# Also, if this is a multi-homed machine, this could return, erm, "indeterminate" (read: wrong) results.
def get_my_ipv4():
return socket.getaddrinfo(socket.gethostname(), None, socket.AF_INET)[0][4][0]
def writeFile(content, file):
fp = open(file, "wb")
fp.writelines(yaml.dump(content))
fp.close()
# The instance ID is a UUID specific to this instance, uniquely identifies this listener
def getInstance():
#print "getInstance()"
if (os.path.isfile(getAndroidPath() + ".twitter_listener")):
fp = open(getAndroidPath()+ ".twitter_listener", "r")
a = yaml.load(fp)
fp.close()
return a
else:
return -1
#Create an instance ID if one does not yet exist
def genInstance():
print "Generating instance UUID..."
instance_id = str(uuid.uuid4())
writeFile(instance_id, getAndroidPath() + ".twitter_listener")
print "Done!"
# Generate an RSA keypair
def genKey():
print "Generating your keypair, THIS WILL TAKE A LONG TIME. At least ten minutes. Make a nice cuppa..."
keypair = rsa.newkeys(512) # Should be secure until 2030. Well, that's what they say, anyway...
writeFile(keypair[0], getAndroidPath() + ".plexus_public_key") # This is so not secure
writeFile(keypair[1], getAndroidPath() + ".plexus_private_key") # THis is especially not secure
print "Done!"
# Retrieve the RSA keypair
def getPubPriv():
if (os.path.isfile(getAndroidPath() + ".plexus_public_key") and os.path.isfile(getAndroidPath() + ".plexus_private_key")):
fp = open(getAndroidPath() + ".plexus_public_key", "rb")
pubkey = yaml.load(fp)
fp = open(getAndroidPath() + ".plexus_private_key", "rb")
privkey = yaml.load(fp)
fp.close()
return {'priv': privkey, 'pub': pubkey}
else:
return -1
def getLogin():
if (os.path.isfile(getAndroidPath() + ".twittercredentials")):
fp = open(getAndroidPath() + ".twittercredentials", "r")
a = yaml.load(fp)
fp.close()
return a
else:
return -1
def authorize():
result = get_access_token()
fp = open(getAndroidPath() + ".twittercredentials", "wb") # This is not secure and must be fixed
fp.writelines(yaml.dump(result))
fp.close()
def get_access_token():
# parse_qsl moved to urlparse module in v2.6
try:
from urlparse import parse_qsl
except:
from cgi import parse_qsl
import oauth2 as oauth
REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token'
ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token'
AUTHORIZATION_URL = 'https://api.twitter.com/oauth/authorize'
SIGNIN_URL = 'https://api.twitter.com/oauth/authenticate'
consumer_key = "4a4AK5ZUpJu3fxNfaXb5A"
consumer_secret = "IRvMihJ6vVLIMcWDDIe945zoqMHiwfVY3FCbnasMAMk"
if consumer_key is None or consumer_secret is None:
print 'You need to edit this script and provide values for the'
print 'consumer_key and also consumer_secret.'
print ''
print 'The values you need come from Twitter - you need to register'
print 'as a developer your "application". This is needed only until'
print 'Twitter finishes the idea they have of a way to allow open-source'
print 'based libraries to have a token that can be used to generate a'
print 'one-time use key that will allow the library to make the request'
print 'on your behalf.'
print ''
sys.exit(1)
signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1()
oauth_consumer = oauth.Consumer(key=consumer_key, secret=consumer_secret)
oauth_client = oauth.Client(oauth_consumer)
print 'Requesting temp token from Twitter'
resp, content = oauth_client.request(REQUEST_TOKEN_URL, 'GET')
if resp['status'] != '200':
print 'Invalid respond from Twitter requesting temp token: %s' % resp['status']
else:
request_token = dict(parse_qsl(content))
if isAndroid(): # We can launch a browser if it's Android
theURL = '%s?oauth_token=%s' % (AUTHORIZATION_URL, request_token['oauth_token'])
import android
droid = android.Android()
droid.startActivity('android.intent.action.VIEW', theURL)
else:
print ''
print 'Please visit this Twitter page and retrieve the pincode to be used'
print 'in the next step to obtaining an Authentication Token:'
print ''
print '%s?oauth_token=%s' % (AUTHORIZATION_URL, request_token['oauth_token'])
print ''
pincode = raw_input('Pincode? ')
token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
token.set_verifier(pincode)
print ''
print 'Generating and signing request for an access token'
print ''
oauth_client = oauth.Client(oauth_consumer, token)
resp, content = oauth_client.request(ACCESS_TOKEN_URL, method='POST', body='oauth_verifier=%s' % pincode)
access_token = dict(parse_qsl(content))
if resp['status'] != '200':
print 'The request for a Token did not succeed: %s' % resp['status']
print access_token
else:
print 'Your Twitter Access Token key: %s' % access_token['oauth_token']
print ' Access Token secret: %s' % access_token['oauth_token_secret']
print ''
return access_token
def get_statuses(token):
return statuses
def make_msg():
msg = MIMEMultipart()
msg['Subject'] = "Twitter status updates"
msg['From'] = "mpesce@gmail.com"
msg['To'] = "mark@markpesce.com"
#print msg.as_string()
return msg
def mail_msg(txt):
# To transport by SMTP, we wrap our RFC2822-compliant message in another header.
# This allows us to have our way with the sender and receiver fields. Muahahah.
print "Sending mail..."
import smtplib
msg = MIMEText(txt)
msg.set_charset('utf-8')
msg['Subject'] = "twitter_listener.py"
s = smtplib.SMTP()
s.set_debuglevel(False)
s.connect("smtp.smtphost.tld", 587) # smtp.google.com, for example
s.ehlo()
s.starttls()
s.ehlo()
s.login("user@smtphost.tld", "password")
s.sendmail("to@hostname.tld", "from@hostname.tld", msg.as_string())
s.close()
def mail_msg_plexus(txt):
# To transport by SMTP, we wrap our RFC2822-compliant message in another header.
# This allows us to have our way with the sender and receiver fields. Muahahah.
print "Sending mail..."
import smtplib
msg = MIMEText(txt)
msg.set_charset('utf-8')
msg['Subject'] = "twitter_listener.py"
s = smtplib.SMTP(set_ip, 4180) # of course, this could be running anywhere, really
s.sendmail("mark@markpesce.com", "mpesce@gmail.com", msg.as_string())
s.quit()
def get_my_screen_name(api):
statuses = api.GetUserTimeline(count=2)
for status in statuses:
screen_name = status.user.screen_name
return screen_name
def main():
# Check to see if we have a keypair
# keys = getPubPriv()
# if keys == -1:
# print "No RSA keypair, we've got to generate them!"
# genKey()
# keys = getPubPriv()
# Check to see if we have login credentials
credentials = getLogin()
#print credentials
if credentials == -1:
print "You don't have Twitter authorization, let's do that now..."
authorize()
credentials = getLogin()
# Get the Instance ID
instance_id = getInstance()
if instance_id == -1:
print "We don't have an instance ID yet, must create it"
genInstance()
instance_id = getInstance()
# We'll use the Facade UI to ask the user for the server IP addreess, if Android.
if isAndroid():
import android
droid = android.Android()
an_ip = droid.dialogGetInput('IP', 'Server IP address?', '127.0.0.1').result
if (an_ip != None): # Cancel pressed?
if (len(an_ip) > 7): # Malformed IP address? (should be validated)
set_ip = an_ip
else:
if (len(sys.argv) > 1):
set_ip = sys.argv[1]
start_id = None
starting_timestamp = time.time() # Current time
dm_since = None
count_num = 50 # number of statuses to return per check (maximum of 100)
api = twitter.Api(consumer_key="4a4AK5ZUpJu3fxNfaXb5A", consumer_secret="IRvMihJ6vVLIMcWDDIe945zoqMHiwfVY3FCbnasMAMk",
access_token_key=credentials['oauth_token'], access_token_secret=credentials['oauth_token_secret'])
screen_name = get_my_screen_name(api) # odd way to do it, but whatevs
while True:
# Check for DMs before we move along to the statuses (because they're more important, aren't they?)
if (dm_since == None):
print "Starting timestamp: ", starting_timestamp
try:
directs = api.GetDirectMessages()
except:
print 'Twitter API GetDirectMessages blew a sprocket, trying to recover gracefully'
directs = None
else:
try:
directs = api.GetDirectMessages(since_id=dm_since)
except:
print 'Twitter API GetDirectMessages blew a sprocket, trying to recover gracefully'
directs = None
if (directs != None):
for direct in directs:
if dm_since < direct.id:
dm_since = direct.id # Keep things current
if (direct.created_at_in_seconds > starting_timestamp): # Since we started up?
name_list = [ direct.sender_screen_name.encode('utf-8'), ]
plexus_data = { "service": "twitter", "plexus-message": direct.text.encode('utf-8'), "source": name_list, "when": str(direct.created_at_in_seconds) }
msg = MIMEText(json.dumps(plexus_data))
msg.set_charset('utf-8')
msg['To'] = screen_name.encode('utf-8') + "." + dest_id + "@plexus.relationalspace.org" # That should be unique and global across Plexus
msg['From'] = "plexus-message." + instance_id + "@plexus.relationalspace.org" # Routing information for message type
msg['Subject'] = str(uuid.uuid4()) # unique ID for tracking messages
print msg.as_string()
#mail_msg(msg.as_string())
mail_msg_plexus(msg.as_string())
try:
statuses = api.GetFriendsTimeline(count=count_num, since_id=start_id)
except:
print 'Twitter API GetFriendsTimeline blew a sprocket, trying to recover gracefully'
statuses = None
if (statuses != None):
for status in statuses:
if start_id < status.id:
start_id = status.id # Keep latest status
#txt = yaml.dump(status)
#txt = status.user.screen_name.encode('utf-8') + ": " + status.text.encode('utf-8')
#msg = make_msg(api, screen_name, status.text.encode('utf-8'))
name_list = [ status.user.screen_name.encode('utf-8'), ]
plexus_data = { "service": "twitter", "plexus-update": status.text.encode('utf-8'), "source": name_list, "when": str(status.created_at_in_seconds) }
#mime_header ="MIME-Version 1.0\nContent-Transfer-Encoding: 7bit\nContent-Type: plexus/update; charset=\"utf-8\"\n\n"
msg = MIMEText(json.dumps(plexus_data))
msg.set_charset('utf-8')
# Create the 'From' field by signing the UUID of the module with the private key
#signature = rsa.sign(listener_uuid, keys['priv'])
#print signature
# Get the user to put into 'From' field
msg['To'] = screen_name.encode('utf-8') + "." + dest_id + "@plexus.relationalspace.org" # That should be unique and global across Plexus
msg['From'] = "plexus-update." + instance_id + "@plexus.relationalspace.org" # Routing information for message type
msg['Subject'] = str(uuid.uuid4()) # unique ID for tracking messages
print msg.as_string()
#mail_msg(msg.as_string())
mail_msg_plexus(msg.as_string())
print "Sleeping..."
time.sleep(30) # wait 30 seconds, and do it all again
if __name__ == "__main__":
main()