-
Notifications
You must be signed in to change notification settings - Fork 3
/
verycd.py
executable file
·123 lines (102 loc) · 3.87 KB
/
verycd.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
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import os
import sys
import requests
import json
import pickle
class VeryCD:
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1) Gecko/20100101 Firefox/14.0.1',
'Referer':'http://www.verycd.com',
'Host':'www.verycd.com',
'X-Requested-With':'XMLHttpRequest'}
login_url = 'http://www.verycd.com/signin'
check_url = 'http://www.verycd.com/game/ajax/checkin/'
cookie_file = os.path.join(os.path.realpath(sys.path[0]), '.verycd_cookies')
def __init__(self, username=None, password=None):
self.username = username
self.password = password
def get_url(self, url, **args):
return requests.get(url, **args)
def post_url(self, url, **args):
return requests.post(url, **args)
def login_success(self):
return self.login_request.text == '{"status":"ok"}'
def checkin_success(self):
return json.loads(self.checkin_request.text).get('code') == 0
def cookies_failed(self, request):
return request.text == '{"code":1,"msg":"\u672a\u767b\u5f55\u7528\u6237"}'
def save_cookies(self):
cookies = self.login_request.cookies
with open(self.cookie_file, 'w') as f:
pickle.dump(requests.utils.dict_from_cookiejar(cookies), f)
def get_cookies(self):
with open(self.cookie_file) as f:
try:
cookies = requests.utils.cookiejar_from_dict(pickle.load(f))
return cookies
except EOFError:
self.login()
return self.login_request.cookies
def login(self):
payload = {'username':self.username,'password':self.password}
request = self.post_url(self.login_url,
data=payload,
headers=self.headers,
timeout=2,
allow_redirects=True)
self.login_request = request
assert self.login_success(), 'login failed'
self.save_cookies()
print 'login ok, cookies save!'
def checkin(self):
request = self.get_url(self.check_url,
headers=self.headers,
cookies=self.get_cookies(),
timeout=2)
self.checkin_request = request
if self.cookies_failed(request):
self.login()
request = self.get_url(self.check_url,
headers=self.headers,
cookies=self.login_request.cookies,
timeout=2)
self.checkin_request = request
assert self.checkin_success(), json.loads(request.text).get('msg').encode('utf-8')
print 'checkin done!'
def command(args=sys.argv[1:]):
def usage():
print """VeryCD Auto Checkin Tool v1.0.0
usage: verycd.py [checkin|login] [-u 'username,password']
"""
def message(name):
print "verycd.py %s -u 'username,password'" %name
sys.exit(1)
def client(args):
verycd = VeryCD()
try:
if args[1] == '-u' and len(args) == 3:
account = args[2].split(',')
if len(account) == 2:
verycd = VeryCD(account[0],account[1])
verycd.login()
if args[0] == 'checkin': verycd.checkin()
sys.exit(1)
else:
message(args[0])
else:
message(args[0])
except IndexError, e:
if args[0] == 'login': message(args[0])
pass
verycd.checkin()
if not args:
usage()
sys.exit(1)
if args[0] in ('login','checkin'):
client(args)
else:
usage()
sys.exit(1)
if __name__ == "__main__":
command()