Skip to content

Commit

Permalink
feat: implemented image download
Browse files Browse the repository at this point in the history
  • Loading branch information
201st-Luka committed Aug 25, 2023
1 parent 94d76b0 commit 58b7cde
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ good as I can.
- more bulk requests
- pytests for every request
- pytests for the models
- possibility to download files (images, etc) from `api-assets.clashofclans.com`

### Planned utils

Expand Down
19 changes: 14 additions & 5 deletions pyclasher/api/models/base_models.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
from datetime import datetime

from aiohttp import ClientSession

from .abc import BaseModel
from ...exceptions import InvalidTimeFormat
from ...exceptions import InvalidTimeFormat, MISSING


class ImageUrl:
__url = None

def __init__(self, url):
self.__url = url
return

async def get_image(self):
raise NotImplementedError
async def get_image(self, logger=MISSING) -> bytes:
async with ClientSession() as session:
async with session.get(self.url) as request:
if request.status == 200:
logger.info(f"Successfully downloaded {self.url}")
return await request.read()

async def save_image(self, filename, logger=MISSING):
image = await self.get_image(logger)
with open(filename, "wb") as file:
file.write(image)

@property
def url(self):
Expand Down
10 changes: 6 additions & 4 deletions pyclasher/api/models/base_models.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
base models for this API wrapper client
"""
from logging import Logger

from .abc import BaseModel
from ...exceptions import MISSING, Missing
Expand All @@ -10,19 +11,20 @@ class ImageUrl:
"""
image URL model
:cvar __url: URL of the image
:ivar __url: URL of the image
:type __url: str
"""

__url: str = None

def __init__(self, url: str) -> None:
"""
initialisation of the image url model
"""
self.__url = url

async def get_image(self):
async def get_image(self, logger: Logger = MISSING) -> bytes:
...

async def save_image(self, logger: Logger):
"""
NOT IMPLEMENTED YET
Expand Down

0 comments on commit 58b7cde

Please sign in to comment.