-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
283 lines (232 loc) · 8.76 KB
/
common.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
import ConfigParser
import datetime
import os
import sys
import oauth2
import pytz
try:
import tzlocal
_DEFAULT_TIMEZONE = tzlocal.get_localzone().zone
except:
_DEFAULT_TIMEZONE = 'Asia/Calcutta'
import six
__version__ = "1.0.dev0"
TWITTER_API_URL = 'https://api.twitter.com/1.1'
# url for list of valid timezones
_TZ_URL = 'http://en.wikipedia.org/wiki/List_of_tz_database_time_zones'
CONF = {
'consumer_key': '',
'consumer_secret': '',
'api_key': '',
'api_secret': '',
'data_to_fetch': 1,
'query': '',
'geocode': '',
'lang': '',
'result_type': 'popular',
'count': 100,
'until': None,
'since_id': None,
'type_of_follow': '1'
}
RESULT_MAP = {
'1': 'popular',
'2': 'recent',
'3': 'mixed'
}
def decoding_strings(f):
def wrapper(*args, **kwargs):
out = f(*args, **kwargs)
if isinstance(out, six.string_types) and not six.PY3:
# todo: make encoding configurable?
if six.PY3:
return out
else:
return out.decode(sys.stdin.encoding)
return out
return wrapper
def _input_compat(prompt):
if six.PY3:
r = input(prompt)
else:
r = raw_input(prompt)
return r
if six.PY3:
str_compat = str
else:
str_compat = unicode
dateObject = 'YYYY-MM-DD'
@decoding_strings
def ask(question, answer=str_compat, default=None, l=None, options=None):
if answer == str_compat:
r = ''
while True:
if default:
r = _input_compat('> {0} [{1}] '.format(question, default))
else:
r = _input_compat('> {0} '.format(question, default))
r = r.strip()
if len(r) <= 0:
if default:
r = default
break
else:
print('You must enter something')
else:
if l and len(r) != l:
print('You must enter a {0} letters long string'.format(l))
else:
break
return r
elif answer == bool:
r = None
while True:
if default is True:
r = _input_compat('> {0} (Y/n) '.format(question))
elif default is False:
r = _input_compat('> {0} (y/N) '.format(question))
else:
r = _input_compat('> {0} (y/n) '.format(question))
r = r.strip().lower()
if r in ('y', 'yes'):
r = True
break
elif r in ('n', 'no'):
r = False
break
elif not r:
r = default
break
else:
print("You must answer 'yes' or 'no'")
return r
elif answer == int:
r = None
while True:
if default:
r = _input_compat('> {0} [{1}] '.format(question, default))
else:
r = _input_compat('> {0} '.format(question))
r = r.strip()
if not r:
r = default
break
try:
r = int(r)
break
except:
print('You must enter an integer')
return r
elif answer == list:
# For checking multiple options
r = None
while True:
if default:
r = _input_compat('> {0} [{1}] '.format(question, default))
else:
r = _input_compat('> {0} '.format(question))
r = r.strip()
if not r:
r = default
break
try:
if int(r) in range(1, len(options) + 1):
break
else:
print('Please select valid option: ' + ' or '.join('{}'.format(s) for _, s in enumerate(options)))
except:
print('Please select valid option: ' + ' or '.join('{}'.format(s) for _, s in enumerate(options)))
return r
if answer == dateObject:
r = ''
while True:
if default:
r = _input_compat('> {0} [{1}] '.format(question, default))
else:
r = _input_compat('> {0} '.format(question, default))
r = r.strip()
if not r:
r = default
break
try:
datetime.datetime.strptime(r, '%Y-%m-%d')
break
except ValueError:
print("Incorrect data format, should be YYYY-MM-DD")
return r
else:
raise NotImplemented(
'Argument `answer` must be str_compat, bool, or integer')
def ask_timezone(question, default, tzurl):
"""Prompt for time zone and validate input"""
lower_tz = [tz.lower() for tz in pytz.all_timezones]
while True:
r = ask(question, str_compat, default)
r = r.strip().replace(' ', '_').lower()
if r in lower_tz:
r = pytz.all_timezones[lower_tz.index(r)]
break
else:
print('Please enter a valid time zone:\n'
' (check [{0}])'.format(tzurl))
return r
def config_reader(filename, exists=False):
config = ConfigParser.RawConfigParser()
if exists:
config.read(filename)
CONF['consumer_key'] = config.get('Credentials', 'consumer_key')
CONF['consumer_secret'] = config.get('Credentials', 'consumer_secret')
CONF['api_key'] = config.get('Credentials', 'api_key')
CONF['api_secret'] = config.get('Credentials', 'api_secret')
else:
config.add_section('Credentials')
config.set('Credentials', 'api_secret', CONF['api_secret'])
config.set('Credentials', 'api_key', CONF['api_key'])
config.set('Credentials', 'consumer_secret', CONF['consumer_secret'])
config.set('Credentials', 'consumer_key', CONF['consumer_key'])
# Writing our configuration file to 'example.cfg'
with open(filename, 'wb') as configfile:
config.write(configfile)
def oauth_req(url, http_method="GET", post_body="", http_headers=None):
consumer_key = CONF['consumer_key']
consumer_secret = CONF['consumer_secret']
key = CONF['api_key']
secret = CONF['api_secret']
consumer = oauth2.Consumer(key=consumer_key, secret=consumer_secret)
token = oauth2.Token(key=key, secret=secret)
client = oauth2.Client(consumer, token)
resp, content = client.request(url, method=http_method, body=post_body, headers=http_headers)
return content
def start():
print(r'''Welcome to Follow Tweet v{v}.
$$$$$$$$\ $$\ $$\ $$$$$$$$\ $$\
$$ _____| $$ |$$ | \__$$ __| $$ |
$$ | $$$$$$\ $$ |$$ | $$$$$$\ $$\ $$\ $$\ $$ |$$\ $$\ $$\ $$$$$$\ $$$$$$\ $$$$$$\
$$$$$\ $$ __$$\ $$ |$$ |$$ __$$\ $$ | $$ | $$ | $$ |$$ | $$ | $$ |$$ __$$\ $$ __$$\\_$$ _|
$$ __|$$ / $$ |$$ |$$ |$$ / $$ |$$ | $$ | $$ | $$ |$$ | $$ | $$ |$$$$$$$$ |$$$$$$$$ | $$ |
$$ | $$ | $$ |$$ |$$ |$$ | $$ |$$ | $$ | $$ | $$ |$$ | $$ | $$ |$$ ____|$$ ____| $$ |$$\
$$ | \$$$$$$ |$$ |$$ |\$$$$$$ |\$$$$$\$$$$ | $$ |\$$$$$\$$$$ |\$$$$$$$\ \$$$$$$$\ \$$$$ |
\__| \______/ \__|\__| \______/ \_____\____/ \__| \_____\____/ \_______| \_______| \____/
This script will help you like user tweets and follow them.
You can unfollow the users who don't follow you back.
Send a Thank you Message to Users who follow you.
Please answer the following questions so this script can generate your
required output.
'''.format(v=__version__))
configfile = 'twitter.cfg'
if os.path.isfile(configfile):
config_reader(configfile, exists=True)
CONF['consumer_key'] = ask(
'Your Application\'s Consumer Key(API Key)? Found here: https://apps.twitter.com/',
answer=str_compat, default=CONF['consumer_key'])
CONF['consumer_secret'] = ask('Your Application\'s Consumer Secret(API Secret)? ' +
'Found here: https://apps.twitter.com/app/{ Your API}/keys',
answer=str_compat, default=CONF['consumer_secret'])
CONF['api_key'] = ask('Your Access Token? ' +
'Found here: https://apps.twitter.com/app/{ Your API}/keys',
answer=str_compat, default=CONF['api_key'])
CONF['api_secret'] = ask('Your Access Token Secret? ' +
'Found here: https://apps.twitter.com/app/{ Your API}/keys',
answer=str_compat, default=CONF['api_secret'])
# if not os.path.isfile(configfile):
config_reader(configfile)