Skip to content

Commit

Permalink
更新优化支持新版查询
Browse files Browse the repository at this point in the history
  • Loading branch information
x29230 committed Jul 6, 2024
1 parent 7fd88fc commit 32860dc
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 268 deletions.
10 changes: 2 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
# 根目录下的部分数据文件
.idea/

# 新版配置文件
/config.json

# 旧版配置文件
/config.yml

# 旧版数据源文件
/source.txt
/config.json
283 changes: 119 additions & 164 deletions __init__.py

Large diffs are not rendered by default.

16 changes: 0 additions & 16 deletions config.yml

This file was deleted.

15 changes: 15 additions & 0 deletions data_source.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"current": "layvtwt.top",
"source_list": {
"layvtwt.top": {
"api": "https://rank.layvtwt.top/api",
"domain": "rank.layvtwt.top",
"remarks": "目前唯一可用数据源"
},
"infedg.xyz": {
"api": "https://kyaru.infedg.xyz",
"domain": "kyaru.infedg.xyz",
"remarks": "目前不可用,仅作留档"
}
}
}
46 changes: 12 additions & 34 deletions lock.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,21 @@
import yaml
import json
import os

# 将数据移至新配置文件
async def move_config(current_dir, old_con_dir):
# 读取旧数据
with open(old_con_dir, 'r', encoding='UTF-8') as f:
file_data = f.read()
config = yaml.load(file_data, Loader=yaml.FullLoader)
data = {}
for server in range(1, 5):
server = str(server)
for user in config[server]:
if user['user']['clan_name'] != '样例:公会名字':
group_id = str(user['user']['group_id'])
data[group_id] = {}
data[group_id]['server'] = server
data[group_id]['clan_name'] = user['user']['clan_name']
# 写入新数据
with open(current_dir, 'r', encoding='UTF-8') as af:
f_data = json.load(af)
f_data['bind'] = data
with open(current_dir, 'w', encoding='UTF-8') as f:
json.dump(f_data, f, indent=4, ensure_ascii=False)
# 删除旧数据文件
os.remove(os.path.join(os.path.dirname(__file__), 'config.yml'))
os.remove(os.path.join(os.path.dirname(__file__), 'source.txt'))

# 绑定公会
async def lock_clan(server, clan_name, group_id):
current_dir = os.path.join(os.path.dirname(__file__), 'config.json')
with open(current_dir, 'r', encoding='UTF-8') as af:
f_data = json.load(af)
f_data['bind'][group_id] = {}
f_data['bind'][group_id]['server'] = server
f_data['bind'][group_id]['clan_name'] = clan_name
f_data[group_id] = {}
f_data[group_id]['server'] = server
f_data[group_id]['clan_name'] = clan_name
with open(current_dir, 'w', encoding='UTF-8') as f:
json.dump(f_data, f, indent=4, ensure_ascii=False)
msg = f'QQ群:{group_id} 已成功绑定{server}服公会“{clan_name}”'
return msg


# 多个绑定选择触发
async def select_all_clan(clan_score):
num = clan_score['total']
Expand All @@ -50,28 +26,30 @@ async def select_all_clan(clan_score):
msg = msg + '\n' + str(num_id + 1) + '. ' + str(clan_name)
return msg


# 解绑公会
async def unlock_clan(group_id):
current_dir = os.path.join(os.path.dirname(__file__), 'config.json')
with open(current_dir, 'r', encoding='UTF-8') as af:
f_data = json.load(af)
clan_name = f_data['bind'][group_id]['clan_name']
f_data['bind'].pop(group_id)
clan_name = f_data[group_id]['clan_name']
f_data.pop(group_id)
with open(current_dir, 'w', encoding='UTF-8') as f:
json.dump(f_data, f, indent=4, ensure_ascii=False)
msg = f'QQ群:{group_id} 已成功解绑公会"{clan_name}"'
return msg


# 查询公会绑定
async def judge_lock(group_id):
current_dir = os.path.join(os.path.dirname(__file__), 'config.json')
with open(current_dir, 'r', encoding='UTF-8') as af:
f_data = json.load(af)
if group_id in list(f_data['bind'].keys()):
server = f_data['bind'][group_id]['server']
clan_name = f_data['bind'][group_id]['clan_name']
if group_id in list(f_data.keys()):
server = f_data[group_id]['server']
clan_name = f_data[group_id]['clan_name']
msg = f'本群:{group_id} 已成功绑定{server}服公会“{clan_name}”'
return msg, True
else:
msg = f'本群:{group_id} 暂未绑定任何公会'
return msg, False
return msg, False
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
prettytable
Pillow
requests
pyyaml
requests
82 changes: 39 additions & 43 deletions search.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,73 +7,69 @@
from prettytable import PrettyTable


