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

59 lines
1.9 KiB
Python

import re
import requests
from bs4 import BeautifulSoup
from updater.site.abstract_site import AbstractSite
from updater.site.enum import GameVersion
class TownlongYak(AbstractSite):
_URLS = [
'https://www.townlong-yak.com/'
]
session = requests.session()
latest_version = None
_page: BeautifulSoup = None
_version_pattern = r'([\d]+\.[\d]+[a-z]*)'
def __init__(self, url: str):
super().__init__(url, GameVersion.agnostic)
def find_zip_url(self):
download_link = self._get_page().find('div', attrs={'class': 'quickdownloads'}).find('a', attrs={'class': 'c-link'})['href']
download_link = TownlongYak._URLS[0] + download_link[1:] # take off the leading / from the href
try:
response = TownlongYak.session.get(download_link)
response.raise_for_status()
page = BeautifulSoup(response.text, 'html.parser')
dl_url = page.find('li', attrs={'class': 'hg'}).find('a')['href']
return TownlongYak._URLS[0] + dl_url[1:]
except Exception as e:
raise self.download_error() from e
def get_latest_version(self):
try:
version = self._get_page().find('div', attrs={'class': 'quickdownloads'}).findAll('a')[-1].text
version = re.search(f'{self._version_pattern}', version).group(1)
self.latest_version = version
return version
except Exception as e:
raise self.version_error() from e
def get_addon_name(self):
return self.url.split('/')[-1]
def _get_page(self):
try:
if not self._page:
response = TownlongYak.session.get(self.url)
response.raise_for_status()
self._page = BeautifulSoup(response.text, 'html.parser')
return self._page
except Exception as e:
raise self.download_error() from e