Plex-Meta-Manager/modules/github.py

101 lines
4 KiB
Python
Raw Normal View History

2022-03-18 13:00:42 +00:00
import re
from modules import util
2023-03-06 21:54:01 +00:00
from modules.util import Failed
2022-03-18 13:00:42 +00:00
logger = util.logger
2023-03-30 19:51:04 +00:00
raw_url = "https://raw.githubusercontent.com"
2023-03-06 21:54:01 +00:00
base_url = "https://api.github.com"
2024-04-22 14:20:12 +00:00
kometa_base = f"{base_url}/repos/Kometa-Team/Kometa"
configs_raw_url = f"{raw_url}/Kometa-Team/Community-Configs"
2022-03-18 13:00:42 +00:00
class GitHub:
2023-07-24 14:03:55 +00:00
def __init__(self, config, params):
2022-03-18 13:00:42 +00:00
self.config = config
2023-07-24 14:03:55 +00:00
self.token = params["token"]
logger.secret(self.token)
self.headers = {"Authorization": f"token {self.token}"} if self.token else None
2024-04-22 14:20:12 +00:00
self.images_raw_url = f"{raw_url}/Kometa-Team/Image-Sets/master/sets/"
self.translation_url = f"{raw_url}/Kometa-Team/Translations/master/defaults/"
2022-07-13 05:13:25 +00:00
self._configs_url = None
2022-09-22 18:34:25 +00:00
self._config_tags = []
2023-03-30 19:51:04 +00:00
self._translation_keys = []
self._translations = {}
2022-03-18 13:00:42 +00:00
2023-07-24 14:03:55 +00:00
def _requests(self, url, err_msg=None, json=True, params=None):
response = self.config.get(url, headers=self.headers, params=params)
if not err_msg:
err_msg = f"URL Not Found: {url}"
if response.status_code >= 400:
raise Failed(f"Git Error: {err_msg}")
if json:
try:
return response.json()
except ValueError:
logger.error(str(response.content))
raise
return response
2023-03-06 21:54:01 +00:00
def get_top_tree(self, repo):
if not str(repo).startswith("/"):
repo = f"/{repo}"
if not str(repo).endswith("/"):
repo = f"{repo}/"
2023-07-24 14:03:55 +00:00
data = self._requests(f"{base_url}/repos{repo}commits", f"No repo found at https://github.com{repo}")
return self.get_tree(data[0]["commit"]["tree"]["url"]), repo
2023-03-06 21:54:01 +00:00
def get_tree(self, tree_url):
2023-07-24 14:03:55 +00:00
return {i["path"]: i for i in self._requests(tree_url, f"No tree found at {tree_url}")["tree"]}
2023-03-06 21:54:01 +00:00
2022-03-18 13:00:42 +00:00
def latest_release_notes(self):
2024-04-22 14:20:12 +00:00
return self._requests(f"{kometa_base}/releases/latest")["body"]
2022-03-18 13:00:42 +00:00
2022-05-05 12:48:04 +00:00
def get_commits(self, dev_version, nightly=False):
2024-04-22 14:20:12 +00:00
master_sha = self._requests(f"{kometa_base}/commits/master")["sha"]
response = self._requests(f"{kometa_base}/commits", params={"sha": "nightly" if nightly else "develop"})
2022-03-18 13:00:42 +00:00
commits = []
for commit in response:
if commit["sha"] == master_sha:
break
message = commit["commit"]["message"]
2023-07-24 14:03:55 +00:00
match = re.match(r"^\[(\d)]", message)
2022-03-18 13:00:42 +00:00
if match and int(match.group(1)) <= dev_version:
break
commits.append(message)
return "\n".join(commits)
2022-07-13 05:13:25 +00:00
@property
2022-09-22 18:34:25 +00:00
def config_tags(self):
if not self._config_tags:
2022-07-13 05:13:25 +00:00
try:
2024-04-22 14:20:12 +00:00
self._config_tags = [r["ref"][11:] for r in self._requests(f"{kometa_base}-Configs/git/refs/tags")]
2022-07-13 05:13:25 +00:00
except TypeError:
2022-09-22 18:34:25 +00:00
pass
return self._config_tags
@property
def configs_url(self):
if self._configs_url is None:
self._configs_url = f"{configs_raw_url}/master/"
2024-03-28 13:08:28 +00:00
if self.config.version[1] in self.config_tags and (self.config.latest_version[1] != self.config.version[1] or self.config.branch == "master"):
2022-09-25 18:43:58 +00:00
self._configs_url = f"{configs_raw_url}/v{self.config.version[1]}/"
2022-07-13 05:13:25 +00:00
return self._configs_url
2023-03-30 19:51:04 +00:00
@property
def translation_keys(self):
if not self._translation_keys:
2024-04-22 14:20:12 +00:00
tree, repo = self.get_top_tree("Kometa-Team/Translations")
2023-03-30 19:51:04 +00:00
self._translation_keys = [tk[:-4] for tk in self.get_tree(tree["defaults"]["url"])]
return self._translation_keys
def translation_yaml(self, translation_key):
if translation_key not in self._translations:
url = f"{self.translation_url}{translation_key}.yml"
2023-07-24 14:03:55 +00:00
yaml = util.YAML(input_data=self._requests(url, json=False).content).data
2023-04-01 19:02:32 +00:00
output = {"collections": {}, "key_names": {}, "variables": {}}
2023-03-30 19:51:04 +00:00
for k in output:
if k in yaml:
output[k] = yaml[k]
self._translations[translation_key] = output
return self._translations[translation_key]