# 设置源
async def set_source(source_id):
if source_id == '1':
source = 'infedg.xyz'
elif source_id == '2':
source = 'layvtwt.top'
current_dir = os.path.join(os.path.dirname(__file__), 'config.json')
with open(current_dir, 'r', encoding='UTF-8') as af:
f_data = json.load(af)
f_data['source'] = source
# 设置当前源
async def set_source(source_name: str) -> bool:
f_data = await get_source()
source_list = dict(f_data['source_list'])
if source_name not in source_list:
return False

f_data['current'] = source_name
current_dir = os.path.join(os.path.dirname(__file__), 'data_source.json')
with open(current_dir, 'w', encoding='UTF-8') as f:
json.dump(f_data, f, indent=4, ensure_ascii=False)
return source
return True


# 获取源
async def get_source():
current_dir = os.path.join(os.path.dirname(__file__), 'config.json')
async def get_source() -> dict:
current_dir = os.path.join(os.path.dirname(__file__), 'data_source.json')
with open(current_dir, 'r', encoding='UTF-8') as af:
f_data = json.load(af)
source = f_data['source']
return source
return f_data


# 获取当前的数据源详情
async def get_source_detail(f_data: dict) -> dict:
current = f_data['current']
source_list = f_data.get('source_list', {})
return source_list.get(current, {})


# 通用头
async def get_headers():
source = await get_source()
if source == 'infedg.xyz':
source = 'kyaru.' + source
elif source == 'layvtwt.top':
source = 'rank.' + source
async def get_headers(f_data: dict) -> dict:
source_detail = await get_source_detail(f_data)
headers = {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'Custom-Source': 'Kyaru',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36',
'Content-Type': 'application/json',
'Origin': f'https://{source}',
'Referer': f'https://{source}/'
'Origin': f'https://{source_detail["domain"]}',
'Referer': f'https://{source_detail["domain"]}/'
}
return headers


# 获取最新数据的时间档
async def get_current_time(server, source):
if source == 'infedg.xyz':
source = 'api.' + source
elif source == 'layvtwt.top':
source = 'rank.' + source + '/api'
url = f'https://{source}/current/getalltime/tw'
time_tmp = await aiorequests.get(url, headers=await get_headers(), timeout=10)
async def get_current_time(server, f_data):
source_detail = await get_source_detail(f_data)
url = source_detail['api'] + '/current/getalltime/tw'
time_tmp = await aiorequests.get(url, headers=await get_headers(f_data), timeout=10)
alltime = await time_tmp.json()
alldays = alltime['data'][server].keys()
upday = list(alldays)[-1]
uphour = list(alltime['data'][server][upday])[-1]
uptime = str(upday) + str(uphour)
return uptime
all_days = alltime['data'][server].keys()
up_day = list(all_days)[-1]
up_hour = list(alltime['data'][server][up_day])[-1]
up_time = str(up_day) + str(up_hour)
return up_time


# 返回查询信息
async def get_search_rank(server, uptime, source, search_type=None, search_param=''):
if source == 'infedg.xyz':
source = 'api.' + source
elif source == 'layvtwt.top':
source = 'rank.' + source + '/api'
url = f'https://{source}/search/{search_type}'
async def get_search_rank(server: str, uptime: str, f_data: dict, search_type: str = None, search_param: str = ''):
source_detail = await get_source_detail(f_data)
url = source_detail['api'] + '/search/' + search_type
file_tmp = 'tw/' + str(server) + '/' + str(uptime)
filename_tmp = 'tw-' + str(server) + '-' + str(uptime) + '-' + str(search_param) + '.png'
params = {
Expand All @@ -82,7 +78,7 @@ async def get_search_rank(server, uptime, source, search_type=None, search_param
'page': 0,
'page_limit': 10
}
clan_score_tmp = await aiorequests.post(url, headers=await get_headers(), json=params, timeout=10)
clan_score_tmp = await aiorequests.post(url, headers=await get_headers(f_data), json=params, timeout=10)
clan_score = await clan_score_tmp.json()
return clan_score, filename_tmp

Expand Down
1 change: 0 additions & 1 deletion source.txt

This file was deleted.

0 comments on commit 32860dc

Please sign in to comment.