Skip to content

Commit

Permalink
Major update
Browse files Browse the repository at this point in the history
Migrate to httpx
Async support without aioify
Added tests for APIs
Added formatter
  • Loading branch information
D4n13l3k00 committed Jan 20, 2024
1 parent 9067573 commit 6c47d53
Show file tree
Hide file tree
Showing 17 changed files with 767 additions and 206 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
dist/
anipics.egg-info/
anipics/__pycache__/
__pycache__/
.vscode/
.venv/
.ruff_cache/
.pytest_cache/

.pypicrc
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
format:
poetry run black anipics/ tests/ setup.py
poetry run isort --profile black anipics/ tests/ setup.py
poetry run ruff --fix anipics/ tests/ setup.py

tests:
poetry run pytest tests/

.PHONY: format tests
15 changes: 5 additions & 10 deletions anipics/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
# Copyright 2021 D4n13l3k00.
# Copyright 2024 Daniel <D4n13l3k00@yandex.ru>.
# SPDX-License-Identifier: AGPL-3.0-or-later

__author__ = 'D4n13l3k00'
__version__ = '1.3'
__author__ = "Daniel <D4n13l3k00@yandex.ru>"
__version__ = "1.5"

from aioify import aioify
__all__ = ["Models", "AniPics", "NekosLife"]

__all__ = ['Parser', 'asyncParser', 'Models']

from .parser import Parser
from .models import Models


asyncParser: Parser = aioify(Parser, 'asyncParser')
from .services import AniPics, NekosLife
4 changes: 1 addition & 3 deletions anipics/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Copyright 2021 D4n13l3k00.
# Copyright 2024 Daniel <D4n13l3k00@yandex.ru>.
# SPDX-License-Identifier: AGPL-3.0-or-later

from typing import *

from pydantic import BaseModel


Expand Down
139 changes: 0 additions & 139 deletions anipics/parser.py

This file was deleted.

4 changes: 4 additions & 0 deletions anipics/services/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__all__ = ["AniPics", "NekosLife"]

from .anipics import AniPics
from .nekoslife import NekosLife
34 changes: 34 additions & 0 deletions anipics/services/anipics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

import httpx
from bs4 import BeautifulSoup

from anipics.models import Models


class AniPics:
def get(self) -> Models.Result:
"""Get picture from animepicsx.net
Returns:
Models.Result
"""
with httpx.Client() as client:
return Models.Result(
url=BeautifulSoup(
client.get("https://animepicsx.net/random").text, "html.parser"
).find("img")["src"]
)

async def async_get(self) -> Models.Result:
"""Get picture from animepicsx.net
Returns:
Models.Result
"""
async with httpx.AsyncClient() as client:
return Models.Result(
url=BeautifulSoup(
(await client.get("https://animepicsx.net/random")).text,
"html.parser",
).find("img")["src"]
)
62 changes: 62 additions & 0 deletions anipics/services/nekoslife.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from typing import NewType, Union

import httpx

from anipics.models import Models

NekosLifeType = NewType("NekosLifeType", str)


class NekosLife:
class Types:
wallpaper: NekosLifeType = "wallpaper"
ngif: NekosLifeType = "ngif"
tickle: NekosLifeType = "tickle"
feed: NekosLifeType = "feed"
gecg: NekosLifeType = "gecg"
gasm: NekosLifeType = "gasm"
slap: NekosLifeType = "slap"
avatar: NekosLifeType = "avatar"
lizard: NekosLifeType = "lizard"
waifu: NekosLifeType = "waifu"
pat: NekosLifeType = "pat"
_8ball: NekosLifeType = "8ball"
kiss: NekosLifeType = "kiss"
neko: NekosLifeType = "neko"
spank: NekosLifeType = "spank"
cuddle: NekosLifeType = "cuddle"
fox_girl: NekosLifeType = "fox_girl"
hug: NekosLifeType = "hug"
smug: NekosLifeType = "smug"
goose: NekosLifeType = "goose"
woof: NekosLifeType = "woof"

def get(self, query: Union[NekosLifeType, str]) -> Models.Result:
"""Get picture from nekos.life
Args:
query (NekosLifeType): See Parser/asyncParser.NekosLife.types
Returns:
Models.Result
"""
with httpx.Client() as client:
return Models.Result(
url=client.get(f"https://nekos.life/api/v2/img/{query}").json()["url"]
)

async def async_get(self, query: Union[NekosLifeType, str]) -> Models.Result:
"""Get picture from nekos.life
Args:
query (NekosLifeType): See Parser/asyncParser.NekosLife.types
Returns:
Models.Result
"""
async with httpx.AsyncClient() as client:
return Models.Result(
url=(await client.get(f"https://nekos.life/api/v2/img/{query}")).json()[
"url"
]
)
29 changes: 0 additions & 29 deletions example.py

This file was deleted.

15 changes: 15 additions & 0 deletions examples/async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from anipics import AniPics, NekosLife
import asyncio

async def main():
ap = AniPics()
nl = NekosLife()

print("AniPics:")
print((await ap.async_get()).url)

print("NekosLife:")
print((await nl.async_get(NekosLife.Types.avatar)).url)

if __name__ == "__main__":
asyncio.run(main())
14 changes: 14 additions & 0 deletions examples/sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from anipics import AniPics, NekosLife

def main():
ap = AniPics()
nl = NekosLife()

print("AniPics:")
print(ap.get().url)

print("NekosLife:")
print(nl.get(NekosLife.Types.avatar).url)

if __name__ == "__main__":
main()
Loading

0 comments on commit 6c47d53

Please sign in to comment.