From 74acac341835da4c859f0385c7daf5f37c0f8d6c Mon Sep 17 00:00:00 2001 From: krypton-byte Date: Tue, 16 Aug 2022 16:27:25 +0700 Subject: [PATCH] fix: hint --- snapsave/snapsave.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/snapsave/snapsave.py b/snapsave/snapsave.py index 32ed2cf..0a37e88 100644 --- a/snapsave/snapsave.py +++ b/snapsave/snapsave.py @@ -2,7 +2,7 @@ from asyncio.tasks import ensure_future, gather from io import BufferedWriter, BytesIO from ast import literal_eval -from typing import Any, Literal, Optional, Union +from typing import Any, Generator, Literal, Optional, Union import httpx from httpx import AsyncClient from .decoder import decoder @@ -13,6 +13,7 @@ def translate(text: str) -> bool: return text.lower() in ['iya', 'yes'] + class Regex: URL_FILTER = re.compile(r'(https?://[\w+&=\.%\-_/?;]+)') ORIGIN_URL = re.compile(r'https?://[\w\.-]+/') @@ -134,7 +135,7 @@ async def get_size(self) -> int: 'GET', self.url_v ).__aenter__() - ).headers["Content-Length"]) + ).headers.get("Content-Length", 0)) async def download( self, @@ -157,11 +158,19 @@ def __repr__(self) -> str: return f'{self.quality.value}::render={self.render}' + ('::'+[ 'SD', 'HD'][self.is_hd] if self.is_hd or self.is_sd else '') + class Videos(list): + def __init__(self, cover: str): super().__init__() self.cover = cover + def __getitem__(self, i) -> FacebookVideo: + return super().__getitem__(i) + + def __iter__(self) -> Generator[FacebookVideo, None, None]: + yield from super().__iter__() + class Fb(AsyncClient): @@ -173,7 +182,7 @@ def __init__(self): 'Chrome/101.0.4994.167 Safari/537.36' } - async def from_url(self, url: str) -> list[FacebookVideo]: + async def from_url(self, url: str) -> Videos: await self.get('https://snapsave.app/id') resp = await self.post( 'https://snapsave.app/action.php?lang=id', @@ -194,8 +203,8 @@ async def from_url(self, url: str) -> list[FacebookVideo]: )) return await self.extract_content(dec) - async def extract_content(self, src: str) -> list[FacebookVideo]: - data = Videos(Regex.COVER.findall(src)[0]) + async def extract_content(self, src: str) -> Videos: + data = [] n = Regex.TABLE.findall(src)[0].replace('\\"', '"') for url, res, render in zip( Regex.URL_FILTER.findall(n), @@ -214,9 +223,11 @@ async def extract_content(self, src: str) -> list[FacebookVideo]: translate(render), fsize )) - return sorted_video(data) + data2 = Videos(Regex.COVER.findall(src)[0]) + data2.extend(sorted_video(data)) + return data2 - async def from_html(self, html: str) -> list[FacebookVideo]: + async def from_html(self, html: str) -> Videos: resp = await self.post( 'https://snapsave.app/download-private-video', data={