-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcronwork.py
76 lines (73 loc) · 2.52 KB
/
cronwork.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#任务计划实现
from pytz import utc
from apscheduler.schedulers.background import BackgroundScheduler
sched = BackgroundScheduler(timezone=utc)
from Page.UserPage import UserPage
from Page.Request import Request
from Page.SearchPage import SearchPage
from Model import *
from pony.orm import *
import time
import Config
import Cache
import re
#fresh borrowed books
def cronwork1():
print('-START- fresh users\' borrowed books')
users = []
with db_session:
users = select(u for u in User)[:]
for u in users:
if u.renew_flag:
request = Request().setCookie(u.student_id, u.password)
with db_session:
u = User.get(student_id=u.student_id)
p = UserPage(u.student_id, u.password)
books = p.fetch().parse()['books']
[r.delete() for r in u.readings]
for b in books:
name = b[1].decode('utf-8')
code = b[0]
borrow_time = b[2]
due_time = b[3]
renewed = False if b[4]=='0' else True
renew_link = b[5]
index_code = b[6]
address = b[7].decode('utf-8')
#AutoRenew
if u.renew_flag:
timestamp = time.mktime(time.strptime(due_time, '%Y-%m-%d'))
if timestamp - time.time() < float(Config.get('auto_renew_time')) and renewed==False:
request.get(Config.get('lib_base_url')+renew_link)
renewed == True
#SaveData
u.readings.create(
name = name,
code = code,
borrow_time = borrow_time,
due_time = due_time,
renewed = renewed,
renew_link = renew_link,
index_code = index_code,
address = address
)
print('-STOP- fresh users\' borrowed books')
#fresh cache
def cronwork2():
print('-START- fresh cache')
r = Cache.r
keys = r.keys('search:*')
for i in keys:
reg = '^search:(.*):(\d+)$'
result = re.findall(reg, i)[0]
r.delete(i)
print(result)
print(SearchPage().fetch(result[0], result[1])._html)
print('-STOP- fresh cache')
sched.add_job(cronwork1, 'interval', hours=5)
sched.add_job(cronwork2, 'interval', days=10)
def start():
sched.start()
print 'cronwork started'