This repository was archived by the owner on Apr 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtokens.py
executable file
·71 lines (53 loc) · 1.56 KB
/
tokens.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
#!/usr/bin/env python
"""
Author: John D. Anderson
Email: jander43@vols.utk.edu
Description: Script to obtain tokens for Twitter API
Usage: tokens
"""
# libs
import sys
import webbrowser
# globals
MSG_USER = '''
A webbrowser has been opened to the following address: apps.twitter.com. Please
check the README.md file for info on how to copy/paste tokens from the web page
to the terminal.
'''
TOKEN_FILE = './.twitter_tokens'
TOKEN_URL = 'http://apps.twitter.com'
TOKEN_NAMES = ('TWITTER_CONSUMER_KEY', 'TWITTER_CONSUMER_SECRET',
'TWITTER_ACCESS_TOKEN', 'TWITTER_ACCESS_TOKEN_SECRET')
# funcs
def get_tokens():
"""Prompt user interactively for Twitter tokens."""
# token dict
tokens = {}
# notify user
print MSG_USER
# get input
try:
for toke in TOKEN_NAMES:
# prompt user
tokens[toke] = raw_input('{0}: '.format(toke)).strip()
except KeyboardInterrupt:
sys.exit('\n\nAborting token file configuration\n')
# return tokens
return tokens
def write_token_file(token_dict):
"""Write out token values to a hidden file."""
# write token file
with open(TOKEN_FILE, 'w') as tokefile:
for key, value in token_dict.iteritems():
tokefile.write('export {0}={1}\n'.format(key, value))
def main():
"""Main function to execute setup of token file."""
# open webbrowser
webbrowser.open(TOKEN_URL)
# get keys
token_values = get_tokens()
# write token file
write_token_file(token_values)
# executable
if __name__ == '__main__':
main()