-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
63 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
Author : Pooneyy | ||
Date : 2023-03-14 20:08:52 | ||
LastEditors : Pooneyy [email protected] | ||
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<br />%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 += "</table></div>" | ||
return msg | ||
|
||
def updated_Tasks_To_Msg(tasksList,config): | ||
def updated_Tasks_To_Msg(tasksList): | ||
msg = r'''<div class="table-container"><table class="new-points"><tr><th class="other-column">任务ID</th><th class="other-column">任务点更新前</th><th class="other-column">任务点更新后</th></tr>''' | ||
for task in tasksList: | ||
msg += f'''<tr><td class="other-column">{task['id']}</td><td class="other-column">{config.get('tasks_record').get(task['id'],{}).get('points',0)}</td><td class="other-column">{task['points']}</td></tr>''' | ||
msg += f'''<tr><td class="other-column">{task['id']}</td><td class="other-column">{CONFIG.get('tasks_record').get(task['id'],{}).get('points',0)}</td><td class="other-column">{task['points']}</td></tr>''' | ||
msg += "</table></div>" | ||
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 = '<style>.table-container {overflow-x: auto;}table {width: 100%;border-collapse: collapse;}.new-tasks {min-width: 700px;}.new-points {min-width: 100px;}th, td {border: 1px solid black;padding: 8px;text-align: left;}.detail-time {width: 50px;word-wrap: break-word;}.other-column {width: 10px;word-wrap: break-word;}</style>' | ||
if latest_id > config['latest_id']: | ||
config['latest_id'] = latest_id | ||
if latest_id > CONFIG['latest_id']: | ||
CONFIG['latest_id'] = latest_id | ||
msg += f"<h4>有新订单。当前最新是 {latest_id}</h4>" | ||
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"<h4>有新任务点</h4>" | ||
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退出,取消初始化") | ||
|