-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_twit.py
525 lines (479 loc) · 16.9 KB
/
simple_twit.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# SimpleTwit.py
# Author: Christopher Kane
import tweepy
from tweepy.error import *
import sys, os, json, webbrowser
# CONSTANTS
VERSION = 0.5
CONFIG_FILE = "twitter_bot.config"
#######################################
# GENERAL FUNCTIONS #
#######################################
def version():
res = "simple_twit, version: " + str(VERSION)
print(res)
return res
def create_api():
# Authentication Information
# If you have your own developer credentials copy them here in place of my own.
consumer_key = "o4UY79JTW4dpTRHm88OOfKhQz"
consumer_secret = "6leYjM3tx72d3DVuLliXkcjMpA384cLHzUfjULPaAcGr6zfJN7"
access_token = None
access_token_secret = None
verify_access = True
if os.path.exists(CONFIG_FILE):
print("READING AUTHORIZATION FROM CONFIG FILE")
f = open(CONFIG_FILE, "r")
config = json.load(f)
access_token = config["access_token"]
access_token_secret = config["access_secret"]
f.close()
verify_access = False
# Authentication Methods
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
if verify_access:
print("AUTHORIZING THROUGH WEB INTERFACE")
try:
redirect_url = auth.get_authorization_url()
except tweepy.TweepError as e:
print("REQUEST VERIFIER URL", e)
print(redirect_url)
print()
webbrowser.open(redirect_url)
verifier = input("Enter Verifier: ")
try:
auth.get_access_token(verifier)
except tweepy.TweepError as e:
print("REQUEST ACCESS TOKEN", e)
access_token = auth.access_token
access_token_secret = auth.access_token_secret
config = {"access_token" : access_token,
"access_secret" : access_token_secret}
f = open(CONFIG_FILE, "w")
json.dump(config, f)
f.close()
if access_token != None:
auth.set_access_token(access_token, access_token_secret)
else:
print("AUTHENTICATION FAILED: EXITING PROGRAM!")
sys.exit()
# Create API object
api = tweepy.API(auth, wait_on_rate_limit = True, wait_on_rate_limit_notify = True)
return api
# end def create_api()
def create_api2():
# Authentication Information
# If you have your own developer credentials copy them here in place of my own.
consumer_key = "26afeoCOwhl0MeioZDwgeLa5s"
consumer_secret = "ugJ45TCai8DcfpOekjTAvNpGp95vpQPWoTktXg9sEHLLspGgKv"
# Authentication Methods
auth = tweepy.AppAuthHandler(consumer_key, consumer_secret)
api = tweepy.API(auth)
return api
# end def create_api2()
#######################################
# TWEET FUNCTIONS #
#######################################
def send_tweet(api = None, text = ""):
usage = "USAGE: send_tweet(api <a tweepy api object>, text <a string>)"
if api == None:
print("ERROR: You must pass an api object to this function.")
print(usage)
return
if text == "":
print("ERROR: You must pass a string to this function.")
print(usage)
return
try:
result = api.update_status(status = text)
except TweepError as e:
print(e)
return
return result
# end def send_tweet()
def send_reply_tweet(api = None, text = "", id = None):
usage = "USAGE: send_tweet(api <a tweepy api object>, text <a string>, id <an int>)"
if api == None:
print("ERROR: You must pass an api object to this function.")
print(usage)
return
if text == "" or "@" not in text:
print("ERROR: You must pass a string to this function.")
print("ERROR: The text must include @username for the author of the" +
" tweet to which this is a reply.")
print(usage)
return
if id == None:
print("ERROR: You must pass a status id in order to send a reply.")
try:
result = api.update_status(status = text, in_reply_to_status_id = id)
except TweepError as e:
print(e)
return
return result
# end def send_reply_tweet()
def send_media_tweet(api = None, text = "", filename = ""):
usage = "USAGE: send_media_tweet(api <a tweepy api object>, text <a string>, filename <a string>)"
if api == None:
print("ERROR: You must pass an api object to this function.")
print(usage)
return
if filename == "":
print("ERROR: you must pass a filename as a string to this function.")
print(usage)
return
try:
mo = api.media_upload(filename) # Returns a Media Object
result = api.update_status(status = text, media_ids = [mo.media_id])
except TweepError as e:
print(e)
return
return result
# end def send_media_tweet()
def retweet(api = None, id = None):
usage = "USAGE: retweet(api <a tweepy api object>, id <numerical id of tweet>)"
if api == None:
print("ERROR: You must pass an api object to this function.")
print(usage)
return
if id == None:
print("ERROR: You must pass an numerical status id to this function.")
print(usage)
return
try:
result = api.retweet(id = id, tweet_mode = "extended")
except TweepError as e:
print(e)
return
return result
# end def retweet()
def get_tweet(api = None, id = None):
usage = "USAGE: get_tweet(api <a tweepy api object>, id <numerical id of tweet>)"
if api == None:
print("ERROR: You must pass an api object to this function.")
print(usage)
return
if id == None:
print("ERROR: You must pass an numerical status id to this function.")
print(usage)
return
try:
result = api.get_status(id = id, tweet_mode = "extended")
except TweepError as e:
print(e)
return
return result
# end def get_tweet()
def get_retweets(api, id, count = 20):
usage = "USAGE: get_retweets(api <a tweepy api object>, id <numerical id of tweet>, "
usage += "count <number of tweets to retrieve>)"
if api == None:
print("ERROR: You must pass an api object to this function.")
print(usage)
return
if id == None:
print("ERROR: You must pass an numerical status id to this function.")
print(usage)
return
if count < 1:
print("ERROR: count argument must be a positive integer; default = 20.")
print(usage)
return
tweets = []
try:
for tweet in tweepy.Cursor(api.retweets, id = id, tweet_mode = "extended").items(count):
tweets.append(tweet)
except TweepError as e:
print(e)
return
return tweets
# end def get_retweets()
def get_retweeters(api, id, count = 20):
usage = "USAGE: get_retweeters(api <a tweepy api object>, id <numerical id of tweet>, "
usage += "count <number of user ids to retrieve>)"
if api == None:
print("ERROR: You must pass an api object to this function.")
print(usage)
return
if id == None:
print("ERROR: You must pass an numerical status id to this function.")
print(usage)
return
if count < 1:
print("ERROR: count argument must be a positive integer; default = 20.")
print(usage)
return
tweets = []
try:
for user_id in tweepy.Cursor(api.retweeters, id = id, tweet_mode = "extended").items(count):
tweets.append(tweet)
except TweepError as e:
print(e)
return
return tweets
# end def get_retweeters()
#######################################
# TIMELINE FUNCTIONS #
#######################################
def get_home_timeline(api = None, count = 20):
usage = "USAGE: get_home_timeline(api <a tweepy api object>, "
usage += "count <number of tweets to retrieve>)"
if api == None:
print("ERROR: You must pass an api object to this function.")
print(usage)
return
if count < 1:
print("ERROR: count argument must be a positive integer; default = 20.")
print(usage)
return
tweets = []
#print("Entering Cursor")
try:
for tweet in tweepy.Cursor(api.home_timeline, tweet_mode = "extended").items(count):
#print("Cursor Iterating")
tweets.append(tweet)
except TweepError as e:
print(e)
return []
return tweets
# end def get_home_timeline()
def get_user_timeline(api = None, user = "", count = 20):
usage = ("USAGE: get_user_timeline(api <a tweepy api object>, " +
"user <name of user>, count <number of tweets to retrieve>)")
if api == None:
print("ERROR: You must pass an api object to this function.")
print(usage)
return
if user == "":
print("ERROR: You must pass an user identifier string to this function.")
print(usage)
return
if count < 1:
print("ERROR: count argument must be a positive integer; default = 20.")
print(usage)
return
tweets = []
try:
for tweet in tweepy.Cursor(api.user_timeline, id = user, tweet_mode = "extended").items(count):
tweets.append(tweet)
except TweepError as e:
print(e)
return
return tweets
# end def get_user_timeline()
def get_retweets_of_me(api = None, count = 20):
usage = "USAGE: get_retweets(api <a tweepy api object>, "
usage += "count <number of tweets to retrieve>)"
if api == None:
print("ERROR: You must pass an api object to this function.")
print(usage)
return
if count < 1:
print("ERROR: count argument must be a positive integer; default = 20.")
print(usage)
return
tweets = []
try:
for tweet in tweepy.Cursor(api.get_retweets_of_me, tweet_mode = "extended").items(count):
tweets.append(tweet)
except TweepError as e:
print(e)
return
return tweets
# end def get_retweets()
def get_mentions(api = None, count = 20):
usage = "USAGE: get_mentions(api <a tweepy api object>, "
usage += "count <number of tweets to retrieve>)"
if api == None:
print("ERROR: You must pass an api object to this function.")
print(usage)
return
if count < 1:
print("ERROR: count argument must be a positive integer; default = 20.")
print(usage)
return
tweets = []
try:
for tweet in tweepy.Cursor(api.mentions_timeline, tweet_mode = "extended").items(count):
tweets.append(tweet)
except TweepError as e:
print(e)
return
return tweets
# end def get_mentions()
#######################################
# USER FUNCTIONS #
#######################################
def get_user(api = None, user = None):
usage = "USAGE: get_user(api <a tweepy api object>, user <name of user>)"
if api == None:
print("ERROR: You must pass an api object to this function.")
print(usage)
return
if user == "":
print("ERROR: You must pass a user name or screen name to this function.")
print(usage)
return
try:
result = api.get_user(id = user, tweet_mode = "extended")
except TweepError as e:
print(e)
return
return result
# end def get_user()
def get_user_friends(api = None, user = "", count = 100):
usage = "USAGE: get_user_friends(api <a tweepy api object>, user <name of user>)"
if api == None:
print("ERROR: You must pass an api object to this function.")
print(usage)
return
if user == "":
print("ERROR: You must pass a user name or screen name to this function.")
print(usage)
return
users = []
try:
for u in tweepy.Cursor(api.friends, id = user, tweet_mode = "extended").items(count):
users.append(u)
except TweepError as e:
print(e)
return
return users
# end def get_user_friends()
def get_my_friends(api = None, count = 100):
usage = "USAGE: get_my_friends(api <a tweepy api object>)"
if api == None:
print("ERROR: You must pass an api object to this function.")
print(usage)
return
users = []
try:
for u in tweepy.Cursor(api.friends, tweet_mode = "extended").items(count):
users.append(u)
except TweepError as e:
print(e)
return
return users
# end def get_my_friends()
def get_user_followers(api = None, user = "", count = 100):
usage = "USAGE: get_user_followers(api <a tweepy api object>, user <name of user>)"
if api == None:
print("ERROR: You must pass an api object to this function.")
print(usage)
return
if user == "":
print("ERROR: You must pass a user name or screen name to this function.")
print(usage)
return
users = []
try:
for u in tweepy.Cursor(api.followers, id = user, tweet_mode = "extended").items(count):
users.append(u)
except TweepError as e:
print(e)
return
return users
# end def get_user_followers()
def get_my_followers(api = None, count = 100):
usage = "USAGE: get_my_followers(api <a tweepy api object>)"
if api == None:
print("ERROR: You must pass an api object to this function.")
print(usage)
return
users = []
try:
for u in tweepy.Cursor(api.followers, tweet_mode = "extended").items(count):
users.append(u)
except TweepError as e:
print(e)
return
return users
# end def get_my_followers()
#######################################
# FOLLOW and UNFOLLOW #
#######################################
def follow_user(api = None, user = ""):
usage = "(USAGE: follow_user(api <a tweepy api object>, user <name of user>)"
if api == None:
print("ERROR: You must pass an api object to this function.")
print(usage)
return
if user == "":
print("ERROR: You must pass a user name or screen name to this function.")
print(usage)
return
try:
result = api.create_friendship(id = user)
except TweepError as e:
print(e)
return
return result
# end def follow_user()
def unfollow_user(api = None, user = ""):
usage = "(USAGE: unfollow_user(api <a tweepy api object>, user <name of user>)"
if api == None:
print("ERROR: You must pass an api object to this function.")
print(usage)
return
if user == "":
print("ERROR: You must pass a user name or screen name to this function.")
print(usage)
return
try:
result = api.destroy_friendship(id = user)
except TweepError as e:
print(e)
return
return result
# end def unfollow_user()
#######################################
# Search Functions #
#######################################
def search(api = None, query = "", count = 20):
usage = "USAGE: search_users(api <a tweepy api object>, query <a string query>, "
usage += "count <number of users to return>"
if api == None:
print("ERROR: You must pass an api object to this function.")
print(usage)
return
if query == "":
print("ERROR: You must pass a string as a query to search on.")
print(usage)
return
if count < 1:
print("ERROR: count argument must be a positive integer; default = 20.")
print(usage)
return
tweets = []
try:
for tweet in tweepy.Cursor(api.search, q = query, tweet_mode = "extended").items(count):
tweets.append(tweet)
except TweepError as e:
print(e)
return
return tweets
# end def search()
def search_users(api = None, query = "", count = 20):
usage = "USAGE: search_users(api <a tweepy api object>, query <a string query>, "
usage += "count <number of users to return>"
if api == None:
print("ERROR: You must pass an api object to this function.")
print(usage)
return
if query == "":
print("ERROR: You must pass a string as a query to search on.")
print(usage)
return
if count < 1:
print("ERROR: count argument must be a positive integer; default = 20.")
print(usage)
return
users = []
try:
for user in tweepy.Cursor(api.search_users, q = query, tweet_mode = "extended").items(count):
users.append(user)
except TweepError as e:
print(e)
return
return users
# end def search_users()