-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwitter.py
executable file
·383 lines (324 loc) · 14.3 KB
/
twitter.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
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
374
375
376
377
378
379
380
381
382
383
from __future__ import print_function
from config import config
import oauth2
import urllib
import sched, time
import json
import os
import sys
import requests
from requests_oauthlib import OAuth1
s = sched.scheduler(time.time, time.sleep)
consumer_secret = config["consumer_secret"]
consumer_key = config["consumer_key"]
access_token = config["access_token"]
access_token_secret = config["access_token_secret"]
webhook_id = config['webhook_id']
env_name = config['env_name']
#webhook_url = urllib.quote_plus(config['webhook_url'])
webhook_url = config['webhook_url']
class TwitterConnection(object):
"""
Class that connects to twitter through get/post HTTP requests.
Sets up authorization and returns decoded JSON responses
"""
def __init__(self, consumer_key, consumer_secret, access_token, access_token_secret):
"""
Initilaise OAuth authorisation
"""
self.oauth = OAuth1(consumer_key,
client_secret=consumer_secret,
resource_owner_key=access_token,
resource_owner_secret=access_token_secret)
def get_webhooks(self):
"""
Get webhook config
"""
response = requests.get(url="https://api.twitter.com/1.1/account_activity/all/webhooks.json", auth=self.oauth)
print(response.json())
def delete_webhook(self):
"""
Delete a webhook
"""
response = requests.delete(url="https://api.twitter.com/1.1/account_activity/all/" + env_name + "/webhooks/" + webhook_id + ".json", auth=self.oauth)
if response.status_code == 204:
print("deleted")
else:
print(response.json())
def set_up_webhook(self):
"""
Set up a web hook for account activity
"""
response = requests.post(url="https://api.twitter.com/1.1/account_activity/all/" + env_name + "/webhooks.json", headers={"content-type" : "application/x-www-form-urlencoded"}, data={"url" : webhook_url}, auth=self.oauth)
print(response.json())
def challenge_webhook(self):
"""
Send CRC check to webhook
"""
response = requests.put(url="https://api.twitter.com/1.1/account_activity/all/" + env_name + "/webhooks/" + webhook_id + ".json", auth=self.oauth)
if response.status_code == 204:
print("webhook valid")
else:
print(response.json())
def subscribe_to_webhook(self):
"""
Subscribe user to webhook using webhook id
"""
response = requests.post(url="https://api.twitter.com/1.1/account_activity/all/" + env_name + "/subscriptions.json", auth=self.oauth)
if response.status_code == 204:
print("subscription success")
else:
print(response.status_code)
print(response.json())
def count_subscriptions(self):
"""
Count all the subscriptions to the webhook
"""
response = requests.get(url="https://api.twitter.com/1.1/account_activity/subscriptions/count", auth=self.oauth)
print(response.json())
def list_subscriptions(self):
"""
List all the subscriptions to the webhook
"""
response = requests.get(url="https://api.twitter.com/1.1/account_activity/all/" + env_name + "/subscriptions/list", auth=self.oauth)
print(response.json())
def check_subscription(self):
"""
Check subscribed to webhook
"""
response = requests.get(url="https://api.twitter.com/1.1/account_activity/all/" + env_name + "/subscriptions", auth=self.oauth)
if response.status_code == 204:
print("subscribed")
else:
print(response.json())
def delete_subscription(self):
"""
Delete subscription
"""
response = requests.delete(url="https://api.twitter.com/1.1/account_activity/all/" + env_name + "/subscriptions", auth=self.oauth)
if response.status_code == 204:
print("deleted")
else:
print(response.json())
def get_messages(self):
"""
Return a json decoded response
List of dictionaries, each dictionary is a direct message
"""
direct_messages = requests.get(url='https://api.twitter.com/1.1/direct_messages/events/list.json', auth=self.oauth)
try:
direct_messages = direct_messages.json()['events']
return direct_messages
except KeyError:
sys.exit(1)
def upload_media(self, file_name, media_type, media_category):
"""
Chunk upload media to twitter and return the media ID once processing is successful
"""
dir_path = os.path.dirname(os.path.realpath(__file__))
path = os.path.abspath(dir_path + file_name)
file_name = path
total_bytes = os.path.getsize(file_name)
request_data = {
'command': 'INIT',
'media_type': 'video/mp4',
'total_bytes': total_bytes,
'media_category': 'dm_video'
}
#first init
init_req = requests.post(url='https://upload.twitter.com/1.1/media/upload.json', data=request_data, auth=self.oauth)
media_id = init_req.json()['media_id']
#then we chunk upload
segment_id = 0
bytes_sent = 0
file = open(file_name, 'rb')
while bytes_sent < total_bytes:
chunk = file.read(1000000)
request_data = {
'command': 'APPEND',
'media_id': media_id,
'segment_index': segment_id
}
files = {
'media':chunk
}
append_req = requests.post(url='https://upload.twitter.com/1.1/media/upload.json', data=request_data, files=files, auth=self.oauth)
segment_id += 1
bytes_sent = file.tell()
print('%s of %s bytes uploaded' % (str(bytes_sent), str(total_bytes)), file=sys.stderr)
print('Upload chunks complete.', file=sys.stderr)
request_data = {
'command': 'FINALIZE',
'media_id': media_id
}
final_req = requests.post(url='https://upload.twitter.com/1.1/media/upload.json', data=request_data, auth=self.oauth)
processing_info = final_req.json().get('processing_info', None)
#check video processing
check = self.check_status(processing_info, media_id)
return check
def check_status(self, processing_info, media_id):
'''
Checks video processing status
'''
if processing_info is None:
return
state = processing_info['state']
print('Media processing status is %s ' % state, file=sys.stderr)
if state == u'succeeded':
print("succeeded", file=sys.stderr)
return media_id
if state == u'failed':
print("failed", file=sys.stderr)
sys.exit(0)
check_after_secs = processing_info['check_after_secs']
print('Checking after %s seconds' % str(check_after_secs), file=sys.stderr)
time.sleep(check_after_secs)
request_params = {
'command': 'STATUS',
'media_id': media_id
}
req = requests.get(url='https://upload.twitter.com/1.1/media/upload.json', params=request_params, auth=self.oauth)
processing_info = req.json().get('processing_info', None)
return self.check_status(processing_info, media_id)
def response(self, message_data, recipient_id):
"""
Sends a direct message using message data argument
"""
request_params = {
"event" : {
"type": "message_create",
"message_create": {
"target": { "recipient_id": recipient_id},
"message_data": message_data
}
}
}
req = requests.post(url='https://api.twitter.com/1.1/direct_messages/events/new.json', headers={"content-type" : "application/json"}, data=json.dumps(request_params), auth=self.oauth)
class Messenger(object):
def __init__(self, consumer_key, consumer_secret, access_token, access_token_secret):
self.prev_messages = []
self.twitter = TwitterConnection(consumer_key, consumer_secret, access_token, access_token_secret)
self.my_sender_id = "811593743858073600"
self.conversations = []
def run(self, new_messages):
"""
Run this show!
"""
#get the messages from twitter
#messages = self.twitter.get_messages()
#if not self.prev_messages:
# print "not got a previous Message"
# self.prev_messages = messages
# return
#filter what ones are new
#new_messages = self.get_new_messages(messages)
#print "The new msg", new_messages
#sort out messages into new and old
new_convo_messages = []
old_convo_messages = []
for message in new_messages:
#ignore the messages I've sent
if self.check_sent_by_me(message) == False:
#sent by someone else - A reply
if self.check_in_convo(message) == False:
#start new
new_convo_messages.append(message)
else:
#continue old
old_convo_messages.append(message)
for message in new_convo_messages:
self.start_conversation(message)
for message in old_convo_messages:
convo = self.get_conversation(message)
self.continue_conversation(message, convo)
self.prev_messages = messages
#s.enter(60, 1, self.run, ())
def get_new_messages(self, messages):
"""
Returns a list of dictionaries that are in messsages but not in prev_messages
"""
diff = [i for i in messages if i not in self.prev_messages]
return diff
def check_sent_by_me(self, message):
"""
Check if the message was sent by me or someone else
"""
if message['message_create']['sender_id'] == self.my_sender_id:
return True
else:
return False
def check_in_convo(self, message):
"""
Check If I've already started a conversation with this ID
"""
for convo in self.conversations:
if message['message_create']['sender_id'] == convo['sender_id']:
return True
return False
def start_conversation(self, message):
"""
Start the conversation
"""
convo = {"sender_id" : message['message_create']['sender_id'], "position" : 0}
self.conversations.append(convo)
self.continue_conversation(message, convo)
def get_conversation(self, message):
"""
Return the conversation dictionary for this sender ID
"""
result = [convo for convo in self.conversations if convo["sender_id"] == message['message_create']['sender_id']]
return result[0]
def continue_conversation(self, message, convo):
"""
Continue conversations
"""
#start of conversations
if convo['position'] == 0:
#Would you like a movie recommendation?
reply = {"text" : "Would you like a movie recommendation?", "quick_reply" : {"type" : "options", "options" : [{"label" : "Yes"}, {"label" : "No"}]}}
self.twitter.response(reply, message['message_create']['sender_id'])
convo['position'] = 1
elif convo['position'] == 1:
#What genre do you like?
if message['message_create']['message_data']['text'] == "Yes":
reply = {"text" : "What genre do you like?", "quick_reply" : {"type" : "options", "options" : [{"label" : "Comedy"}, {"label" : "Action"}]}}
self.twitter.response(reply, message['message_create']['sender_id'])
convo['position'] = 2
elif message['message_create']['message_data']['text'] == "No":
reply = {"text" : "No worries, come back soon"}
self.twitter.response(reply, message['message_create']['sender_id'])
convo['position'] = 0
else:
reply = {"text" : "Sorry didn't get that. Would you like a movie recommendation?", "quick_reply" : {"type" : "options", "options" : [{"label" : "Yes"}, {"label" : "No"}]}}
self.twitter.response(reply, message['message_create']['sender_id'])
convo['position'] = 1
elif convo['position'] == 2:
#Heres a movie
if message['message_create']['message_data']['text'] == "Comedy":
print("chosen comedy", file=sys.stderr)
media_id = self.twitter.upload_media('/assets/bossbaby_vid.mp4', 'video/mp4', 'dm_video')
reply = {"text" : "Boss Baby is a fun comedy! http://www.imdb.com/title/tt3874544/. Thanks!", "attachment" : {"type" : "media", "media" : { "id" : media_id}}}
self.twitter.response(reply, message['message_create']['sender_id'])
convo['position'] = 0
elif message['message_create']['message_data']['text'] == "Action":
media_id = self.twitter.upload_media('/assets/apes_vid.mp4', 'video/mp4', 'dm_video')
reply = {"text" : "War for the Planet of the Apes is an action! http://www.imdb.com/title/tt3450958/. Thanks!", "attachment" : {"type" : "media", "media" : { "id" : media_id}}}
self.twitter.response(reply, message['message_create']['sender_id'])
convo['position'] = 0
else:
reply = {"text" : "Sorry what genre was that?", "quick_reply" : {"type" : "options", "options" : [{"label" : "Comedy"}, {"label" : "Action"}]}}
self.twitter.response(reply, message['message_create']['sender_id'])
convo['position'] = 2
#msg = Messenger(consumer_key, consumer_secret, access_token, access_token_secret)
#msg.run()
#s.enter(60, 1, msg.run, ())
#s.run()
#twitter = TwitterConnection(consumer_key, consumer_secret, access_token, access_token_secret)
#twitter.upload_media('assets/BossBaby_vid.mp4', 'video/mp4', 'dm_video')
#twitter.delete_webhook()
#twitter.set_up_webhook()
#twitter.subscribe_to_webhook()
#twitter.list_subscriptions()
#twitter.get_webhooks()
#twitter.challenge_webhook()
#twitter.delete_webhook()