-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
585 lines (470 loc) · 19.8 KB
/
main.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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
import sys
import subprocess
import os
import importlib
def install(package):
if(package=="tweepy"):
subprocess.call([sys.executable, "-m", "pip", "install","-U", "git+https://github.com/tweepy/tweepy.git@2efe385fc69385b57733f747ee62e6be12a1338b"])
else:
subprocess.call([sys.executable, "-m", "pip", "install", package])
def install_and_import(package,Auto_Install = False):
import importlib
#print(package)
try:
globals()[package] = importlib.import_module(package)
except ImportError:
if Auto_Install:
print("\n >> Auto Installing " + package)
#os.system('python -m pip install '+package)
install(package)
print("\n >> " + package +" Installed.\n")
finally:
if Auto_Install:
globals()[package] = importlib.import_module(package)
print("Checking for Python Version...")
try:
if(True):
assert sys.version_info >= (3, 7)
print("\nYay! You're Good to Go!")
except AssertionError:
print("\nOops! Seems Like You Don't Have Python >= 3.7.x")
print("Consider Upgrading it. Then Try Again")
input("\n>>> Press Enter to Quit for Now")
quit()
print("\nChecking For pip...")
print("\nChecking Dependencies...")
print("All Error Logs are Optimized for Python 3.7")
def Check_consent():
Auto_Install = False
response = ""
while True:
response = input("\nWould you like to fix missing packages while checking ( if any )? [Y / N] => ")
if(response.lower() == 'y'):
Auto_Install = True
break
elif(response.lower()=="n"):
Auto_Install = False
break
else:
print("Sorry that was an Incorrect input.")
if(Auto_Install):
return True
else:
return False
Auto_Install=Check_consent()
error=False
install_and_import("colorama",Auto_Install)
install_and_import("termcolor",Auto_Install)
from termcolor import colored
import time
install_and_import("ctypes",Auto_Install)
install_and_import("pyfiglet",Auto_Install)
install_and_import("tweepy",Auto_Install)
try:
import pyfiglet
except ImportError:
print ('\nWARNING!!!!!!!!! Error, pyfiglet is required')
print ("\n\tQuick Fix:\n")
print ("--Try Running 'python -m pip install pyfiglet' in CMD / Terminal\n")
error=True
try:
import tweepy
except ImportError:
print ('\nWARNING!!!!!!!!! Error, Tweepy is required')
print ("\n\tQuick Fix:\n")
print ("--For Python 3.6 => Try Running 'python -m pip install tweepy' in CMD / Terminal")
print ("--For Python 3.7 => Try Running \n\t'python -m pip install -U git+https://github.com/tweepy/tweepy.git@2efe385fc69385b57733f747ee62e6be12a1338b' in CMD / Terminal")
error=True
'''
try:
from termcolor import colored
except ImportError:
print ('Error, termcolor is required')
error=True
try:
import ctypes
except ImportError:
print ('Error, pathlib is required')
error=True
'''
from time import sleep as SleepTime
install_and_import("pathlib",Auto_Install)
#try:
# import pathlib
#except ImportError:
# print ('Error, pathlib is required')
# error=True
if(error):
print("\n\nThere is/are one or more Dependencies, which are not installed.")
print("Please run 'pip install package_name' to install them.")
print("Then Come Back and Try Again")
input("Press Enter to terminate for now")
quit()
def createDir(nameofdir, hidden = False):
pathlib.Path(os.getcwd()+"\\"+nameofdir).mkdir(exist_ok=True)
if(hidden):
ctypes.windll.kernel32.SetFileAttributesW(os.getcwd()+"\\"+nameofdir, 2)
#--------------------------------------------------------#
colorama.init()
def prRed(skk): print("\033[91m{}\033[00m" .format(skk),end="")
def prGreen(skk): print("\033[92m{}\033[00m" .format(skk),end="")
def prYellow(skk): print("\033[93m{}\033[00m" .format(skk), end="")
def prCyan(skk): print("\033[96m{}\033[00m" .format(skk),end="")
def prWhite(skk): print("\033[97m{}\033[00m" .format(skk),end="")
def prBlack(skk): print("\033[98m{}\033[00m" .format(skk),end="")
def AnimatedPrint(Text,duration=.01,color="white",con=False):
for char in Text:
if(color=="red"):
prRed(char)
elif(color=="green"):
prGreen(char)
elif(color=="yellow"):
prYellow(char)
elif(color=="cyan"):
prCyan(char)
elif(color=="white"):
prWhite(char)
else:
prBlack(char)
SleepTime(duration)
if not con:
print()
# Print iterations progress
def printProgressBar (iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '█'):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print('\r%s |%s| %s%% %s' % (prefix, bar, percent, suffix), end = '\r')
# Print New Line on Complete
if iteration == total:
print()
def sprint(string,style=""):
try:
result = pyfiglet.figlet_format(string, font=style)
except:
result = pyfiglet.figlet_format(string)
#print(prYellow("\n"+result),end="")
return result
def intro():
items = list(range(0, 57))
l = len(items)
# Initial call to print 0% progress
printProgressBar(0, l, prefix = 'Loading App...', suffix = 'Please Wait...', length = 40)
for i, item in enumerate(items):
# Do stuff...
SleepTime(0.02)
# Update Progress Bar
printProgressBar(i + 1, l, prefix = 'Loading App...', suffix = 'Please Wait...', length = 50)
AnimatedPrint("\t\t\t\tLoading Done...!\n",color="green",duration=.02)
SleepTime(.5)
AnimatedPrint("-"*45)
AnimatedPrint(sprint(" W E L C O M E ","bubble"),color="yellow")
AnimatedPrint(sprint("Twitter Auto Reply Bot", "digital"),color="yellow")
AnimatedPrint(" Made by Tuhin Karmakar",duration=.05,color="yellow")
print()
AnimatedPrint("GitHub: http://github.com/tuhinkarmakar3882",color="cyan")
print()
AnimatedPrint("-"*45)
ViewInfo=input("Would you like to view the 'HOW TO USE IT' information? (Y/N): ")
if(len(ViewInfo)!=0 and ViewInfo[0].lower()=='y'):
AnimatedPrint("\n>>> Info :\n\tThis app is made to make it easier for you to set an Auto Reply Response on Twitter."
+ "\n\tIt requires some basic setup to get you started."
+ "\n\tJust follow up the below stuffs carefully and"
+ " you'll be up and running within a few moments!")
AnimatedPrint("\nAll Right! Let's Dig in.\n")
def isExistingUser(): #check for Existing User or Not
try:
os.chdir(os.getcwd()+"//data_Twi")
userStat= open(".userstat","r")
userStat.close()
os.chdir("..")
return True
except:
return False
def BasicInfo():
AnimatedPrint("\t---Basic Details---")
Name_User=""
while(Name_User==""):
AnimatedPrint(">>> What should we call you? => ",con=True)
Name_User=str(input())
if(Name_User==""):
AnimatedPrint("Oh Snap! That's Empty, Let's Try Again.\n")
AnimatedPrint("Hello, "+Name_User,duration=.05)
return(Name_User)
def CheckForDevAc():
agree=["y","yes"]
disagree=["n","no"]
Have_Dev_AC=""
has_ac=False
while(True):
AnimatedPrint(">>> Do you Have a Twitter Developer Account? (Y/N) => ",con=True)
Have_Dev_AC=str(input())
if(Have_Dev_AC.lower() in agree):
AnimatedPrint("Great...!!! Let's Fast Forward to our Main Goal.")
has_ac=True
break
elif(Have_Dev_AC.lower() in disagree):
AnimatedPrint("Ah, Not a Problem. Let's Get you Fixed.")
has_ac=False
break
else:
AnimatedPrint("Looks like, that was an INCORRECT input.\n"
+ "Not to worry Let's Try Again.\n")
return(has_ac)
def guideUser():
print()
AnimatedPrint("To get started, we need to do the following.", color="cyan")
AnimatedPrint("\nStep 1 of 5: Open https://developer.twitter.com/",color="yellow")
AnimatedPrint("Hit Enter When Done...",color="red")
input()
AnimatedPrint("\nStep 2 of 5: Log in with Twitter and Click on Apply.",color="yellow")
AnimatedPrint("Hit Enter When Done...",color="red")
input()
AnimatedPrint("\nStep 3 of 5: Fill Up the Details Over there and Click on Next/Submit.",color="yellow")
AnimatedPrint("Hit Enter When Done...",color="red")
input()
AnimatedPrint("\nStep 4 of 5 : Now that you have an Twitter Developer Account"
+ " click on Apps-> Create an App."
+ "Now Fill up the details.\nDon't Worry about the Website link just add anything in it. "
+ "It hardly Matters."
,color="yellow")
AnimatedPrint("Hit Enter When Done...",color="red")
input()
AnimatedPrint("\nStep 5 of 5: Go to the Keys & Tokens of the App and hit Generate and Save the Keys.",color="yellow")
AnimatedPrint("\n\tNote! These keys are sensitive! Don't Expose it to Anyone else."
+"\n\tThis app uses them just to connect to your Twitter account\n\tand retrieve the Tweets in order"
+" to send replies as per your specifications.",duration=.1,color="cyan")
AnimatedPrint("Hit Enter When Done...",color="red")
input()
def retrieve_last_seen_id(file_name):
f_read = open(file_name, 'r')
last_seen_id = int(f_read.read().strip())
f_read.close()
return last_seen_id
def store_last_seen_id(last_seen_id, file_name):
f_write = open(file_name, 'w')
f_write.write(str(last_seen_id))
f_write.close()
return
def reply_to_tweets(FILE_NAME, hashTag, YourMsg, Name_User):
AnimatedPrint(sprint("---Synchronizing---","bubble"),color="yellow")
AnimatedPrint("Checking for New Tweets...",color="cyan")
AnimatedPrint('Replying to new tweets (if any)...',color="cyan")
# DEV NOTE: use 1060651988453654528 for testing.
last_seen_id = retrieve_last_seen_id(FILE_NAME)
# NOTE: We need to use tweet_mode='extended' below to show
# all full tweets (with full_text). Without it, long tweets
# would be cut off.
mentions = api.mentions_timeline(last_seen_id,tweet_mode='extended')
#try:
# mentions = api.mentions_timeline(last_seen_id,tweet_mode='extended')
#except:
# AnimatedPrint(sprint("<<<<<WRONG CREDENTIALS IN KEYS>>>>>",style="digital"),color="red")
# AnimatedPrint(sprint("Press Enter to exit and Try Again",style="digital"),color="yellow")
# input()
# quit()
for mention in reversed(mentions):
AnimatedPrint(str(mention.id) + ' - ' + mention.full_text,duration=.001)# flush=True)
last_seen_id = mention.id
store_last_seen_id(last_seen_id, FILE_NAME)
if hashTag.lower() in mention.full_text.lower():
AnimatedPrint('Found! '+hashTag, color="yellow")
AnimatedPrint('Responding back...!',color="green")
api.update_status('@' + mention.user.screen_name +" "
+ hashTag + " " + YourMsg + "\n - " + Name_User
+ "<sent via Twitter-Auto-Reply-Bot>", mention.id)
def validateKeys(FILE_NAME,CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET):
try:
AnimatedPrint("Setting Keys")
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
AnimatedPrint("Setting Parameters")
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
AnimatedPrint("Connecting...")
api = tweepy.API(auth)
AnimatedPrint("Verifying...")
ment= api.mentions_timeline(1060651988453654528,tweet_mode='extended')
return True
except:
AnimatedPrint(sprint("<<<<<WRONG CREDENTIALS IN KEYS>>>>>",style="digital"),color="red")
AnimatedPrint(sprint("\tTry Again",style="digital"),color="yellow")
return False
if __name__ == '__main__':
intro()
userstat=isExistingUser()
UseIt=False
if userstat:
while(True):
AnimatedPrint(">>> Existing Configuation Found... Would you like to \n\t1:Use it or 2:Change it?", color="cyan")
AnimatedPrint("Response : ", color="cyan", con=True)
option=str(input())
if(option=='1'):
UseIt=True
break
elif(option=='2'):
UseIt=False
break
else:
AnimatedPrint("Wrong Option. Please Select the Valid One\n", color="red")
if (not userstat) or (not UseIt):
Name_User=BasicInfo()
has_ac=CheckForDevAc()
if not has_ac:
guideUser()
SleepTime(.3)
#Take the keys as Input
AnimatedPrint("-"*45)
AnimatedPrint("\nAlright, now, that You've an account, Please Specify the Required Settings",color="cyan")
AnimatedPrint("\n Step 1 of 4: ",color="yellow")
AnimatedPrint("\t Enter Your Consumer Key => ",con="True")
CONSUMER_KEY = str(input())
AnimatedPrint("\n Step 2 of 4: ",color="yellow")
AnimatedPrint("\t Enter Your Consumer Secret Key => ",con="True")
CONSUMER_SECRET = str(input())
AnimatedPrint("\n Step 3 of 4: ",color="yellow")
AnimatedPrint("\t Enter Your Access Key => ",con="True")
ACCESS_KEY = str(input())
AnimatedPrint("\n Step 4 of 4: ",color="yellow")
AnimatedPrint("\t Enter Your Access Secret Key => ",con="True")
ACCESS_SECRET = str(input())
createDir("data_Twi", hidden=True)
os.chdir(os.getcwd()+"\\data_Twi")
file=open('.CONSUMER_KEY','w')
file.write(CONSUMER_KEY)
file.close()
file=open('.CONSUMER_SECRET','w')
file.write(CONSUMER_SECRET)
file.close()
file=open('.ACCESS_KEY','w')
file.write(ACCESS_KEY)
file.close()
file=open('.ACCESS_SECRET','w')
file.write(ACCESS_SECRET)
file.close()
lastseenfile=open('.LastSeenValue',"w")
lastseenfile.write("1060651988453654528")
lastseenfile.close()
file=open('.USER_NAME','w')
file.write(Name_User)
file.close()
file = open('.userstat','w')
file.write("User has been Successfully Created") #ADDD EXSISTENCE
file.close()
os.chdir("..")
else:
CONSUMER_KEY = ""
CONSUMER_SECRET = ""
ACCESS_KEY = ""
ACCESS_SECRET = ""
Name_User = ""
#change The Dir
os.chdir(os.getcwd()+"\\data_Twi")
#Load Data from Existing File
file=open('.USER_NAME','r')
for name in file:
Name_User = name
file.close()
AnimatedPrint("Welcome Back, "+Name_User, duration=.2, color="yellow")
file=open('.CONSUMER_KEY','r')
for key in file:
CONSUMER_KEY = key
file.close()
file=open('.CONSUMER_SECRET','r')
for key in file:
CONSUMER_SECRET = key
file.close()
file=open('.ACCESS_KEY','r')
for key in file:
ACCESS_KEY = key
file.close()
file=open('.ACCESS_SECRET','r')
for key in file:
ACCESS_SECRET = key
file.close()
os.chdir("..")
#Perform the Connections
AnimatedPrint('\n>>> Activating The Twitter Bot <<<',color='red')
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
FILE_NAME = '.LastSeenValue'
attempt=3
while( not validateKeys(FILE_NAME,CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET) ):
if(attempt==0):
AnimatedPrint(sprint("Too Many Wrong Attempts",style="digital"),color="red")
AnimatedPrint(sprint("Terminating in 5secs",style="bubble"),color="red")
SleepTime(5)
quit()
AnimatedPrint("\n Step 1 of 4: ",color="yellow")
AnimatedPrint("\t Enter Your Consumer Key => ",con="True")
CONSUMER_KEY = str(input())
AnimatedPrint("\n Step 2 of 4: ",color="yellow")
AnimatedPrint("\t Enter Your Consumer Secret Key => ",con="True")
CONSUMER_SECRET = str(input())
AnimatedPrint("\n Step 3 of 4: ",color="yellow")
AnimatedPrint("\t Enter Your Access Key => ",con="True")
ACCESS_KEY = str(input())
AnimatedPrint("\n Step 4 of 4: ",color="yellow")
AnimatedPrint("\t Enter Your Access Secret Key => ",con="True")
ACCESS_SECRET = str(input())
os.chdir(os.getcwd()+"\\data_Twi")
file=open('.CONSUMER_KEY','w')
file.write(CONSUMER_KEY)
file.close()
file=open('.CONSUMER_SECRET','w')
file.write(CONSUMER_SECRET)
file.close()
file=open('.ACCESS_KEY','w')
file.write(ACCESS_KEY)
file.close()
file=open('.ACCESS_SECRET','w')
file.write(ACCESS_SECRET)
file.close()
os.chdir("..")
attempt-=1
AnimatedPrint('\t Activated!\n',color='green')
SleepTime(.3)
AnimatedPrint("-"*45)
AnimatedPrint("Final Step...", color="cyan")
os.chdir(os.getcwd()+"\\data_Twi")
while(True):
AnimatedPrint("\nStep 1 of 2:\n\tWhich HashTag would you like to Target? [e.g. #HelloWorld] => ",color="yellow", con=True)
hashTag = str(input())
if('#' in hashTag):
AnimatedPrint("Alright! HashTag Acquired", color="green")
break
else:
AnimatedPrint("Ouch! Did you forget to include a #?", color="red")
while(True):
AnimatedPrint("\nStep 2 of 2:\n\tEnter Your Auto Reply => ",color="yellow", con=True)
YourMsg=str(input())
if(len(YourMsg)!=0):
AnimatedPrint("Great! Auto Response Message Saved", color="green")
break
else:
AnimatedPrint("Ouch! Did you forget to include a Message?", color="red")
AnimatedPrint("Yay! Ready to Deploy Now!")
items = list(range(0, 100))
l = len(items)
# Initial call to print 0% progress
printProgressBar(0, l, prefix = 'Loading App...', suffix = 'Please Wait...', length = 50)
for i, item in enumerate(items):
# Do stuff...
SleepTime(0.02)
# Update Progress Bar
printProgressBar(i + 1, l, prefix = 'Deploying App...', suffix = 'Please Wait...', length = 50)
AnimatedPrint("\n\t\tYou're Live Now! We'll Refresh in Every 15 Seconds",color="green")
AnimatedPrint("\n\t\t\t To Teminate, just close this Window",color="red")
while True:
reply_to_tweets(FILE_NAME, hashTag, YourMsg, Name_User)
time.sleep(15)