-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.py
126 lines (104 loc) · 4.5 KB
/
update.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
120
121
122
123
124
125
126
import json
import time
from collections import defaultdict
from enum import Enum
from pathlib import Path
from typing import Dict
import httpx
import lxml.html
class Region(str, Enum):
NA = "NA"
JP = "JP"
class Store(str, Enum):
PLAY_STORE = "Google Play Store"
APP_STORE = "iOS App Store"
STORE_URL = {
Region.NA: {
Store.APP_STORE: "https://itunes.apple.com/lookup?bundleId=com.aniplex.fategrandorder.en&country=us",
Store.PLAY_STORE: "https://play.google.com/store/apps/details?id=com.aniplex.fategrandorder.en",
},
Region.JP: {
Store.APP_STORE: "https://itunes.apple.com/lookup?bundleId=com.aniplex.fategrandorder&country=jp",
Store.PLAY_STORE: "https://play.google.com/store/apps/details?id=com.aniplex.fategrandorder",
},
}
AVATAR_URL = {
Store.PLAY_STORE: "https://i.imgur.com/kN7NO37.png", # From the PLay Store apk P5w.png
Store.APP_STORE: "https://i.imgur.com/fTxPeCW.png", # https://www.apple.com/app-store/
}
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36"
PLAY_STORE_XPATH = "/html/body/div[1]/div[4]/c-wiz/div/div[2]/div/div/main/c-wiz[4]/div[1]/div[2]/div/div[4]/span/div/span"
APP_STORE_XPATH = "/html/body/div[1]/div[4]/main/div/section[4]/div[2]/div[1]/div/p"
HEADERS = {"user-agent": USER_AGENT}
def get_website_ver(play_store_url: str, xpath: str) -> str:
response = httpx.get(play_store_url, follow_redirects=True)
site_html = lxml.html.fromstring(response.text)
try:
version_string: str = site_html.xpath(xpath)[0].text
return version_string.replace("バージョン", "").replace("Version", "").strip()
except Exception: # pylint: disable=broad-except
return "2.0.0"
def get_app_store_ver(app_store_url: str) -> str:
app_store_response = httpx.get(
app_store_url + f"&time={int(time.time())}", follow_redirects=True
)
app_detail = app_store_response.json()["results"][0]
api_version = str(app_detail["version"])
return api_version
# app_store_site_url = str(app_detail["trackViewUrl"]).split("?", maxsplit=1)[0]
# app_store_version = get_website_ver(app_store_site_url, APP_STORE_XPATH)
# if is_new_ver(api_version, app_store_version):
# return api_version
# else:
# if api_version != app_store_version:
# print("App store website version is newer than api version")
# return app_store_version
def get_app_ver(store: str, url: str) -> str:
if store == Store.PLAY_STORE:
return get_website_ver(url, PLAY_STORE_XPATH)
else:
return get_app_store_ver(url)
def is_new_ver(new_ver: str, current_ver: str) -> bool:
try:
new_nums = [int(num) for num in new_ver.split(".")]
current_nums = [int(num) for num in current_ver.split(".")]
for new_num, current_num in zip(new_nums, current_nums):
if new_num != current_num:
return new_num > current_num
except ValueError:
return False
return False
def main(webhook: dict[str, str]) -> None:
current_ver_path = Path("current_ver.json")
if current_ver_path.exists():
old_save_data: Dict[str, Dict[str, str]] = json.loads(
current_ver_path.read_bytes()
)
else:
old_save_data = {
Region.NA: {Store.PLAY_STORE: "2.0.0", Store.APP_STORE: "2.0.0"},
Region.JP: {Store.PLAY_STORE: "2.0.0", Store.APP_STORE: "2.0.0"},
}
save_data: Dict[Region, Dict[Store, str]] = defaultdict(dict)
for region in [Region.NA, Region.JP]:
for store in [Store.APP_STORE]:
old_ver = old_save_data[region][store]
new_ver = get_app_ver(store, STORE_URL[region][store])
if is_new_ver(new_ver, old_ver):
message = f"{region} update: v{new_ver}"
print(message)
webhook_content = {
"content": message,
"username": store.value,
"avatar_url": AVATAR_URL[store],
}
httpx.post(webhook[str(region)], data=webhook_content, follow_redirects=True)
save_data[region][store] = new_ver
else:
save_data[region][store] = old_ver
with open(current_ver_path, "w", encoding="utf-8") as fp:
json.dump(save_data, fp, indent=2)
if __name__ == "__main__":
with open("WEBHOOK.url", encoding="utf-8") as webhook_fp:
webhook_url = json.load(webhook_fp)
main(webhook_url)