From 36a2aff363bf07f203aaed1556d8cca63987cea9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Thu, 15 Jul 2021 00:43:42 +0200 Subject: [PATCH] [vk] improve metadata extraction and URL pattern (fixes #1691) - always fetch all user metadata - use 'user[name]' for directory names if available --- gallery_dl/extractor/vk.py | 65 +++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 18 deletions(-) diff --git a/gallery_dl/extractor/vk.py b/gallery_dl/extractor/vk.py index 1ce1140f..21786417 100644 --- a/gallery_dl/extractor/vk.py +++ b/gallery_dl/extractor/vk.py @@ -17,43 +17,60 @@ class VkPhotosExtractor(Extractor): """Extractor for photos from a vk user""" category = "vk" subcategory = "photos" - directory_fmt = ("{category}", "{user[id]}") + directory_fmt = ("{category}", "{user[name]|user[id]}") filename_fmt = "{id}.{extension}" archive_fmt = "{id}" root = "https://vk.com" request_interval = 1.0 - pattern = r"(?:https://)?(?:www\.|m\.)?vk\.com/(?:albums|photos|id)(\d+)" + pattern = (r"(?:https://)?(?:www\.|m\.)?vk\.com/(?:" + r"(?:albums|photos|id)(-?\d+)|([^/?#]+))") test = ( ("https://vk.com/id398982326", { "pattern": r"https://sun\d+-\d+\.userapi\.com/c\d+/v\d+" r"/[0-9a-f]+/[\w-]+\.jpg", "count": ">= 35", + "keywords": { + "id": r"re:\d+", + "user": { + "id": "398982326", + "info": "Мы за Движуху! – m1ni SounD #4 [EROmusic]", + "name": "", + "nick": "Dobrov Kurva", + }, + }, + }), + ("https://vk.com/cosplayinrussia", { + "range": "75-100", + "keywords": { + "id": r"re:\d+", + "user": { + "id" : "-165740836", + "info": "Предложка открыта, кидайте ваши косплейчики. При " + "правильном оформлении они будут опубликованы", + "name": "cosplayinrussia", + "nick": "Косплей | Cosplay 18+", + }, + }, }), ("https://m.vk.com/albums398982326"), ("https://www.vk.com/id398982326?profile=1"), + ("https://vk.com/albums-165740836"), ) def __init__(self, match): Extractor.__init__(self, match) - self.user_id = match.group(1) + self.user_id, self.user_name = match.groups() def items(self): - user_id = self.user_id - - if self.config("metadata"): - url = "{}/id{}".format(self.root, user_id) - extr = text.extract_from(self.request(url).text) - data = {"user": { - "id" : user_id, - "nick": text.unescape(extr( - "", " | VK<")), - "name": text.unescape(extr( - '<h1 class="page_name">', "<")).replace(" ", " "), - "info": text.unescape(text.remove_html(extr( - '<span class="current_text">', '</span'))) - }} + if self.user_id: + user_id = self.user_id + prefix = "public" if user_id[0] == "-" else "id" + url = "{}/{}{}".format(self.root, prefix, user_id.lstrip("-")) + data = self._extract_profile(url) else: - data = {"user": {"id": user_id}} + url = "{}/{}".format(self.root, self.user_name) + data = self._extract_profile(url) + user_id = data["user"]["id"] photos_url = "{}/photos{}".format(self.root, user_id) headers = { @@ -86,3 +103,15 @@ class VkPhotosExtractor(Extractor): if cnt <= 40 or offset == params["offset"]: return params["offset"] = offset + + def _extract_profile(self, url): + extr = text.extract_from(self.request(url).text) + return {"user": { + "name": text.unescape(extr( + 'rel="canonical" href="https://vk.com/', '"')), + "nick": text.unescape(extr( + '<h1 class="page_name">', "<")).replace(" ", " "), + "info": text.unescape(text.remove_html(extr( + '<span class="current_text">', '</span'))), + "id" : extr('<a href="/albums', '"'), + }}