1
0
mirror of https://gitee.com/sui-feng-cb/AzurLaneAutoScript1 synced 2026-03-12 19:07:01 +08:00
Files
AzurLaneAutoScript/module/update.py

105 lines
3.2 KiB
Python
Raw Normal View History

import json
import re
import subprocess
from datetime import datetime
import requests
from module.config.config import AzurLaneConfig
from module.logger import logger
class Update:
def __init__(self, config):
"""
Args:
config (AzurLaneConfig):
"""
self.config = config
@staticmethod
def github_api(author, token):
return f'https://api.github.com/repos/{author}/AzurLaneAutoScript/commits?access_token={token}'
def get_github_commit(self, author, token=None, proxy=None):
"""
Args:
author (str):
token (str): To generate your token visit https://github.com/settings/tokens
proxy (str): Local http or socks proxy, example: http://127.0.0.1:10809, socks://127.0.0.1:10808
Need to install requests[socks], if using a socks proxy.
Returns:
datetime.datetime, str
"""
if proxy:
proxy = {'http': proxy, 'https': proxy}
resp = requests.get(self.github_api(author, token), proxies=proxy)
resp.encoding = 'utf-8'
data = json.loads(resp.content)
pattern = re.compile('^[Mm]erge')
for commit in data:
if re.search(pattern, commit['commit']['message']):
continue
date = datetime.strptime(commit['commit']['author']['date'], '%Y-%m-%dT%H:%M:%SZ')
return date, author
logger.warning(f'No commit found. author={author}')
@staticmethod
def get_local_commit():
"""
Returns:
datetime.datetime, str
"""
cmd = ['git', 'log', '--no-merges', '-1']
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
result = process.communicate(timeout=4)[0].decode("utf-8")
date = datetime.strptime(result.split('\n')[2][8:], '%a %b %d %H:%M:%S %Y %z')
date -= date.utcoffset()
date = date.replace(tzinfo=None)
return date, '<local>'
def get_latest_commit(self):
"""
Returns:
datetime.datetime, str
Logs:
Time Author
old: 2020-06-20_09:12:19 <local>
2020-06-20_10:12:19 whoamikyo
new: 2020-06-20_11:12:19 LmeSzinc
"""
logger.hr('Update Check')
commits = [
self.get_github_commit('whoamikyo', token=self.config.GITHUB_TOKEN, proxy=self.config.UPDATE_PROXY),
self.get_github_commit('LmeSzinc', token=self.config.GITHUB_TOKEN, proxy=self.config.UPDATE_PROXY),
self.get_local_commit()
]
commits.sort(key=lambda x: x[0])
text = [f'{commit[0]} {commit[1]}' for commit in commits]
text.insert(0, 'Time(UTC) Author')
for index, line in enumerate(text):
if index == 1:
logger.info(f'old: {line}')
elif index == len(text) - 1:
logger.info(f'new: {line}')
else:
logger.info(f' {line}')
if commits[-1][1] != '<local>':
logger.warning('A new update is available')
cfg = AzurLaneConfig()
cfg.UPDATE_PROXY = ''
update = Update(cfg)
update.get_latest_commit()