2023-05-09 23:03:56 +02:00

55 lines
1.9 KiB
Python

import requests
import re
from bs4 import BeautifulSoup
from typing import Optional
from updater.site.abstract_site import AbstractSite
from updater.site.enum import GameVersion
from updater.site import CURSE_UA
class GitHubRelease(AbstractSite):
_URLS = ['https://www.github.com/', 'https://github.com/']
session = requests.session()
def __init__(self, url: str):
super().__init__(url, GameVersion.agnostic)
@classmethod
def handles(cls, url: str) -> bool:
return bool(re.match('^https://(www.)?github.com/[^/]+/[^/]+/releases/?$', url))
def find_zip_url(self):
try:
response = GitHubRelease.session.get(self.url+"/expanded_assets/"+self.get_latest_version())
response.raise_for_status()
page = BeautifulSoup(response.text, 'html.parser')
zipurl = list(
filter(lambda a: not any(ext in a for ext in ['wrath','classic','tbc','ctbc','wotlk','cwotlk']),
filter(lambda a: a.endswith('.zip'),
map(lambda a: a['href'], page.findAll('a'))
)
)
)
return "https://github.com"+zipurl[0]
except Exception as e:
raise self.download_error() from e
def _get_repo_name(self):
_, owner, repo, _ = self.url.removeprefix('https://').split('/', 4)
return f'{owner}/{repo}'
def get_latest_version(self):
try:
response = GitHubRelease.session.get(self.url)
response.raise_for_status()
page = BeautifulSoup(response.text, 'html.parser')
tags = page.find('svg',attrs={'class': 'octicon-tag'}).parent
version = tags.find('span').get_text(strip=True)
return version
except Exception as e:
raise self.version_error() from e
def get_addon_name(self):
return self._get_repo_name().split('/')[-1]