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
| 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']]
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()
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()
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() 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') yag.send(to = "接收方邮箱", subject = "Troy's Sw_Spider", contents = content) print 'Email successfully!'
username = '***********' password = '*********'
sw = Sw(username, password) sw.Login()
|