forked from 40Cakes/pokebot-gen3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.py
119 lines (98 loc) · 4.13 KB
/
updater.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import datetime
import sys
from dataclasses import dataclass
from datetime import datetime
from urllib import request
from urllib.error import URLError
from zipfile import ZipFile
import requests
from modules.console import console
from modules.runtime import get_base_path
from modules.version import pokebot_version
# Module `exceptions` is not being used, but it needs to be imported because otherwise `modules.console`
# runs into a circular import issue.
@dataclass
class ReleaseInfo:
tag_name: str
release_url: str
download_url: str
download_filename: str
created_at: datetime
def get_last_update_check_datetime() -> datetime | None:
check_file_path = get_base_path() / ".last-update-check"
if not check_file_path.is_file():
return None
try:
with open(check_file_path, "r") as file:
result = datetime.fromisoformat(file.read())
return result
except Exception:
return None
def get_most_recent_release_on_github() -> ReleaseInfo | None:
try:
response = requests.get("https://api.github.com/repos/40cakes/pokebot-gen3/releases/latest")
if response.status_code != 200:
return None
data = response.json()
created_at = datetime.fromisoformat(data["created_at"])
except Exception as e:
console.print(f"[bold yellow]Error while checking for updates:[/] [yellow]{str(e)}[/]")
return None
if (
"html_url" in data
and "assets" in data
and len(data["assets"]) > 0
and "browser_download_url" in data["assets"][0]
and "name" in data["assets"][0]
):
with open(get_base_path() / ".last-update-check", "w") as file:
file.write(str(datetime.now()))
return ReleaseInfo(
tag_name=data["tag_name"],
release_url=data["html_url"],
download_url=data["assets"][0]["browser_download_url"],
download_filename=data["assets"][0]["name"],
created_at=created_at,
)
else:
return None
def fetch_release_from_github(release_info: ReleaseInfo) -> bool:
try:
request.urlretrieve(release_info.download_url, get_base_path() / release_info.download_filename)
return True
except URLError as e:
console.print(f"[bold yellow]Error while downloading update:[/] [yellow]{str(e)}[/]")
return False
def extract_update_file(release_info: ReleaseInfo) -> bool:
try:
with ZipFile(get_base_path() / release_info.download_filename) as zip:
zip.extractall(path=get_base_path())
return True
except Exception as e:
console.print(f"[bold yellow]Error while extracting update:[/] [yellow]{str(e)}[/]")
return False
def run_updater(ignore_last_update: bool = False) -> None:
last_update = get_last_update_check_datetime()
if ignore_last_update or last_update is None or datetime.now().timestamp() - last_update.timestamp() > 86400:
release_info = get_most_recent_release_on_github()
if release_info is not None and release_info.tag_name != pokebot_version:
console.print("\n[green bold]There is a new update available![/]\n")
console.print(
f"The most recent version is [cyan bold]{release_info.tag_name}[/], "
f"released on [yellow]{str(release_info.created_at)}[/].\n"
)
response = input("Download now? [y/N] ")
if response in ["y", "Y"]:
if fetch_release_from_github(release_info) and extract_update_file(release_info):
console.print("\n[green]Update successful![/]")
console.print("[green bold]Restart the bot to apply.[/]")
sys.exit(0)
else:
console.print("\n[yellow]Ignoring update.[/] Will ask again tomorrow.")
console.print(
f"If you change your mind, you can manually download the update here: {release_info.release_url}\n"
)
elif ignore_last_update:
console.print("No newer version found.")
if __name__ == "__main__":
run_updater(ignore_last_update=True)