-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathpypi.py
More file actions
31 lines (24 loc) · 1.4 KB
/
pypi.py
File metadata and controls
31 lines (24 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import aiohttp
from typing import Optional
BASE_URL_PYPI: str = 'https://pypi.org/pypi'
BASE_URL_PYPISTATS: str = 'https://pypistats.org/api'
class PyPIAPI:
def __init__(self, ses: aiohttp.ClientSession):
self.ses: aiohttp.ClientSession = ses
async def get_project_data(self, project: str) -> Optional[dict]:
res: aiohttp.ClientResponse = await self.ses.get(BASE_URL_PYPI + f'/{project}/json')
if res.status == 200:
return await res.json()
async def get_project_version_data(self, project: str, version: str) -> Optional[dict]:
# This endpoint doesn't make sense, returns the same data as the non-versioned one
res: aiohttp.ClientResponse = await self.ses.get(BASE_URL_PYPI + f'/{project}/{version}/json')
if res.status == 200:
return await res.json()
async def get_project_overall_downloads(self, project: str, mirrors: bool = False) -> Optional[dict]:
res: aiohttp.ClientResponse = await self.ses.get(BASE_URL_PYPISTATS + f'/packages/{project.lower()}/overall?mirrors={str(mirrors).lower()}')
if res.status == 200:
return await res.json()
async def get_project_recent_downloads(self, project: str) -> Optional[dict]:
res: aiohttp.ClientResponse = await self.ses.get(BASE_URL_PYPISTATS + f'/packages/{project.lower()}/recent')
if res.status == 200:
return await res.json()