wuhu-torn-helper/fstock.py

119 lines
3.6 KiB
Python

import json
from datetime import datetime, timezone, timedelta
import os
import requests
DEST_ARR = ['mex', 'cay', 'can', 'haw', 'uni', 'arg', 'swi', 'jap', 'chi', 'uae', 'sou']
DEST_TRANS = {
'mex': '', 'cay': '', 'can': '',
'haw': '', 'uni': '', 'arg': '',
'swi': '', 'jap': '', 'chi': '', 'uae': '', 'sou': '',
}
VALUED_ITEM_ARR = [
'Dahlia', 'Jaguar Plushie',
'Banana Orchid', 'Stingray Plushie',
'Crocus', 'Wolverine Plushie',
'Orchid', 'Large Suitcase',
'Red Fox Plushie', 'Nessie Plushie', 'Heather',
'Ceibo Flower', 'Monkey Plushie', 'Tear Gas',
'Chamois Plushie', 'Edelweiss',
'Cherry Blossom',
'Panda Plushie', 'Peony',
'Camel Plushie', 'Tribulus Omanense',
'Lion Plushie', 'African Violet', 'Xanax',
]
ITEM_TRANS = {
'Dahlia': '', 'Jaguar Plushie': '',
'Banana Orchid': '', 'Stingray Plushie': '',
'Crocus': '', 'Wolverine Plushie': '',
'Orchid': '', 'Large Suitcase': '大箱',
'Red Fox Plushie': '赤狐', 'Nessie Plushie': '水怪', 'Heather': '',
'Ceibo Flower': '', 'Monkey Plushie': '', 'Tear Gas': '催泪弹',
'Edelweiss': '', 'Chamois Plushie': '',
'Cherry Blossom': '',
'Peony': '', 'Panda Plushie': '',
'Tribulus Omanense': '', 'Camel Plushie': '',
'African Violet': '', 'Lion Plushie': '', 'Xanax': 'XAN',
}
HISTORY_FILE = 'stock_hist.json'
HISTORY_EXIST = os.path.exists(HISTORY_FILE)
UTC_8 = timezone(timedelta(hours=8))
URL = "https://yata.yt/api/v1/travel/export/"
resObj = requests.get(URL).json()["stocks"]
# 更新时间
lastUpdate = 0
for dest in DEST_ARR:
res_update = resObj[dest]["update"]
if lastUpdate < res_update:
lastUpdate = res_update
lastUpdate = datetime.fromtimestamp(lastUpdate).astimezone(UTC_8)
UPDATE_TIME_STR = lastUpdate.strftime('%H:%M:%S')
# 整理过的对象
stockObj = {}
for dest in DEST_ARR:
arr = []
for f_item in resObj[dest]['stocks']:
for v_item in VALUED_ITEM_ARR:
if f_item['name'] == v_item:
arr.append(f_item)
stockObj[dest] = arr
# 移除亏本的xan
# 加拿大
index = -1
for i in range(len(stockObj['can'])):
if stockObj['can'][i]['name'] == 'Xanax':
index = i
if index != -1:
del stockObj['can'][index]
# 英国
index = -1
for i in range(len(stockObj['uni'])):
if stockObj['uni'][i]['name'] == 'Xanax':
index = i
if index != -1:
del stockObj['uni'][index]
# 日本
index = -1
for i in range(len(stockObj['jap'])):
if stockObj['jap'][i]['name'] == 'Xanax':
index = i
if index != -1:
del stockObj['jap'][index]
# 读取库存历史
stockHistObj = {}
if HISTORY_EXIST:
print('历史文件存在')
with open(HISTORY_FILE, 'r') as load_f:
stockHistObj = json.load(load_f)
else:
stockHistObj['json'] = []
stockHistObj['json'].append({str(int(datetime.now().timestamp())): stockObj})
if len(stockHistObj['json']) > 100:
del stockHistObj['json'][0]
with open(HISTORY_FILE, 'w') as f:
json.dump(stockHistObj, f)
hist_arr = stockHistObj['json']
# 用于写入文件的列表
seq = ["更新时间: %s\n" % UPDATE_TIME_STR]
for dest in DEST_ARR:
seq_str = '%s ' % DEST_TRANS[dest]
for i in range(len(stockObj[dest])):
seq_str += '%s%s' % (ITEM_TRANS[stockObj[dest][i]['name']], stockObj[dest][i]['quantity'])
if i != len(stockObj[dest]) - 1:
seq_str += ' '
seq_str += '\n'
seq.append(seq_str)
# 写入文件
fo = open(file="stock.txt", mode="w", encoding="utf-8")
fo.writelines(seq)
fo.close()
print("花偶库存更新完成")