27 lines
555 B
Python
27 lines
555 B
Python
"""HTTP helper methods."""
|
|
from aiohttp import ClientSession
|
|
|
|
from jarvis_core.filters import url
|
|
|
|
|
|
async def get_size(link: str) -> int:
|
|
"""
|
|
Get the url filesize.
|
|
|
|
Args:
|
|
link: URL to get
|
|
|
|
Returns:
|
|
Size in bytes
|
|
|
|
Raises:
|
|
Exception: On status code
|
|
ValueError: On bad URL
|
|
"""
|
|
if not url.match(link):
|
|
raise ValueError("Invalid URL.")
|
|
|
|
async with ClientSession() as session:
|
|
resp = await session.get(link)
|
|
resp.raise_for_status()
|
|
return resp.content_length or -1
|