From 0bfa0d23268903038a86c5326724c0b0914b049b Mon Sep 17 00:00:00 2001 From: "Casper V. Kristensen" Date: Thu, 10 Oct 2019 00:08:06 +0200 Subject: [PATCH] Add support for GitHub releases. --- README.md | 4 +++- wau/providers/__init__.py | 4 ++-- wau/providers/github.py | 33 +++++++++++++++++++++++++++++---- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f3adaad..fe40d3d 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,9 @@ For more information, see . - CurseForge.com - WowInterface.com - Git - - ~~GitHub Releases~~ (coming soon™) + - Supports `#` syntax. + - GitHub Releases + - `https://github.com/x/y/releases`. - HTTP diff --git a/wau/providers/__init__.py b/wau/providers/__init__.py index baba41c..45eec84 100644 --- a/wau/providers/__init__.py +++ b/wau/providers/__init__.py @@ -1,7 +1,7 @@ from .base import Provider from .curseforge import CurseForge from .git import Git -#from .github import GitHub +from .github import GitHub from .web import Web from .wowinterface import WowInterface @@ -9,7 +9,7 @@ from .wowinterface import WowInterface PROVIDERS = { # in order of preference "curse": CurseForge, "wowinterface": WowInterface, - #"github": GitHub, + "github": GitHub, "git": Git, "web": Web } diff --git a/wau/providers/github.py b/wau/providers/github.py index 1fdcfee..0c51a75 100644 --- a/wau/providers/github.py +++ b/wau/providers/github.py @@ -1,13 +1,38 @@ +import logging import re +from typing import Tuple +from .web import Web +from .. import http from ..addons import Addon +logger = logging.getLogger(__name__) + + +class GitHub(Web): + api_url = "https://api.github.com" -class Provider: @classmethod 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 - def download(cls, addon: Addon) -> bool: - raise NotImplemented + def download(cls, addon: Addon, url: str = None) -> bool: + 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