Bulk asynchronous downloader for multiple file hosts
Python asynciobunkrcyberdropdownloaderdropboxfilestergofilegoogle-drivemediafiremegamega-nzonedrivepatreonpawchivepinterestpixeldraintransfer-ittwitchtwittervk
feat: add playlist support (VKvideo)
NTFSvolume2402c0f18456
diff --git a/cyberdrop_dl/crawlers/vk/api.py b/cyberdrop_dl/crawlers/vk/api.pyindex 356c30ce8..81baf8e01 100644--- a/cyberdrop_dl/crawlers/vk/api.py+++ b/cyberdrop_dl/crawlers/vk/api.py@@ -23,6 +23,7 @@ from cyberdrop_dl.crawlers.vk.types import (PhotoSize,Post,Video,+ VideoPlaylist,)from cyberdrop_dl.exceptions import DDOSGuardError, ScrapeErrorfrom cyberdrop_dl.url_objects import AbsoluteHttpURL@@ -211,6 +212,44 @@ class VKAPI(API):section = await self.catalog.get_section(section.id, section.next_from)+ async def get_playlist(self, owner_id: str, playlist_id: str) -> VideoPlaylist:+ resp = await self.method(+ "video.getAlbumById",+ {+ "album_id": playlist_id,+ "owner_id": owner_id,+ },+ )+ if not resp:+ raise ScrapeError(404)+ return VideoPlaylist.parse(resp)++ async def get_playlist_videos(self, owner_id: str, playlist_id: str, *, offset: int = 0) -> AsyncGenerator[Video]:+ data = {+ "album_id": playlist_id,+ "offset": 0,+ "owner_id": owner_id,+ "sort_album": 0,+ }+ seen: set[str] = set()+ resp = await self.method("video.getFromAlbum", data)+ total = resp["count"]+ while True:+ offset += len(items := resp["items"])+ found_new_videos: bool = False+ for item in items:+ video = Video.parse(item["video"])+ if video.slug not in seen:+ seen.add(video.slug)+ found_new_videos = True+ yield video++ if not found_new_videos or offset >= total:+ break++ data["offset"] = offset+ resp = await self.method("video.getFromAlbum", data)+@deprecated("DO NOT USE. This endpoint works but all images sources return 404")async def get_photo(self, owner_id: str, photo_id: str, album_id: str | None = None) -> Photo:album_options = (album_id,) if album_id else ("profile", "wall")diff --git a/cyberdrop_dl/crawlers/vk/types.py b/cyberdrop_dl/crawlers/vk/types.pyindex f83c9931a..5c0c9e127 100644--- a/cyberdrop_dl/crawlers/vk/types.py+++ b/cyberdrop_dl/crawlers/vk/types.py@@ -192,6 +192,12 @@ class AudioPlaylist(Owned):return deserialize(cls, playlist, thumb=Graffiti.parse(g) if (g := playlist.get("photo")) else None)[email protected](slots=True, frozen=True)+class VideoPlaylist(Owned):+ title: str+ count: int++@dataclasses.dataclass(slots=True, frozen=True)class Graffiti(APIObject): # Old photo formatsizes: dict[int, AbsoluteHttpURL]diff --git a/cyberdrop_dl/crawlers/vk/vk.py b/cyberdrop_dl/crawlers/vk/vk.pyindex 218ff0493..2d22125c0 100644--- a/cyberdrop_dl/crawlers/vk/vk.py+++ b/cyberdrop_dl/crawlers/vk/vk.py@@ -29,6 +29,8 @@ from cyberdrop_dl.utils._url import remove_query_paramsfrom cyberdrop_dl.utils.errors import error_handling_wrapperif TYPE_CHECKING:+ from collections.abc import AsyncIterable+from cyberdrop_dl.url_objects import ScrapeItem@@ -135,6 +137,7 @@ class VKVideoCrawler(VKBaseCrawler):"@<owner_screen_name>/all","@<owner_screen_name>/donut",),+ "Playlist": "/playlist/<owner_id>_<playlist_id>",}SUPPORTED_DOMAINS: ClassVar[SupportedDomains] = "vkvideo.ru", "vkvideo.com"PRIMARY_URL: ClassVar[AbsoluteHttpURL] = AbsoluteHttpURL("https://vkvideo.com")@@ -152,6 +155,9 @@ class VKVideoCrawler(VKBaseCrawler):case [owner, "clips" | "all" | "donut" as kind]:kind = "videos" if kind == "all" else kindawait self.owner_videos(scrape_item, owner.removeprefix("@"), kind)+ case ["playlist", slug] if "_" in slug:+ owner_id, _, playlist_id = slug.partition("_")+ await self.playlist(scrape_item, owner_id, playlist_id)case [slug] if m := re.match(_PAGE_REGEX, slug):match m.groups():case ["clip" | "video", str(owner_id), str(video_id)]:@@ -161,18 +167,33 @@ class VKVideoCrawler(VKBaseCrawler):case _:raise ValueError+ @error_handling_wrapper+ async def playlist(self, scrape_item: ScrapeItem, owner_id: str, playlist_id: str) -> None:+ owner, play = await aio.gather(+ self.api.resolve_owner(owner_id), self.api.get_playlist(owner_id, playlist_id), fail_fast=False+ )+ scrape_item.setup_as_profile(self.create_title(owner.full_name))+ title = self.create_title(f"{play.title or play.slug} [playlist]", play.slug)+ scrape_item.append_folders(title)+ await self._iter_videos(scrape_item, self.api.get_playlist_videos(owner_id, playlist_id))+@error_handling_wrapperasync def owner_videos(self, scrape_item: ScrapeItem, owner_id: str, kind: str) -> None:owner = await self.api.resolve_owner(owner_id)scrape_item.setup_as_profile(self.create_title(owner.full_name))scrape_item.append_folders(kind)+ await self._iter_videos(scrape_item, self.api.get_videos(scrape_item.url))++ async def _iter_videos(self, scrape_item: ScrapeItem, videos: AsyncIterable[Video]) -> None:+ sleep = aio.periodic_sleep(10)async with self.new_task_group() as tg:- async for video in self.api.get_videos(scrape_item.url):+ async for video in videos:url = self.PRIMARY_URL / video.web_pathif video.access_key:url = url.update_query(list=video.access_key)tg.create_task(self.video_task(scrape_item.create_child(url), video))scrape_item.add_children()+ await sleep()@error_handling_wrapperasync def video(self, scrape_item: ScrapeItem, owner_id: str, video_id: str) -> None: