This repository has been archived by the owner on May 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
WebManager.py
123 lines (107 loc) · 3.79 KB
/
WebManager.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
import web
import gevent
import gevent.monkey
import gevent.pywsgi
from db import xiamidb
import time
import traceback
import os
from libXiami import User
gevent.monkey.patch_all()
class Main:
def GET(self):
return file("static/index.html").read()
def POST(self):
args = web.input()
email = args.pop("email")
password = args.pop("password")
user = User()
user.login(email,password)
if user.islogined:
xiamidb.add(user["user_id"],email,password)
account = xiamidb.get(user["user_id"])
account["last"] = time.time()
account["cookie"] = user.dumpCookie()
account["email"] = email
if "savepw" in args:
account["password"] = password
else:
account["password"] = None
if "notifyme" in args:
account["notify"] = True
else:
account["notify"] = False
account["errcount"] = 0
account["nexttime"] = account["last"]
account["days"] = int(user["sign"]["persist_num"])
xiamidb.update(**account)
yield "email:%s\n"%email
yield "uid:%s\n"%account['uid']
yield "cookie:\n%s"%account['cookie']
yield "days:%s\n"%account["days"]
yield '\xe7\x99\xbb\xe8\xae\xb0\xe6\x88\x90\xe5\x8a\x9f\xef\xbc\x8c\xe8\xaf\xb7\xe5\x85\xb3\xe9\x97\xad\xe9\xa1\xb5\xe9\x9d\xa2'
else:
yield '\xe6\x97\xa0\xe6\xb3\x95\xe8\x8e\xb7\xe5\x8f\x96\xe7\x94\xa8\xe6\x88\xb7\xe4\xbf\xa1\xe6\x81\xaf\xef\xbc\x8c\xe8\xaf\xb7\xe6\xa3\x80\xe6\x9f\xa5\xe5\xb8\x90\xe5\x8f\xb7\xe6\x88\x96\xe5\xaf\x86\xe7\xa0\x81'
class Ctrl:
def GET(self):
yield "uid\temail\t\t\tdays\tlastcheck\tnextcheck\terrcount\tstatus\n"
accounts = xiamidb.scan()
for account in accounts:
account["last"] -= time.time()
account["nexttime"] -= time.time()
yield "%(uid)s\t%(email)s\t%(days)s\t%(last)s\t%(nexttime)s\t%(errcount)s\t%(status)s\n"%account
class StaticFile:
def GET(self,filename):
path = "static/%s"%filename
if os.path.exists(path):
return file(path,"rb")
return "404"
def checkin(account):
user = User()
user.loadCookie(account["cookie"])
if not user.islogined and account["password"]:
user.login(account["email"],account["password"])
if user.islogined:
if not user.ischeckined:
days = user.checkin()
if days:
account["days"] = days
else:
account["errcount"] += 1
account["nexttime"] = time.time() + 600
account["status"] = u'\u7b7e\u5230\u5931\u8d25'
else:
days = int(user["sign"]["persist_num"])
account["nexttime"] = time.time() + 3600
account["status"] = u'\u6ca1\u6709\u5f02\u5e38'
else:
account["errcount"] += 1
account["nexttime"] = time.time() + 600
account["status"] = u'\u767b\u5f55\u5931\u8d25'
account["last"] = time.time()
xiamidb.update (**account)
def daemon():
while True:
accounts = xiamidb.scan(where="errcount<3 and nexttime<%s"%time.time())
for account in accounts:
try:
checkin(account)
except:
traceback.print_exc()
print "checkin error"
time.sleep(5)
time.sleep(5)
urls = (
"/", "Main",
"/Ctrl","Ctrl",
"/(.*)","StaticFile",
)
def serve_forever():
gevent.spawn(daemon)
print web
application = web.application(urls, globals()).wsgifunc()
print 'Serving on 8888'
server = gevent.pywsgi.WSGIServer(('', 8888), application)
server.serve_forever()
if __name__ == "__main__":
serve_forever()