diff --git a/README.md b/README.md index 1b2493d..6b599d9 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,10 @@ pip install -U xkcd-python ## Usage +### Normal usage + ```python -from xkcd_python import * +from xkcd_python import Client #creates the client client = Client() @@ -31,6 +33,21 @@ client.random() client.latest_comic() ``` +### Async usage + +```python +from xkcd_python import Client +import asyncio + +client = Client() + +async def main(): + tasks = (client.get(x) for x in range(1, 20)) + return await asyncio.gather(*tasks) + +asyncio.run(main) +``` + ## Contributing Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. @@ -38,3 +55,4 @@ Please make sure to update tests as appropriate. ## License [MIT](https://github.com/Sas2k/xkcd-python/blob/main/LICENSE) + diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..57f7bfe --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,27 @@ +atomicwrites==1.4.0 +attrs==21.4.0 +bleach==5.0.0 +colorama==0.4.5 +commonmark==0.9.1 +docutils==0.18.1 +importlib-metadata==4.11.4 +iniconfig==1.1.1 +keyring==23.6.0 +packaging==21.3 +pkginfo==1.8.3 +pluggy==1.0.0 +py==1.11.0 +Pygments==2.12.0 +pyparsing==3.0.9 +pytest==7.1.2 +pywin32-ctypes==0.2.0 +readme-renderer==35.0 +requests==2.28.0 +requests-toolbelt==0.9.1 +rfc3986==2.0.0 +rich==12.4.4 +six==1.16.0 +tomli==2.0.1 +twine==4.0.1 +webencodings==0.5.1 +zipp==3.8.0 diff --git a/requirements.txt b/requirements.txt index 0b22437..387a1bf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,31 +1,16 @@ -atomicwrites==1.4.0 -attrs==21.4.0 -bleach==5.0.0 -certifi==2022.6.15 -charset-normalizer==2.0.12 -colorama==0.4.5 -commonmark==0.9.1 -docutils==0.18.1 -idna==3.3 -importlib-metadata==4.11.4 -iniconfig==1.1.1 -keyring==23.6.0 -packaging==21.3 -pkginfo==1.8.3 -pluggy==1.0.0 -py==1.11.0 -Pygments==2.12.0 -pyparsing==3.0.9 -pytest==7.1.2 -pywin32-ctypes==0.2.0 -readme-renderer==35.0 -requests==2.28.0 -requests-toolbelt==0.9.1 -rfc3986==2.0.0 -rich==12.4.4 -six==1.16.0 -tomli==2.0.1 -twine==4.0.1 -urllib3==1.26.9 -webencodings==0.5.1 -zipp==3.8.0 +certifi>=2022.6.15 +charset-normalizer>=2.0.12 +idna>=3.3 +urllib3>=1.26.9 +charset-normalizer>=2.0 +charset-normalizer<3.0 +multidict>=4.5 +multidict<7.0 +async_timeout>=4.0 +async_timeout<5.0 +asynctest==0.13.0 +yarl>=1.0 +yarl<2.0 +typing_extensions>=3.7.4 +frozenlist>=1.1.1 +aiosignal>=1.1.2 diff --git a/setup.py b/setup.py index e29c75b..25bfc7c 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setup( # the name must match the folder name 'verysimplemodule' name= "xkcd_python", - version= "3.1.1", + version= "3.2.0", author= xkcd_python.__author__, author_email= "sasen.learnings@gmail.com", description= xkcd_python.__shot_des__, @@ -21,4 +21,4 @@ "Operating System :: MacOS :: MacOS X", "Operating System :: Microsoft :: Windows" ] -) \ No newline at end of file +) diff --git a/xkcd_python/__init__.py b/xkcd_python/__init__.py index c79108a..e9b3b41 100644 --- a/xkcd_python/__init__.py +++ b/xkcd_python/__init__.py @@ -2,7 +2,7 @@ Client = xkcd -__version__ = '2.9.0' +__version__ = '3.2.0' __author__ = "Sasen Perera" __shot_des__ = "A python wrapper for xkcd.com" -__description__ = "A wrapper for xkcd.com's API. Built Using Python." \ No newline at end of file +__description__ = "A wrapper for xkcd.com's API. Built Using Python." diff --git a/xkcd_python/xkcd.py b/xkcd_python/xkcd.py index 130cfcd..6e9e473 100644 --- a/xkcd_python/xkcd.py +++ b/xkcd_python/xkcd.py @@ -1,20 +1,59 @@ -from requests import * -from random import * +import random +import requests -class xkcd(): - def __init__(self) -> None: - pass - - def random(self = None): - latest_comic_id = request("GET", "https://xkcd.com/info.0.json") - comic_id = randint(1, int(latest_comic_id.json()["num"])) - random_comic = request("GET", f"https://xkcd.com/{comic_id}/info.0.json") +class xkcd: + def __init__(self, async=False, client=None) -> None: + self.current = int(latest_comic_id.json()["num"]) + if async: + self.random = self._arandom + self.get = self._aget + self.latest_comic = self._alatest_comic + self._client = client + + self.__aenter__ = self.enter + self.__aexit__ = self.exit + else: + self.random = self._random + self.get = self._get + self.latest_comic = self._latest_comic + + if client: + raise NotImplementedError + + async def enter(self): + self._client = aiohttp.ClientSession() + return self + + async def exit(self): + await self._client.close() + + def _random(self): + comic_id = random.randint(1, self.current) + random_comic = requests.get(f"https://xkcd.com/{comic_id}/info.0.json") return random_comic.json() - def get(id: int): - comic = request("GET", f"https://xkcd.com/{id}/info.0.json") + def _get(self, id: int): + comic = requests.get(f"https://xkcd.com/{id}/info.0.json") return comic.json() - def latest_comic(self = None): - comic = request("Get", f"https://xkcd.com/info.0.json") - return comic.json() \ No newline at end of file + def _latest_comic(self): + comic = requests.get(f"https://xkcd.com/info.0.json") + return comic.json() + + async def _arandom(self): + client = self.client if self.client else aiohttp.ClientSession() + comic_id = random.randint(1, self.current) + async with client.get(f"https://xkcd.com/{comic_id}/info.0.json") as random_comic: + return await random_comic.json() + + async def _aget(self, id: int): + client = self.client if self.client else aiohttp.ClientSession() + comic_id = random.randint(1, self.current) + async with client.get(f"https://xkcd.com/{id}/info.0.json") as comic: + return await comic.json() + + async def _alatest_comic(self): + client = self.client if self.client else aiohttp.ClientSession() + comic_id = random.randint(1, self.current) + async with client.get(f"https://xkcd.com/info.0.json") as latest_comic: + return await latest_comic.json()