V2RayAggregator/utils/list_update.py
2022-09-13 13:55:58 +08:00

111 lines
4.0 KiB
Python

#!/usr/bin/env python3
from datetime import timedelta, datetime
import json, re
import requests
from requests.adapters import HTTPAdapter
# 文件路径定义
sub_list_json = './sub/sub_list.json'
with open(sub_list_json, 'r', encoding='utf-8') as f: # 载入订阅链接
raw_list = json.load(f)
f.close()
def url_updated(url): # 判断远程远程链接是否已经更新
s = requests.Session()
s.mount('http://', HTTPAdapter(max_retries=2))
s.mount('https://', HTTPAdapter(max_retries=2))
try:
resp = s.get(url, timeout=2)
status = resp.status_code
except Exception:
status = 404
if status == 200:
url_updated = True
else:
url_updated = False
return url_updated
class update_url():
def update_main():
for sub in raw_list:
id = sub['id']
current_url = sub['url']
try:
if sub['update_method'] != 'auto' and sub['enabled'] == True:
print(f'Finding available update for ID{id}')
if sub['update_method'] == 'change_date':
new_url = update_url.change_date(id,current_url)
if new_url == current_url:
print(f'No available update for ID{id}\n')
else:
sub['url'] = new_url
print(f'ID{id} url updated to {new_url}\n')
elif sub['update_method'] == 'page_release':
new_url = update_url.find_link(id,current_url)
if new_url == current_url:
print(f'No available update for ID{id}\n')
else:
sub['url'] = new_url
print(f'ID{id} url updated to {new_url}\n')
except KeyError:
print(f'{id} Url not changed! Please define update method.')
updated_list = json.dumps(raw_list, sort_keys=False, indent=2, ensure_ascii=False)
file = open(sub_list_json, 'w', encoding='utf-8')
file.write(updated_list)
file.close()
def change_date(id,current_url):
if id == 36:
today = datetime.today().strftime('%Y%m%d')
this_month = datetime.today().strftime('%Y%m')
url_front = 'https://nodefree.org/dy/'
url_end = '.txt'
new_url = url_front + this_month + '/' + today + url_end
if id == 0:
today = datetime.today().strftime('%m%d')
url_front = 'https://raw.githubusercontent.com/pojiezhiyuanjun/freev2/master/'
url_end = '.txt'
new_url = url_front + today + url_end
if url_updated(new_url):
return new_url
else:
return current_url
def find_link(id,current_url):
if id == 38:
try:
res_json = requests.get('https://api.github.com/repos/mianfeifq/share/contents/').json()
for file in res_json:
if file['name'].startswith('data'):
return file['download_url']
else:
return current_url
except Exception:
return current_url
if id == 33:
url_update = 'https://v2cross.com/archives/1884'
if url_updated(url_update):
try:
resp = requests.get(url_update, timeout=5)
raw_content = resp.text
raw_content = raw_content.replace('amp;', '')
pattern = re.compile(r'https://shadowshare.v2cross.com/publicserver/servers/temp/\w{16}')
new_url = re.findall(pattern, raw_content)[0]
return new_url
except Exception:
return current_url
else:
return current_url
if __name__ == '__main__':
update_url.update_main()