-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiToken.py
69 lines (47 loc) · 2.58 KB
/
apiToken.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
import sys
import subprocess
import json
from os import path
# Importing Cryptography library
try:
from cryptography.fernet import Fernet
print('importing cryptography ...')
except ModuleNotFoundError:
print("Warning: cryptography module not found!")
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'cryptography'])
print("\nimporting cryptography ...")
from cryptography.fernet import Fernet
def getKeys(console_entry = bool()):
if not console_entry:
raw_entry = input("\nAre you authorized to use Sanchayan's Twitter API Tokens? ... [Yes/no] ")
if raw_entry.lower() in ['true', '1', 't', 'y', 'yes', 'yeah', 'yup', 'ye']:
console_entry = True
elif raw_entry.lower() in ['no', '0', 'not', 'false', 'none', 'n', 'f', 'fls', 'fals']:
console_entry = False
else:
print("\nError: Invalid Option! \nPlease try again with correct option.")
return getKeys()
if console_entry == True and not path.exists("key/tokens.encrypted"):
print("\nError: Sanchayan's encrypted token file is missing. \nPlease try with your own API keys.")
return getKeys(console_entry = False)
elif console_entry == True and path.exists("key/tokens.encrypted"):
decryption_key = input("\nPlease Enter The Decryption Key Provided by Sanchayan: ")
try:
with open('key/tokens.encrypted', 'rb') as encrypted_file:
decrypted_content = Fernet(decryption_key).decrypt(encrypted_file.read())
token = json.loads(decrypted_content)
consumer_key = token.get("API Key")
consumer_secret = token.get("API Secret")
access_token = token.get("Access Token")
access_token_secret = token.get("Access Token Secret")
return (consumer_key, consumer_secret, access_token, access_token_secret)
except:
print("\nError: Wrong decryption key! \nPlease check the key and try again.")
return getKeys()
elif console_entry == False:
print("\nPlease Create a Twitter API token and enter below.\n")
consumer_key = input('Please Enter API Key: ')
consumer_secret = input('Please Enter API Secret: ')
access_token = input('Please Enter Access Token: ')
access_token_secret = input('Please Enter Access Token Secret: ')
return (consumer_key, consumer_secret, access_token, access_token_secret)