Add support for GitHub releases.

This commit is contained in:
Casper V. Kristensen 2019-10-10 00:08:06 +02:00
parent 6e3203108a
commit 0bfa0d2326
Signed by: caspervk
GPG key ID: 289CA03790535054
3 changed files with 34 additions and 7 deletions

View file

@ -35,7 +35,9 @@ For more information, see <https://git.caspervk.net/caspervk/wau>.
- CurseForge.com - CurseForge.com
- WowInterface.com - WowInterface.com
- Git - Git
- ~~GitHub Releases~~ (coming soon™) - Supports `<url>#<branch>` syntax.
- GitHub Releases
- `https://github.com/x/y/releases`.
- HTTP - HTTP

View file

@ -1,7 +1,7 @@
from .base import Provider from .base import Provider
from .curseforge import CurseForge from .curseforge import CurseForge
from .git import Git from .git import Git
#from .github import GitHub from .github import GitHub
from .web import Web from .web import Web
from .wowinterface import WowInterface from .wowinterface import WowInterface
@ -9,7 +9,7 @@ from .wowinterface import WowInterface
PROVIDERS = { # in order of preference PROVIDERS = { # in order of preference
"curse": CurseForge, "curse": CurseForge,
"wowinterface": WowInterface, "wowinterface": WowInterface,
#"github": GitHub, "github": GitHub,
"git": Git, "git": Git,
"web": Web "web": Web
} }

View file

@ -1,13 +1,38 @@
import logging
import re import re
from typing import Tuple
from .web import Web
from .. import http
from ..addons import Addon from ..addons import Addon
logger = logging.getLogger(__name__)
class GitHub(Web):
api_url = "https://api.github.com"
class Provider:
@classmethod @classmethod
def is_supported(cls, url: str) -> bool: def is_supported(cls, url: str) -> bool:
return bool(re.search("github.com/.*/.*/releases", url, flags=re.IGNORECASE)) try:
cls._parse_url(url)
return True
except AttributeError:
return False
@classmethod @classmethod
def download(cls, addon: Addon) -> bool: def download(cls, addon: Addon, url: str = None) -> bool:
raise NotImplemented repo_owner, repo_name = cls._parse_url(addon.url)
addon.name = repo_name
latest_release = http.open(
url=f"{cls.api_url}/repos/{repo_owner}/{repo_name}/releases/latest"
).json()
for asset in latest_release["assets"]:
if asset["content_type"] == "application/x-zip-compressed":
return super().download(addon, asset["browser_download_url"])
raise FileNotFoundError("No zip file found for latest release")
@classmethod
def _parse_url(cls, url: str) -> Tuple[str, str]:
repo_owner, repo_name = re.search(r"github.com/(.+)/(.+)/releases", url).groups()
return repo_owner, repo_name