Skip to content

Commit

Permalink
Merge pull request #1 from SomeHybrid/main
Browse files Browse the repository at this point in the history
3.2.0
  • Loading branch information
Sas2k committed Oct 8, 2022
2 parents 7ccbfed + a863e94 commit ce6b7a3
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 51 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -31,10 +33,26 @@ 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.

Please make sure to update tests as appropriate.

## License
[MIT](https://github.com/Sas2k/xkcd-python/blob/main/LICENSE)

27 changes: 27 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -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
47 changes: 16 additions & 31 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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= "[email protected]",
description= xkcd_python.__shot_des__,
Expand All @@ -21,4 +21,4 @@
"Operating System :: MacOS :: MacOS X",
"Operating System :: Microsoft :: Windows"
]
)
)
4 changes: 2 additions & 2 deletions xkcd_python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
__description__ = "A wrapper for xkcd.com's API. Built Using Python."
69 changes: 54 additions & 15 deletions xkcd_python/xkcd.py
Original file line number Diff line number Diff line change
@@ -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()
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()

0 comments on commit ce6b7a3

Please sign in to comment.