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:
InternalCache = InternalCache()
"""Implements the methods required to get the latest releases and patches from revanced repositories."""
headers = {'Accept': "application/vnd.github+json",
'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.
#
# Args:
# client (httpx_cache.AsyncClient): httpx_cache reusable async client
# repository (str): Github's standard username/repository notation
#
# Returns:
# dict: dictionary of filename and download url
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:
release_assets = response.json()['assets']
@ -58,26 +58,26 @@ class Releases:
cached_releases = await self.InternalCache.get("releases")
return cached_releases
except:
async with httpx_cache.AsyncClient(headers=self.headers, http2=True) as client:
for repository in repositories:
files = await self._get_release(client, repository)
if files:
for file in files:
releases['tools'].append(file)
for repository in repositories:
files = await self._get_release(repository)
if files:
for file in files:
releases['tools'].append(file)
await self.InternalCache.store('releases', 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.
#
# Returns:
# 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:
"""Get patches.json from revanced-patches repository.
@ -92,23 +92,21 @@ class Releases:
cached_patches = await self.InternalCache.get("patches")
return cached_patches
except:
async with httpx_cache.AsyncClient(headers=self.headers, http2=True) as client:
patches = await self._get_patches_json(client)
await self.InternalCache.store('patches', patches)
patches = await self._get_patches_json()
await self.InternalCache.store('patches', 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.
#
# Args:
# client (httpx_cache.AsyncClient): httpx_cache reusable async client
# repository (str): Github's standard username/repository notation
#
# Returns:
# 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()
@ -129,12 +127,11 @@ class Releases:
cached_contributors = await self.InternalCache.get("contributors")
return cached_contributors
except:
async with httpx_cache.AsyncClient(headers=self.headers, http2=True) as client:
for repository in repositories:
if 'revanced' in repository:
repo_contributors = await self._get_contributors(client, repository)
data = { 'name': repository, 'contributors': repo_contributors }
contributors['repositories'].append(data)
for repository in repositories:
if 'revanced' in repository:
repo_contributors = await self._get_contributors(repository)
data = { 'name': repository, 'contributors': repo_contributors }
contributors['repositories'].append(data)
await self.InternalCache.store('contributors', contributors)
return contributors