From caea440c903f1fe19779d2cf6eedd81dc1d0e5f2 Mon Sep 17 00:00:00 2001 From: pooneyy <85266337+pooneyy@users.noreply.github.com> Date: Wed, 14 Feb 2024 15:09:09 +0800 Subject: [PATCH] Update 1.4 --- README.md | 9 +++-- index.py | 106 +++++++++++++++++++++++++++++------------------------- 2 files changed, 63 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 32af430..217b2c5 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,9 @@ 现有一个已部署的demo,使用微信扫码,也可接收消息推送: -showqrcode +showqrcode -这个二维码**有效期至2024年2月20日** +这个二维码**有效期至2024年3月14日** ### 使用说明 @@ -32,7 +32,10 @@ python index.py ### 更新日志 ``` -1.3.1 (2024.02.02) 修复:修复了无法登录的问题 +1.4 (2024.02.14) 更新:优化登录方式,配置文件不再保存账号密码,提高安全性。 + 配置文件版本更新,版本不一致将会重新创建配置文件,请提前备份。 + +1.3.1 (2024.02.02) 修复:修复了无法登录的问题。 1.3 (2024.01.25) 修复: 1、修复了表格显示错误; diff --git a/index.py b/index.py index 3d7529a..84f2a60 100644 --- a/index.py +++ b/index.py @@ -4,7 +4,7 @@ Author : Pooneyy Date : 2023-03-14 20:08:52 LastEditors : Pooneyy 85266337+pooneyy@users.noreply.github.com -LastEditTime : 2023-03-31 21:48:32 +LastEditTime : 2024-02-14 14:56:29 FilePath : /suanleme/index.py Description : “算了么”平台 监听任务列表 @@ -17,24 +17,29 @@ import sys import requests -VERSION = '1.3.1' # 2024.02.02 -CONFIG_VERSION = 2 +VERSION = '1.4' # 2024.02.14 +CONFIG = {} +CONFIG_VERSION = 3 HOST = "https://api.suanleme.cn/api/v1" +REFRESH_TOKEN = '' def timeStamp_To_dateTime(timeStamp):return datetime.datetime.fromtimestamp(int(timeStamp), pytz.timezone('Asia/Shanghai')).strftime('%Y-%m-%d %H:%M:%S') def isoDateTime_To_dateTime(iso_date_time):return datetime.datetime.strptime(iso_date_time, "%Y-%m-%dT%H:%M:%S.%f%z").strftime('%Y-%m-%d
%H:%M:%S') -def saveConfig(config): - config['tasks_record'] = dict(sorted(config['tasks_record'].items(), reverse=True)) +def saveConfig(): + CONFIG['refresh_token'] = REFRESH_TOKEN + CONFIG['tasks_record'] = dict(sorted(CONFIG['tasks_record'].items(), reverse=True)) with open("config.json", "w", encoding='utf8') as file: - json.dump(config, file, ensure_ascii=False, indent = 4) + json.dump(CONFIG, file, ensure_ascii=False, indent = 4) def loadConfig(): + global CONFIG, REFRESH_TOKEN with open("config.json", "r+", encoding='utf8') as file: - config = json.load(file) - config['tasks_record'] = {int(key):config['tasks_record'][key] for key in config['tasks_record']} - return config + CONFIG = json.load(file) + if CONFIG['config_version'] != CONFIG_VERSION:raise FileNotFoundError + CONFIG['tasks_record'] = {int(key):CONFIG['tasks_record'][key] for key in CONFIG['tasks_record']} + REFRESH_TOKEN = CONFIG['refresh_token'] def checkUpdate(): url = "https://api.github.com/repos/pooneyy/suanleme/releases/latest" @@ -45,52 +50,54 @@ def checkUpdate(): except:return False def init(): - print('首次运行初始化') - config = {} - config['config_version'] = CONFIG_VERSION - config['latest_id'] = 0 - config['suanleme_username'] = input('请输入算了么账号:') - config['suanleme_password'] = input('请输入算了么密码:') - config['truecaptcha_userid'] = '' - config['truecaptcha_apikey'] = '' - config['pushplus_token'] = input('请输入pushplus推送加的token:') - config['pushplus_topic'] = input('(选填)输入pushplus推送加的群组编码:') - config['tasks_record'] = {} - saveConfig(config) + global CONFIG, REFRESH_TOKEN + print('初始化配置文件') + CONFIG['config_version'] = CONFIG_VERSION + CONFIG['latest_id'] = 0 + CONFIG['refresh_token'] = '' + account = input('请输入算了么账号:') + password = input('请输入算了么密码:') + CONFIG['pushplus_token'] = input('请输入pushplus推送加的token:') + CONFIG['pushplus_topic'] = input('(选填)输入pushplus推送加的群组编码:') + CONFIG['tasks_record'] = {} + login(account, password) + saveConfig() print('初始化完成,请重新运行') -def sendMsg(config, msg): +def sendMsg(msg): data = {} url = "http://www.pushplus.plus/send" - data['token'] = config['pushplus_token'] + data['token'] = CONFIG['pushplus_token'] data['title'] = '算了么来新单啦!' data['template'] = 'html' - data['topic'] = config['pushplus_topic'] # 群组编码,发送群组消息。 + data['topic'] = CONFIG['pushplus_topic'] # 群组编码,发送群组消息。 data['content'] = msg response = requests.post(url,data=data) return response.text -def login(config): +def login(account, password): + global REFRESH_TOKEN url = f"{HOST}/user/token" payload={ - 'account': config['suanleme_username'], - 'password': config['suanleme_password'], + 'account': account, + 'password': password, } response = requests.post(url, data=payload) print(f"{timeStamp_To_dateTime(time.time())}\t登录成功") - refresh_token = response.json()['refresh'] - return refresh_token + REFRESH_TOKEN = response.json()['refresh'] -def refresh(refresh_token): +def refresh(): + global REFRESH_TOKEN url = f"{HOST}/user/token/refresh" - payload={'refresh': refresh_token} + payload={'refresh': REFRESH_TOKEN} response = requests.post(url, data=payload) + REFRESH_TOKEN = response.json()['refresh'] return response.json()["access"] -def getTasks(refresh_token): +def getTasks(): '''获取全部订单''' url = f"{HOST}/tasks/?page=1&data_type=all" - headers = {'Authorization': f'Bearer {refresh(refresh_token)}'} + headers = {'Authorization': f'Bearer {refresh()}'} response = requests.get(url, headers=headers) response.close() return response.json() @@ -155,37 +162,36 @@ def taskList_To_Msg(taskList): msg += "" return msg -def updated_Tasks_To_Msg(tasksList,config): +def updated_Tasks_To_Msg(tasksList): msg = r'''
''' for task in tasksList: - msg += f'''''' + msg += f'''''' msg += "
任务ID任务点更新前任务点更新后
{task['id']}{config.get('tasks_record').get(task['id'],{}).get('points',0)}{task['points']}
{task['id']}{CONFIG.get('tasks_record').get(task['id'],{}).get('points',0)}{task['points']}
" return msg -def loop(config): +def loop(): try: - refresh_token = login(config) while True: - tasksList = analyzing_tasks_info(getTasks(refresh_token)) + tasksList = analyzing_tasks_info(getTasks()) if tasksList: i = [i['id'] for i in tasksList] # 列表,临时存储订单ID用于寻找最大的ID latest_id = int(max(i)) msg = '' - if latest_id > config['latest_id']: - config['latest_id'] = latest_id + if latest_id > CONFIG['latest_id']: + CONFIG['latest_id'] = latest_id msg += f"

有新订单。当前最新是 {latest_id}

" msg += taskList_To_Msg(tasksList) print(f"{taskList_To_Msg(tasksList)}") updated_tasks = [] for task in tasksList: - if task['points'] > config.get('tasks_record').get(task['id'],{}).get('points',0):updated_tasks.append(task) + if task['points'] > CONFIG.get('tasks_record').get(task['id'],{}).get('points',0):updated_tasks.append(task) if updated_tasks: msg += f"

有新任务点

" - msg += updated_Tasks_To_Msg(updated_tasks,config) - print(f"{updated_Tasks_To_Msg(updated_tasks,config)}") - for task in tasksList:config['tasks_record'][task['id']] = task - if latest_id > config['latest_id'] or updated_tasks:sendMsg(config, msg) - saveConfig(config) + msg += updated_Tasks_To_Msg(updated_tasks) + print(f"{updated_Tasks_To_Msg(updated_tasks)}") + for task in tasksList:CONFIG['tasks_record'][task['id']] = task + if latest_id > CONFIG['latest_id'] or updated_tasks:sendMsg(msg) + saveConfig() print(f"{timeStamp_To_dateTime(time.time())}\t当前最新{latest_id}...\r",end='') else: print(f"{timeStamp_To_dateTime(time.time())}\t当前没有订单...\r",end='') @@ -195,18 +201,20 @@ def loop(config): try: print(f"{timeStamp_To_dateTime(time.time())}\t网络连接中断") time.sleep(30) - loop(config) + loop() except KeyboardInterrupt:print("结束") except requests.exceptions.ChunkedEncodingError: print(f"{timeStamp_To_dateTime(time.time())}\t远程主机关闭连接") time.sleep(3) - loop(config) + loop() def main(): if 'linux' in sys.platform: sys.stdout.write(f"\x1b]2;算了么 - 监听任务列表 - 版本 {VERSION}\x07") elif 'win' in sys.platform: os.system(f"title 算了么 - 监听任务列表 - 版本 {VERSION}") if checkUpdate():print("请更新到最新版本:https://github.com/pooneyy/suanleme/releases/latest \n") - try:loop(loadConfig()) + try: + loadConfig() + loop() except FileNotFoundError: try:init() except KeyboardInterrupt:print("\n退出,取消初始化")