forked from huxuan/xiami_auto_checkin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xiami.py
executable file
·372 lines (334 loc) · 13.7 KB
/
xiami.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# encoding:utf-8
#-------------------------------------------------------------------------------
# Name: xiami.py
# Purpose:
# Author: [email protected]
# Created: May 17, 2012
#-------------------------------------------------------------------------------
import re
import os
import urllib
import urllib2
import cookielib
import pprint
import logging
import logging.handlers
import random
import datetime
import time as Time
import traceback
from StringIO import StringIO
import gzip
import time
# Init urllib2
global_cookie_jar = cookielib.CookieJar()
global_cookie_processor = urllib2.HTTPCookieProcessor(global_cookie_jar)
global_opener = urllib2.build_opener(global_cookie_processor)
urllib2.install_opener(global_opener)
def initlogger():
"""
Initialize logger
"""
# Formatter
logformatter = logging.Formatter('[%(asctime)s] [%(name)-12s] [%(filename)s(%(lineno)d)] %(message)s')
# Filter
xiamifilter = logging.Filter('xiami')
# Rotating file logger
#rotatingfilehandler = logging.handlers.RotatingFileHandler("xiami.log", "a", 5 * 1024 * 1024, 3)
#rotatingfilehandler.setLevel(logging.DEBUG)
#rotatingfilehandler.setFormatter(logformatter)
#rotatingfilehandler.addFilter(xiamifilter)
# Console logger
consolehandler = logging.StreamHandler()
consolehandler.setLevel(logging.DEBUG)
consolehandler.setFormatter(logformatter)
consolehandler.addFilter(xiamifilter)
#logging.getLogger('').addHandler(rotatingfilehandler)
logging.getLogger('').addHandler(consolehandler)
logging.getLogger('').setLevel(logging.DEBUG)
class XiamiHandler:
"""
Xiami web request/response Handler
"""
def __init__(self, username, password):
self.logger = logging.getLogger('xiami.XiamiHandler')
self.username = username
self.password = password
self.login_response = ''
self.check_in_response = ''
self.logout_response = ''
self.cookie = ''
self.user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) " \
"Chrome/27.0.1453.94 Safari/537.36"
self.main_page_url = 'http://www.xiami.com'
self.mobile_url = 'http://www.xiami.com/web'
self.login_url = 'https://login.xiami.com/member/login'
self.login_data = {
'_xiamitoken': '',
'done': "/",
'type': '',
'email': self.username,
'password': self.password,
'autologin': 0,
'submit': '登 录',
}
self.common_headers = {
'Referer': 'http://www.xiami.com/',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip,deflate,sdch',
'Accept-Language': 'zh-CN,en-US;q=0.8,en;q=0.6',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
#'Host': 'http://www.xiami.com',
'Origin': 'http://www.xiami.com',
'Pragma': 'no-cache',
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': self.user_agent,
'X-Requested-With': 'XMLHttpRequest',
}
self.home_headers = {
'Referer': 'http://www.xiami.com/',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding': 'gzip,deflate,sdch',
'Accept-Language': 'zh-CN,en-US;q=0.8,en;q=0.6',
'Cache-Control': 'must-revalidate',
'Connection': 'keep-alive',
#'Host': 'www.xiami.com',
#'Origin': 'http://www.xiami.com',
#'Pragma': 'no-cache',
#'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': self.user_agent,
}
self.checkin_url = "http://www.xiami.com/task/signin"
self.checkin_headers = {
'Referer': 'http://www.xiami.com/home',
'Accept': '*/*',
'Accept-Encoding': 'gzip,deflate,sdch',
'Accept-Language': 'zh-CN,en-US;q=0.8,en;q=0.6',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Host': 'www.xiami.com',
'Origin': 'http://www.xiami.com',
'Pragma': 'no-cache',
'Content-Length': '0',
'X-Requested-With': 'XMLHttpRequest',
'User-Agent': self.user_agent,
}
self.logout_url = 'http://www.xiami.com/member/logout'
self.logout_headers = {
'Referer': 'http://www.xiami.com/member/mypm-notice',
'User-Agent': self.user_agent,
}
self.user_id = -1
self.mail_content = {}
def process(self, debug=False):
self.mail_content = {}
login_result = self._login()
if not login_result:
self.logger.error('[Error] Login Failed! user = %s', self.username)
return False
result = False
day = self._get_day(self.login_response)
if day:
# Checkin Already
self.logger.info('[Succeed] Checkin Already! user = %s, result = %s days',
self.username, day)
result = True
else:
result = self._checkin()
if result:
self._logout()
self.logger.info('Logout, user = %s', self.username)
if debug:
self._dump()
return result
@staticmethod
def _get_day(content):
try:
if isinstance(content, str):
content = content.decode("utf-8")
#pass
if isinstance(content, unicode):
pass
#content = content.encode("utf-8")
except UnicodeEncodeError:
pass
except UnicodeDecodeError:
pass
#签到%d天
number = re.search(ur"\u7b7e\u5230(\d+)\u5929", content)
if number:
return number.group(1)
return None
def _login(self):
try:
xiami_token = ""
# Main page
main_page_request = urllib2.Request(self.main_page_url, None, self.common_headers)
main_page_response = urllib2.urlopen(main_page_request)
self.logger.info('_login requested main page ...')
cookies = global_cookie_jar.make_cookies(main_page_response, main_page_request)
if cookies:
xiami_cookie = cookies[0] # _xiamitoken
#unsign_cookie = cookies[1] # _unsign_token
xiami_token = xiami_cookie.value
#unsign_token = xiami_cookie.unsign_cookie
self.logger.info('_xiamitoken = %s', pprint.pformat(cookies))
else:
self.logger.info('no cookies ...')
# home
time_value = int(time.time()*1000)
main_page_home_request = urllib2.Request(
"%s/index/home?_=%s" % (self.main_page_url, str(time_value)),
None, self.home_headers)
main_page_home_response = urllib2.urlopen(main_page_home_request)
self.logger.info('_login requested index/home, time_value = %s', time_value)
cookies = global_cookie_jar.make_cookies(main_page_home_response, main_page_home_request)
if cookies:
xiami_cookie = cookies[0] # _xiamitoken
#unsign_cookie = cookies[1] # _unsign_token
xiami_token = xiami_cookie.value
#unsign_token = xiami_cookie.unsign_cookie
#self.logger.info('_xiamitoken = %s', pprint.pformat(cookies))
else:
#self.logger.info('no cookies ...')
pass
self.login_data.update({"_xiamitoken": xiami_token})
self.logger.info('token = %s', xiami_token)
# Post login info
login_request = urllib2.Request(self.login_url, urllib.urlencode(self.login_data), self.common_headers)
login_response = urllib2.urlopen(login_request)
self.user_id = global_cookie_jar._cookies[".xiami.com"]["/"]["user"].value.split('%22')[0]
self.logger.info('user_id = %s', self.user_id)
# Switch to mobile web page
mobile_request = urllib2.Request(self.mobile_url, None, self.common_headers)
mobile_response = urllib2.urlopen(mobile_request)
#self.login_response = urllib2.urlopen(mobile_request).read()
if mobile_response.info().get('Content-Encoding') == 'gzip':
temp_buffer = StringIO(mobile_response.read())
gf = gzip.GzipFile(fileobj=temp_buffer)
self.login_response = gf.read()
self.mail_content.update({"_login": "OK"})
return True
except urllib2.HTTPError, he:
self.mail_content.update({"_login": he})
self.logger.error('[Error] _login Failed! error = %s', he)
self.login_response = ""
return False
except urllib2.URLError, ue:
self.mail_content.update({"_login": ue})
self.logger.error('[Error] _login Failed! error = %s', ue)
self.login_response = ""
return False
except Exception, ce:
self.mail_content.update({"_login": ce})
self.logger.error('[Error] _login Failed! error = %s', ce)
self.login_response = ""
return False
def _logout(self):
# Logout
try:
logout_request = urllib2.Request(self.logout_url, None, self.logout_headers)
self.logout_response = urllib2.urlopen(logout_request).read()
except urllib2.HTTPError, he:
self.logger.error('[Error] _logout Failed! error = %s', he)
self.logout_response = ""
except urllib2.URLError, ue:
self.logger.error('[Error] _logout Failed! error = %s', ue)
self.login_response = ""
return False
except Exception, ce:
self.logger.error('[Error] _logout Failed! error = %s', ce)
self.logout_response = ""
def _checkin(self):
try:
mobile_sign_in_url = '%s/checkin/id/%s' % (self.mobile_url, self.user_id)
sign_in_request = urllib2.Request(mobile_sign_in_url, None, self.common_headers)
sign_in_response = urllib2.urlopen(sign_in_request)
#self.check_in_response = urllib2.urlopen(sign_in_request).read()
if sign_in_response.info().get('Content-Encoding') == 'gzip':
temp_buffer = StringIO(sign_in_response.read())
gf = gzip.GzipFile(fileobj=temp_buffer)
self.check_in_response = gf.read()
checked_days = self._get_day(self.check_in_response)
self.logger.info('[Succeed] Checked in! user = %s, result = %s days',
self.username, checked_days)
self.mail_content.update({"_checkin": "OK, %s days" % checked_days})
return True
except urllib2.HTTPError, he:
self.mail_content.update({"_checkin": he})
self.logger.error('[Error] _checkin Failed! error = %s', he)
self.check_in_response = ""
return False
except urllib2.URLError, ue:
self.mail_content.update({"_checkin": ue})
self.logger.error('[Error] _checkin Failed! error = %s', ue)
self.check_in_response = ""
return False
except Exception, ce:
self.mail_content.update({"_checkin": ce})
self.logger.error('[Error] _checkin Failed! error = %s', ce)
self.check_in_response = ""
return False
def _dump(self):
datas = {
'login': self.login_response,
#'checkin': self.check_in_response,
#'logout': self.logout_response
}
logdir = os.path.join(os.getcwd(), "logs")
if not os.path.exists(logdir):
os.mkdir(logdir)
for k, v in datas.items():
output_file_name = os.path.join(logdir, "%s-%s.html" % (k, self.username))
output_file = file(output_file_name, "wb")
output_file.write(v)
output_file.close()
users_info = [
("[email protected]", "xxx"),
("[email protected]", "xxx"),
]
def work():
"""
Main process of auto checkin
"""
# Logger
initlogger()
xiami_logger = logging.getLogger('xiami')
xiami_logger.info('')
xiami_logger.info('-' * 30)
try:
xiami_logger.info('Start single-pass task...')
all_done = True
results = {
"title": "",
"content": [],
}
for i, p in enumerate(users_info):
xiami_handler = XiamiHandler(p[0], p[1])
xiami_logger.info('Process %s...', p[0])
retry = 5
while not xiami_handler.process(debug=False):
retry -= 1
if retry < 0:
xiami_logger.error('Process %s failed! abort...', p[0])
break
xiami_logger.error('Process %s failed! try it again 15 seconds later...', p[0])
Time.sleep(15)
if retry < 0:
all_done = False
results["content"].append({
"user": p[0],
"result": xiami_handler.mail_content,
})
if i != len(users_info) - 1:
Time.sleep(random.randint(5, 10))
xiami_logger.info('Single-pass task was completed successfully. Quit.')
results["title"] = "XIAMI check-in %s %s" % (datetime.datetime.now().strftime("%Y%m%d"),
"OK" if all_done else "FAILED")
return results
except Exception, e:
xiami_logger.error('fatal error, error = %s, traceback = %s', e, traceback.format_exc())
return {}
if __name__ == '__main__':
pprint.pprint(work())