-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
53 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import os | ||
import io | ||
import zipfile | ||
import requests | ||
from . import retry, HEADERS | ||
|
||
|
||
class GitHubAddon: | ||
@retry() | ||
def __init__(self, url): | ||
project = url.replace('https://github.com/', '') | ||
self.payload = requests.get(f'https://api.github.com/repos/{project}/releases/latest', headers=HEADERS) | ||
if self.payload.status_code == 404: | ||
raise RuntimeError(url) | ||
else: | ||
self.payload = self.payload.json() | ||
if 'assets' not in self.payload or len(self.payload['assets']) == 0 \ | ||
or self.payload['assets'][0]['content_type'] not in ['application/x-zip-compressed', 'application/zip']: | ||
raise RuntimeError(url) | ||
self.name = project.split('/')[1] | ||
self.downloadUrl = self.payload['assets'][0]['browser_download_url'] | ||
self.currentVersion = self.payload['name'] | ||
self.archive = None | ||
self.directories = [] | ||
|
||
@retry() | ||
def get_addon(self): | ||
self.archive = zipfile.ZipFile(io.BytesIO(requests.get(self.downloadUrl).content)) | ||
for file in self.archive.namelist(): | ||
if '/' not in os.path.dirname(file): | ||
self.directories.append(os.path.dirname(file)) | ||
self.directories = list(filter(None, set(self.directories))) | ||
if len(self.directories) == 0: | ||
raise RuntimeError(f'{self.name}.\nProject package is corrupted or incorrectly packaged.') | ||
|
||
def install(self, path): | ||
self.archive.extractall(path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters