From 7d8d07ec415355dbaa84f221ea8248e44d08ce90 Mon Sep 17 00:00:00 2001 From: SomeHybrid <91648368+SomeHybrid@users.noreply.github.com> Date: Fri, 7 Oct 2022 13:14:59 +0000 Subject: [PATCH 1/7] Update and rename requirements.txt to requirements-dev.txt --- requirements.txt => requirements-dev.txt | 4 ---- 1 file changed, 4 deletions(-) rename requirements.txt => requirements-dev.txt (86%) diff --git a/requirements.txt b/requirements-dev.txt similarity index 86% rename from requirements.txt rename to requirements-dev.txt index 0b22437..57f7bfe 100644 --- a/requirements.txt +++ b/requirements-dev.txt @@ -1,12 +1,9 @@ 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 @@ -26,6 +23,5 @@ 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 From e7fb5f04455ef412b89aaa5de1d598b4d7b040b8 Mon Sep 17 00:00:00 2001 From: SomeHybrid <91648368+SomeHybrid@users.noreply.github.com> Date: Fri, 7 Oct 2022 13:15:14 +0000 Subject: [PATCH 2/7] Create requirements.txt --- requirements.txt | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7b5e5fd --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +certifi>=2022.6.15 +charset-normalizer>=2.0.12 +idna>=3.3 +urllib3>=1.26.9 From 92a7155da75f373122246968df613bc4ddb7d2d8 Mon Sep 17 00:00:00 2001 From: SomeHybrid <91648368+SomeHybrid@users.noreply.github.com> Date: Fri, 7 Oct 2022 13:17:32 +0000 Subject: [PATCH 3/7] Update requirements.txt --- requirements.txt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/requirements.txt b/requirements.txt index 7b5e5fd..387a1bf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,15 @@ 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 From a67b758f9ff716818a78673a3df6c4830fc73028 Mon Sep 17 00:00:00 2001 From: SomeHybrid <91648368+SomeHybrid@users.noreply.github.com> Date: Fri, 7 Oct 2022 13:32:45 +0000 Subject: [PATCH 4/7] Update xkcd.py --- xkcd_python/xkcd.py | 69 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 54 insertions(+), 15 deletions(-) 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() From 7f24cd5c90d2d2ca11653c1a6f4aca3cb7053a66 Mon Sep 17 00:00:00 2001 From: SomeHybrid <91648368+SomeHybrid@users.noreply.github.com> Date: Fri, 7 Oct 2022 13:34:47 +0000 Subject: [PATCH 5/7] Update setup.py --- setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 +) From 25ccd469241f5da6d6623948099b3398020dd5f7 Mon Sep 17 00:00:00 2001 From: SomeHybrid <91648368+SomeHybrid@users.noreply.github.com> Date: Fri, 7 Oct 2022 13:35:06 +0000 Subject: [PATCH 6/7] Update __init__.py --- xkcd_python/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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." From a863e94995d0c97a730fdf18cd22e10afa06af8f Mon Sep 17 00:00:00 2001 From: SomeHybrid <91648368+SomeHybrid@users.noreply.github.com> Date: Sat, 8 Oct 2022 07:51:16 +0000 Subject: [PATCH 7/7] Update README.md --- README.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) 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) +