· 5 years ago · Aug 04, 2020, 05:00 PM
1#/testing/twitterbot/main.py
2import sys, string, tweepy, os
3from getpass import getpass
4
5print("#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#")
6print("#-#-#-#-#-#-#-#-TWITTER BOT-#-#-#-#-#-#-#-#")
7print("#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#")
8if len(sys.argv) == 0:
9 print("To access the bot you must verify your twitter")
10 print("API credentials first. Do this with:")
11 sys.exit("python "+sys.argv[0]+" verify")
12elif len(sys.argv) > 2:
13 sys.exit("Arguments too long")
14
15
16def verify(apikey, apikeys, token, tokens):
17 auth = tweepy.OAuthHandler(apikey, apikeys)
18 auth.set_access_token(token, tokens)
19 api = tweepy.API(auth)
20 try:
21 api.verify_credentials()
22 if api.verify_credentials() == False:
23 sys.exit("!!! Error during authentication !!!")
24 else:
25 print("Authentication OK")
26 f = open('config.py', 'a')
27 f.write("#./config.py \n")
28 f.write("apikey = "+apikey+"\n")
29 f.write("apikeys = "+apikeys+"\n")
30 f.write("token = "+token+"\n")
31 f.write("tokens = "+tokens+"\n")
32 f.close()
33
34 except:
35 sys.exit("!!! Error during authentication !!!")
36
37
38if sys.argv[1] == "verify" and os.path.exists('./config.py') == False:
39 apikey = getpass("Insert API Key: ")
40 print("API Key: "+apikey)
41 print("- - - - - - - - - - - - - - - - ")
42 apikeys = getpass("Input API Key Secret: ")
43 print("API Key Secret: "+apikeys)
44 print("- - - - - - - - - - - - - - - - ")
45 token = getpass("Input Access Token: ")
46 print("Access Token: "+token)
47 print("- - - - - - - - - - - - - - - - ")
48 tokens = getpass("Input Access Token Secret: ")
49 print("Access Token Secret: "+tokens)
50 print("- - - - - - - - - - - - - - - - ")
51 print("Testing credentials...")
52 verify(apikey, apikeys, token, tokens)
53elif sys.argv[1] == "verify" and os.path.exists('./config.py') == True:
54 print("config.py already exists. Authenticating with these parameters:")
55 f = open('config.py', 'a')
56 print("- - - - - - - - - - - - - - - - ")
57 print(f.read())
58 print("- - - - - - - - - - - - - - - - ")
59 apikey = f.readline(2)
60 apikeys = f.readline(3)
61 token = f.readline(4)
62 tokens = f.readline(5)
63 f.close()
64 verify(apikey, apikeys, token, tokens)
65else:
66 sys.exit("Please verfiy before continuing.")