This repository has been archived by the owner on Feb 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclclt.py
332 lines (297 loc) · 11.8 KB
/
clclt.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
#!/usr/bin/env python2.6
# -*- coding: utf-8 -*-
#
# calculate bot (http://twitter.com/clclt)
#
# Copyright (C) 2011 Michael Karpitsky <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import os
import json
import urllib2
import gzip
import StringIO
import time
import datetime
import rfc822
import random
import oauth2 as oauth
from math import *
from settings import *
class TwitterHandler:
def __init__(self, num=0):
if (TOKEN_KEY[num] and TOKEN_SECRET[num]):
self._signature_method_plaintext = oauth.SignatureMethod_PLAINTEXT()
self._signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1()
self._oauth_token = oauth.Token(key=TOKEN_KEY[num], secret=TOKEN_SECRET[num])
self._oauth_consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
else:
exit()
def UsersLookup(self, user_id):
url = 'https://api.twitter.com/1/users/lookup.json'
data = {}
parameters = {}
uids = list()
uids.extend(user_id)
parameters['user_id'] = ','.join(["%s" % u for u in uids])
_json = self._FetchUrl(url, parameters=parameters)
print _json
try:
data = json.loads(_json)
except:
data = {}
error = self._ParseError(data)
if error is not None:
if error.find("Rate limit") == 0:
print "Rate limit"
exit(1)
return [self._ParseResult(u) for u in data]
def GetFollowerIDs(self, cursor=-1):
url = 'https://api.twitter.com/1/followers/ids.json'
parameters = {}
parameters['cursor'] = cursor
parameters['user_id'] = USERID
_json = self._FetchUrl(url, parameters=parameters)
data = json.loads(_json)
error = self._ParseError(data)
if error is not None:
if error.find("Rate limit") == 0:
print "Rate limit"
exit(1)
return data['ids']
def PostDirectMessage(self, user, text):
url = 'https://api.twitter.com/1/direct_messages/new.json'
data = {'text': text, 'user': user}
_json = json.loads(self._FetchUrl(url, post_data=data))
return _json
def PostUpdate(self, status):
url = 'https://api.twitter.com/1/statuses/update.json'
u_status = unicode(status, 'utf-8')
data = {'status': status}
_json = json.loads(self._FetchUrl(url, post_data=data))
return _json
def _ParseError(self, data):
try:
error = data.get('error', None)
except:
error = None
return error
def _ParseResult(self, user):
data = {}
try:
data['id'] = user.get('id', None)
except:
data['id'] = None
data['screen_name'] = user.get('screen_name', None)
data['created_at'] = user.get('created_at', None)
data['statuses_count'] = user.get('statuses_count', None)
data['followers_count'] = user.get('followers_count', None)
return data
def _FetchUrl(self, url, post_data=None, parameters=None):
if post_data:
http_method = "POST"
else:
http_method = "GET"
opener = urllib2.OpenerDirector()
opener.add_handler(urllib2.HTTPHandler())
opener.add_handler(urllib2.HTTPSHandler())
if not post_data:
opener.addheaders.append(('Accept-Encoding', 'gzip'))
if http_method == "POST":
parameters = post_data.copy()
req = oauth.Request.from_consumer_and_token(self._oauth_consumer,
token=self._oauth_token,
http_method=http_method,
http_url=url, parameters=parameters)
req.sign_request(self._signature_method_hmac_sha1, self._oauth_consumer, self._oauth_token)
if http_method == "POST":
encoded_post_data = req.to_postdata()
else:
encoded_post_data = None
url = req.to_url()
try:
response = opener.open(url, encoded_post_data)
url_data = self._UnGzip(response)
except urllib2.HTTPError, e:
print e
opener.close()
return url_data
def _UnGzip(self, response):
data = response.read()
if response.headers.get('content-encoding', None) == 'gzip':
compressedstream = StringIO.StringIO(data)
url_data = gzip.GzipFile(fileobj=compressedstream).read()
else:
url_data = data
return url_data
class FilesHandler:
def get_json_from_file(self, file, user_file=0):
try:
json_data = open(file)
try:
data = json.load(json_data)
if user_file == 1:
data['sub'] = data.get("sub", "follow")
data['dm'] = data.get("dm", "public")
for i in ["y", "f", "s"]:
if i in data:
pass
else:
data[i] = 0
except:
data = {}
data['sub'] = "follow"
data['dm'] = "public"
data['y'] = 0
data['s'] = 0
data['f'] = 0
json_data.close()
except:
print "get_json_from_file error"
return data
def get_user_info(self, user_id):
user_file = ROOT_PATH + '/data/' + str(user_id) + ".json"
if not os.path.exists(user_file):
open(user_file, 'w').close()
return self.get_json_from_file(user_file, 1)
def save_info_to_file(self, user_id, user_data):
user_file = ROOT_PATH + '/data/' + str(user_id) + ".json"
try:
print user_file
file = open(user_file, 'w')
data = json.dumps(user_data)
print data
file.write(data)
file.close()
except:
exit()
return True
def save_ids(self, ids_file, ids):
file = open(ids_file, 'w')
data = json.dumps(ids)
file.write(data)
file.close()
return True
def get_template(self, template_type, user_name, number, lang=LANG):
template_file = TEMPLATE_PATH + template_type + "_" + lang + ".data"
if template_type == "birthday":
if lang == "ru":
y = ("", '1 год', '2 года', '3 года', '4 года', '5 лет', '6 лет', '7 лет', '8 лет', '9 лет', '10 лет')
elif lang == "en":
y = ("", '1 year', '2 years', '3 years', '4 years', '5 years', '6 years', '7 years', '8 years', '9 years', '10 years')
number = y[number]
lines = open(template_file).read().splitlines()
line = random.choice(lines)
user_name = user_name.encode('ascii','ignore')
return line % (user_name, number)
class Calculate:
def __init__(self):
ids_file = ROOT_PATH + '/ids.json'
self.fh = FilesHandler()
self.bot = 0
self.th = TwitterHandler(self.bot)
self.th_w = TwitterHandler()
self.requests = 0
self.ids = self.fh.get_json_from_file(ids_file)
followersIDs = self.th.GetFollowerIDs()
self.requests += 1
new = self.ids + followersIDs
self.ids = list(set(new))
self.ids.reverse()
self.fh.save_ids(ids_file, self.ids)
def do_calculate(self):
users_count = len(self.ids)
req_count = ceil(users_count/100.0)
i = 0
while i < req_count:
mod_req = self.requests % 340
print self.requests
if mod_req == 0:
self.bot += 1
self.th = TwitterHandler(self.bot)
part_of_100_ids = self.ids[(i*100):(((i+1)*100)-1)]
part_of_100_data = self.th.UsersLookup(user_id=part_of_100_ids)
self.requests += 1
for user in part_of_100_data:
user_data = self.fh.get_user_info(user['id'])
if user_data['sub'] != "nofollow":
lang = "ru"
if user_data.has_key("lang"):
lang = user_data["lang"]
self.checking_birthday(user, user_data, lang)
self.checking_statuses(user, user_data, lang)
self.checking_followers(user, user_data, lang)
i += 1
def checking_birthday(self, user, user_data, lang):
parse_created_at = rfc822.parsedate(user['created_at'])
dt = datetime.datetime.utcnow()
check = 0
if dt.strftime('%m%d') == time.strftime("%m%d", parse_created_at):
indent = 8 # 8 hours to check
i = 0
while i <= indent:
hours_back = int(dt.strftime('%H')) - i
if hours_back == int(time.strftime("%H", parse_created_at)):
check = int(dt.strftime('%Y')) - int(time.strftime("%Y", parse_created_at))
i += 1
self._post("y", check, user, user_data, lang)
def checking_statuses(self, user, user_data, lang):
check = self._roundness(user['statuses_count'], user['id'], 900)
self._post("s", check, user, user_data, lang)
def checking_followers(self, user, user_data, lang):
check = self._roundness(user['followers_count'], user['id'], 100)
self._post("f", check, user, user_data, lang)
def _roundness(self, number, user, min, indent=8):
if number > min:
i = 0
length_mod = len(str(number))
mod = "1" + "0"*(length_mod - 1)
mod = int(mod)
indent = mod/(10 * (length_mod - 1))
while i < indent:
i += 1
result = number + i
is_round = result % mod
if is_round == 0:
return result
return False
def _post(self, type, check, user, user_data, lang):
if check:
user_id = user['id']
if check != user_data[type]:
if type == "s":
template = "statuses"
elif type == "f":
template = "followers"
elif type == "y":
template = "birthday"
user_data[type] = check
message = self.fh.get_template(template, user['screen_name'], check, lang)
if user_data['dm'] == "private":
_check_post = self.th_w.PostDirectMessage(user_id, message)
if _check_post.has_key("error"):
print _check_post
else:
self.fh.save_info_to_file(user_id, user_data)
else:
_check_post = self.th_w.PostUpdate(message)
if _check_post.has_key("error"):
print _check_post
else:
self.fh.save_info_to_file(user_id, user_data)
if __name__ == '__main__':
c = Calculate()
c.do_calculate()
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4