minor refactoring

This commit is contained in:
Alexandre Teles 2022-09-04 03:44:22 -03:00
parent 2b2221bf19
commit 5862853774

View File

@ -7,26 +7,26 @@ from modules.InternalCache import InternalCache
class Releases: class Releases:
InternalCache = InternalCache()
"""Implements the methods required to get the latest releases and patches from revanced repositories.""" """Implements the methods required to get the latest releases and patches from revanced repositories."""
headers = {'Accept': "application/vnd.github+json", headers = {'Accept': "application/vnd.github+json",
'Authorization': "token " + os.environ['GITHUB_TOKEN'] 'Authorization': "token " + os.environ['GITHUB_TOKEN']
} }
async def _get_release(self, client: httpx_cache.AsyncClient, repository: str) -> list: httpx_client = httpx_cache.AsyncClient(headers=headers, http2=True)
InternalCache = InternalCache()
async def _get_release(self, repository: str) -> list:
# Get assets from latest release in a given repository. # Get assets from latest release in a given repository.
# #
# Args: # Args:
# client (httpx_cache.AsyncClient): httpx_cache reusable async client
# repository (str): Github's standard username/repository notation # repository (str): Github's standard username/repository notation
# #
# Returns: # Returns:
# dict: dictionary of filename and download url # dict: dictionary of filename and download url
assets = [] assets = []
response = await client.get(f"https://api.github.com/repos/{repository}/releases/latest") response = await self.httpx_client.get(f"https://api.github.com/repos/{repository}/releases/latest")
if response.status_code == 200: if response.status_code == 200:
release_assets = response.json()['assets'] release_assets = response.json()['assets']
@ -58,9 +58,8 @@ class Releases:
cached_releases = await self.InternalCache.get("releases") cached_releases = await self.InternalCache.get("releases")
return cached_releases return cached_releases
except: except:
async with httpx_cache.AsyncClient(headers=self.headers, http2=True) as client:
for repository in repositories: for repository in repositories:
files = await self._get_release(client, repository) files = await self._get_release(repository)
if files: if files:
for file in files: for file in files:
releases['tools'].append(file) releases['tools'].append(file)
@ -68,16 +67,17 @@ class Releases:
return releases return releases
async def _get_patches_json(self, client: httpx_cache.AsyncClient) -> dict: async def _get_patches_json(self) -> dict:
# Get revanced-patches repository's README.md. # Get revanced-patches repository's README.md.
# #
# Returns: # Returns:
# dict: JSON content # dict: JSON content
# #
content = await client.get(f"https://api.github.com/repos/revanced/revanced-patches/contents/patches.json") response = await self.httpx_client.get(f"https://api.github.com/repos/revanced/revanced-patches/contents/patches.json")
content = orjson.loads(b64decode(response.json()['content']).decode('utf-8'))
return orjson.loads(b64decode(content.json()['content']).decode('utf-8')) return content
async def get_patches_json(self, simplified: bool = False) -> dict: async def get_patches_json(self, simplified: bool = False) -> dict:
"""Get patches.json from revanced-patches repository. """Get patches.json from revanced-patches repository.
@ -92,23 +92,21 @@ class Releases:
cached_patches = await self.InternalCache.get("patches") cached_patches = await self.InternalCache.get("patches")
return cached_patches return cached_patches
except: except:
async with httpx_cache.AsyncClient(headers=self.headers, http2=True) as client: patches = await self._get_patches_json()
patches = await self._get_patches_json(client)
await self.InternalCache.store('patches', patches) await self.InternalCache.store('patches', patches)
return patches return patches
async def _get_contributors(self, client: httpx_cache.AsyncClient, repository: str) -> list: async def _get_contributors(self, repository: str) -> list:
# Get contributors from a given repository. # Get contributors from a given repository.
# #
# Args: # Args:
# client (httpx_cache.AsyncClient): httpx_cache reusable async client
# repository (str): Github's standard username/repository notation # repository (str): Github's standard username/repository notation
# #
# Returns: # Returns:
# list: a list of dictionaries containing the repository's contributors # list: a list of dictionaries containing the repository's contributors
response = await client.get(f"https://api.github.com/repos/{repository}/contributors") response = await self.httpx_client.get(f"https://api.github.com/repos/{repository}/contributors")
return response.json() return response.json()
@ -129,10 +127,9 @@ class Releases:
cached_contributors = await self.InternalCache.get("contributors") cached_contributors = await self.InternalCache.get("contributors")
return cached_contributors return cached_contributors
except: except:
async with httpx_cache.AsyncClient(headers=self.headers, http2=True) as client:
for repository in repositories: for repository in repositories:
if 'revanced' in repository: if 'revanced' in repository:
repo_contributors = await self._get_contributors(client, repository) repo_contributors = await self._get_contributors(repository)
data = { 'name': repository, 'contributors': repo_contributors } data = { 'name': repository, 'contributors': repo_contributors }
contributors['repositories'].append(data) contributors['repositories'].append(data)
await self.InternalCache.store('contributors', contributors) await self.InternalCache.store('contributors', contributors)