利用书蜗自动续借图书

本文最后更新于:4 年前

女票经常在图书馆借书, 蓝鹅她经常逾期忘还, 正好分析了书蜗, 顺手一写.

准备

  1. Python2.7
  2. requests (pip install requests)
  3. yagmail (pip install yagmail)

分析

书蜗分析
大同小异, 不赘述了

代码

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
# -*- coding:gbk -*- 
import requests
import pprint
import time,os
import yagmail

class Sw():
def __init__(self, username, password):
self.username = username
self.password = password
self.host = 'http://t1.beijingzhangtu.com'


def Login(self):
loginPayload = {
'phone': self.username,
'pass': self.password
}

login_json = requests.post(self.host+'/api/user/loginByPhone.html', data=loginPayload).json() #登录

assert int(login_json['code']), login_json['msg']
print login_json['msg']

self.userId = login_json['data']['id']
self.libraryId = login_json['data']['libraries'][0]['id']
self.token = login_json['data']['token']

def getStuInfo(self):
page = '1'
ListPayload = {
'page': page,
'userId': self.userId,
'libraryId': self.libraryId,
'token': self.token
}
List_json = requests.post(self.host+'/api/userLibrary/getList.html', data=ListPayload).json() #学生信息
studentName = List_json['data'][0]['cardusername']
print studentName, '你好'

def getBorrowList(self):
self.books = {}
page = '1'
BorrowPayload = {
'userId': self.userId,
'libraryId': self.libraryId,
'token': self.token,
'page': page
}
Borrow_json = requests.post(self.host+'/api/borrow/getList.html', data=BorrowPayload).json() #在借图书
for book in Borrow_json['data']:
print '-' * 20
print '书名:', book['name']
print '借书日期', book['loan_DATE']
print '距离还书还有', book['surplus_DAYS'], '天'

self.books[book['name']] = [book['bar_NUMBER'], book['surplus_DAYS']]

#pprint.pprint(self.books)

def RenewSome(self, BookList):
self.getBorrowList()

for book in BookList:
RenewSomePayload = {
'bookName': book,
'barNumber': self.books[book][0],
'libraryId': self.libraryId,
'userId': self.userId,
'token': self.token,
}
RenewSome_json = requests.post(self.host+'/api/borrow/continue.html', data=RenewSomePayload).json() #续借
#print '-' * 20
#print '书名:', book
#print '续借结果:', RenewSome_json['msg']


def RenewAll(self):
self.getBorrowList()
content = ''
for book in self.books:
if self.books[book][1] == '0':
RenewAllPayload = {
'bookName': book,
'barNumber': self.books[book][0],
'libraryId': self.libraryId,
'userId': self.userId,
'token': self.token,
}
RenewAll_json = requests.post(self.host+'/api/borrow/continue.html', data=RenewAllPayload).json() #续借
#print '-' * 20
#print '书名:', book
#print '续借结果:', RenewAll_json['msg']

content += '-' * 20 + '\n书名: ' + book + '\n续借结果: ' + RenewAll_json['msg'] + '\n'

else:
content += '-' * 20 + '\n书名: ' + book + '\n续借结果: ' + '距离自动续借还有' + self.books[book][1] + '天' + '\n'

return '你一共借了 ' + str(len(self.books)) + '本书\n' + content

def autoRenewAll(self):
while 1:
sw.Login() #防止 token 失效
content = self.RenewAll()
print '-' * 20
print content
self.Email(content.encode('utf8'))
time.sleep(86400) #一天
os.system('cls')

def Email(self, content):
yag = yagmail.SMTP(user = '发送方邮箱', password = '发送方邮箱密码', host = '邮箱 host', port = '25') #如 139 邮箱 host 是 smtp.139.com
yag.send(to = "接收方邮箱", subject = "Troy's Sw_Spider", contents = content)
print 'Email successfully!'


username = '***********' #账号
password = '*********' #密码
#BookList = ['应用MATLAB实现神经网络', 'PHP、MySQL与JavaScript学习手册']

#BookList = [book.decode('gbk') for book in BookList] #记得进行编码
sw = Sw(username, password)
sw.Login() #登录
#sw.getStuInfo() #获取学生信息
#sw.getBorrowList()
#sw.RenewSome(BookList)
#sw.RenewAll()
#sw.autoRenewAll()

使用说明

和书蜗抢座那篇差不多

  1. 首先创建一个对象
    sw = Sw(username, password)
  2. 续借某几本书的时候会用到 BookList, 要记得进行编码
  3. 基本法规定, 最多续到 44 天
  4. 续到 44 天还想续? 服务器会返回 New due date must be bigger than current's loan due date. 续还是要按照基本法啊

获取在借图书

  1. Login()
  2. getBorrowList()

续借某几本书

  1. Login()
  2. RenewSome(BookList)

续借所有书

  1. Login()
  2. RenewAll()

自动续借所有书

调用 autoRenewAll() 即可

续借完了会发邮件到指定邮箱.
如果只想自动续借某几本, 调用 RenewSome(BookList) 就行

测试一下

结果
截图
Emmmm…..这 2 个截图没有对应, 反正就长这样, 懒得再截图了.

后语

之前还书的时候欠了 30 多块, Emmmmmmm…
查了一下南馆的续借规则
续借规则
只能续 2 次. 看来要在最后一天续借比较划算. Emmmmmmm…

来呀快活呀


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!