Skip to content
This repository has been archived by the owner on Mar 3, 2022. It is now read-only.

Commit

Permalink
新增 支持扫码登录
Browse files Browse the repository at this point in the history
  • Loading branch information
Mufanc committed Jul 20, 2021
1 parent 7f19877 commit d62df84
Showing 1 changed file with 51 additions and 8 deletions.
59 changes: 51 additions & 8 deletions flash.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import sys
import json
import requests
import tkinter as tk
from io import BytesIO
from loguru import logger
from random import randint
from time import time, sleep
from PIL import Image, ImageTk

logger.remove()
logger.add(sys.stdout, format='<level>[{time:HH:MM:SS}] {message}</level>')
Expand All @@ -14,8 +17,8 @@ def timestamp():


class WeibanClient(object):
def __init__(self, userinfo):
self.userinfo = userinfo
def __init__(self):
self.userinfo = {}
self.session = requests.session()
self.current_request = None
self.session.headers.update({
Expand All @@ -24,6 +27,38 @@ def __init__(self, userinfo):
})
self.last_choice = {}

def login_with_qrcode(self):
logger.debug('正在获取二维码...')
qrcode = self.session.get('https://weiban.mycourse.cn/pharos/login/genBarCodeImageAndCacheUuid.do', params={
'timestamp': timestamp()
}).json()['data']
image = Image.open(BytesIO(self.session.get(qrcode['imagePath']).content))
logger.debug('请扫码完成登录')
root = tk.Tk()
root.title('扫码登录后关闭此窗口')
image = ImageTk.PhotoImage(image)
tk.Label(root, image=image).pack()
root.lift()
root.attributes('-topmost', True)
root.after_idle(root.attributes, '-topmost', False)
root.mainloop()
sleep(3)
result = self.post('https://weiban.mycourse.cn/pharos/login/barCodeWebAutoLogin.do', {
'barCodeCacheUserId': qrcode['barCodeCacheUserId']
})['data']
self.userinfo = {key: result[key] for key in result if key in ('token', 'userId', 'tenantCode')}
logger.warning('登录成功!')

def login_with_password(self):
pass

def login_manually(self):
logger.debug('将下列内容复制到浏览器地址栏并按回车:')
print('javascript:(function(){data=JSON.parse(localStorage.user);prompt(\'\',JSON.stringify({token:data['
'\'token\'],userId:data[\'userId\'], tenantCode:data[\'tenantCode\']}));})();')
logger.warning('(注意某些浏览器会把开头的“javascript:”吞掉,如果粘贴后发现没有请自行补上)')
self.userinfo = json.loads(input('把弹窗显示的内容粘贴到这里:'))

def post(self, url, data={}):
return self.session.post(url, params={'timestamp': timestamp()}, data={
**self.userinfo,
Expand Down Expand Up @@ -140,13 +175,19 @@ def main():
logger.warning('使用说明:在电脑浏览器访问 https://weiban.mycourse.cn,然后选择右侧「账号登录」进行登录')
logger.debug('登录后先完成初始的 10 题考试(如果没有请自动忽略)')
logger.info('按回车键继续...'), input()
logger.debug('将下列内容复制到浏览器地址栏并按回车:')
print('javascript:(function(){data=JSON.parse(localStorage.user);prompt(\'\',JSON.stringify({token:data['
'\'token\'],userId:data[\'userId\'], tenantCode:data[\'tenantCode\']}));})();')
logger.warning('(注意某些浏览器会把开头的“javascript:”吞掉,如果粘贴后发现没有请自行补上)')
userinfo = json.loads(input('把弹窗显示的内容粘贴到这里:'))

client = WeibanClient(userinfo)
logger.debug('请选择登录方式:')
for i, method in enumerate(('扫码登录(默认)', '手动登录')):
logger.info(f'{i+1}: {method}')
client = WeibanClient()
method = int(input() or '1')
if method == 1:
client.login_with_qrcode()
elif method == 2:
client.login_manually()
else:
logger.error('What?')
exit()
for trial in range(10):
try:
client.flash()
Expand All @@ -162,5 +203,7 @@ def main():
if __name__ == '__main__':
try:
main()
except Exception:
logger.warning('Exiting...')
except KeyboardInterrupt:
logger.warning('Exiting...')

0 comments on commit d62df84

Please sign in to comment.