利用书蜗自动续借图书

Author Avatar
Tr0y 8月 09, 2017 21:53:15 本文共 986 字
  • 文为知己者书
  • 在其它设备中阅读本文章

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

准备

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

分析

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

代码

# -*- 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…

End

What do you think?

本文标题: 利用书蜗自动续借图书
原始链接: http://www.tr0y.wang/2017/08/09/SwRenew/
发布时间: 2017.08.09-21:53
最后更新: 2018.11.03-21:01
版权声明: 本站文章均采用CC BY-NC-SA 4.0协议进行许可。转载请注明出处!