2021-03-23 18:48:01 +01:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
2022-02-06 21:39:24 +01:00
|
|
|
|
# Copyright 2021-2022 Mike Fährmann
|
2021-03-23 18:48:01 +01:00
|
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License version 2 as
|
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
|
|
|
|
|
|
"""Extractors for https://vk.com/"""
|
|
|
|
|
|
2021-04-01 14:35:56 +02:00
|
|
|
|
from .common import Extractor, Message
|
2022-05-04 11:46:30 +02:00
|
|
|
|
from .. import text, exception
|
2021-03-23 18:48:01 +01:00
|
|
|
|
|
2021-10-23 00:46:20 +02:00
|
|
|
|
BASE_PATTERN = r"(?:https://)?(?:www\.|m\.)?vk\.com"
|
2021-03-23 18:48:01 +01:00
|
|
|
|
|
2021-10-23 00:46:20 +02:00
|
|
|
|
|
|
|
|
|
class VkExtractor(Extractor):
|
|
|
|
|
"""Base class for vk extractors"""
|
2021-03-23 18:48:01 +01:00
|
|
|
|
category = "vk"
|
2021-07-15 00:43:42 +02:00
|
|
|
|
directory_fmt = ("{category}", "{user[name]|user[id]}")
|
2021-03-23 18:48:01 +01:00
|
|
|
|
filename_fmt = "{id}.{extension}"
|
|
|
|
|
archive_fmt = "{id}"
|
2021-04-01 14:35:56 +02:00
|
|
|
|
root = "https://vk.com"
|
|
|
|
|
request_interval = 1.0
|
2021-10-23 00:46:20 +02:00
|
|
|
|
|
|
|
|
|
def items(self):
|
2022-04-21 14:01:50 +02:00
|
|
|
|
sizes = "wzyxrqpo"
|
|
|
|
|
|
2021-10-23 00:46:20 +02:00
|
|
|
|
data = self.metadata()
|
|
|
|
|
yield Message.Directory, data
|
2022-04-21 14:01:50 +02:00
|
|
|
|
|
2021-10-23 00:46:20 +02:00
|
|
|
|
for photo in self.photos():
|
2022-04-21 14:01:50 +02:00
|
|
|
|
|
|
|
|
|
for size in sizes:
|
|
|
|
|
size += "_"
|
|
|
|
|
if size in photo:
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
self.log.warning("no photo URL found (%s)", photo.get("id"))
|
|
|
|
|
continue
|
|
|
|
|
|
2022-04-29 13:56:39 +02:00
|
|
|
|
try:
|
2022-06-27 10:24:35 +02:00
|
|
|
|
_, photo["width"], photo["height"] = photo[size]
|
2022-04-29 13:56:39 +02:00
|
|
|
|
except ValueError:
|
|
|
|
|
# photo without width/height entries (#2535)
|
|
|
|
|
photo["width"] = photo["height"] = 0
|
|
|
|
|
|
2022-06-27 10:24:35 +02:00
|
|
|
|
photo["url"] = photo[size + "src"]
|
2022-04-21 14:01:50 +02:00
|
|
|
|
photo["id"] = photo["id"].rpartition("_")[2]
|
2022-04-29 13:56:39 +02:00
|
|
|
|
photo.update(data)
|
2021-10-23 00:46:20 +02:00
|
|
|
|
|
2022-04-21 14:01:50 +02:00
|
|
|
|
text.nameext_from_url(photo["url"], photo)
|
|
|
|
|
yield Message.Url, photo["url"], photo
|
2021-10-23 00:46:20 +02:00
|
|
|
|
|
2022-04-21 14:01:50 +02:00
|
|
|
|
def _pagination(self, photos_id):
|
|
|
|
|
url = self.root + "/al_photos.php"
|
2021-10-23 00:46:20 +02:00
|
|
|
|
headers = {
|
|
|
|
|
"X-Requested-With": "XMLHttpRequest",
|
|
|
|
|
"Origin" : self.root,
|
2022-04-21 14:01:50 +02:00
|
|
|
|
"Referer" : self.root + "/" + photos_id,
|
2021-10-23 00:46:20 +02:00
|
|
|
|
}
|
2022-04-21 14:01:50 +02:00
|
|
|
|
data = {
|
|
|
|
|
"act" : "show",
|
|
|
|
|
"al" : "1",
|
|
|
|
|
"direction": "1",
|
|
|
|
|
"list" : photos_id,
|
|
|
|
|
"offset" : 0,
|
2021-10-23 00:46:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
payload = self.request(
|
2022-04-21 14:01:50 +02:00
|
|
|
|
url, method="POST", headers=headers, data=data,
|
2021-10-23 00:46:20 +02:00
|
|
|
|
).json()["payload"][1]
|
|
|
|
|
|
2022-05-04 11:46:30 +02:00
|
|
|
|
if len(payload) < 4:
|
|
|
|
|
self.log.debug(payload)
|
|
|
|
|
raise exception.AuthorizationError(payload[0])
|
|
|
|
|
|
2022-04-21 14:01:50 +02:00
|
|
|
|
total = payload[1]
|
|
|
|
|
photos = payload[3]
|
2021-10-23 00:46:20 +02:00
|
|
|
|
|
2022-04-21 14:01:50 +02:00
|
|
|
|
data["offset"] += len(photos)
|
|
|
|
|
if data["offset"] >= total:
|
|
|
|
|
# the last chunk of photos also contains the first few photos
|
|
|
|
|
# again if 'total' is not a multiple of 10
|
|
|
|
|
extra = total - data["offset"]
|
|
|
|
|
if extra:
|
|
|
|
|
del photos[extra:]
|
2021-10-23 00:46:20 +02:00
|
|
|
|
|
2022-04-21 14:01:50 +02:00
|
|
|
|
yield from photos
|
2021-10-23 00:46:20 +02:00
|
|
|
|
return
|
2022-04-21 14:01:50 +02:00
|
|
|
|
|
|
|
|
|
yield from photos
|
2021-10-23 00:46:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class VkPhotosExtractor(VkExtractor):
|
|
|
|
|
"""Extractor for photos from a vk user"""
|
|
|
|
|
subcategory = "photos"
|
|
|
|
|
pattern = (BASE_PATTERN + r"/(?:"
|
|
|
|
|
r"(?:albums|photos|id)(-?\d+)"
|
|
|
|
|
r"|(?!album-?\d+_)([^/?#]+))")
|
2021-03-23 18:48:01 +01:00
|
|
|
|
test = (
|
|
|
|
|
("https://vk.com/id398982326", {
|
2022-04-21 14:01:50 +02:00
|
|
|
|
"pattern": r"https://sun\d+-\d+\.userapi\.com/s/v1/if1"
|
|
|
|
|
r"/[\w-]+\.jpg\?size=\d+x\d+&quality=96&type=album",
|
2021-03-23 18:48:01 +01:00
|
|
|
|
"count": ">= 35",
|
2021-07-15 00:43:42 +02:00
|
|
|
|
"keywords": {
|
|
|
|
|
"id": r"re:\d+",
|
|
|
|
|
"user": {
|
|
|
|
|
"id": "398982326",
|
|
|
|
|
"info": "Мы за Движуху! – m1ni SounD #4 [EROmusic]",
|
|
|
|
|
"name": "",
|
|
|
|
|
"nick": "Dobrov Kurva",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
("https://vk.com/cosplayinrussia", {
|
2022-04-29 13:56:39 +02:00
|
|
|
|
"range": "15-25",
|
2021-07-15 00:43:42 +02:00
|
|
|
|
"keywords": {
|
|
|
|
|
"id": r"re:\d+",
|
|
|
|
|
"user": {
|
|
|
|
|
"id" : "-165740836",
|
|
|
|
|
"info": "Предложка открыта, кидайте ваши косплейчики. При "
|
|
|
|
|
"правильном оформлении они будут опубликованы",
|
|
|
|
|
"name": "cosplayinrussia",
|
|
|
|
|
"nick": "Косплей | Cosplay 18+",
|
|
|
|
|
},
|
|
|
|
|
},
|
2021-03-23 18:48:01 +01:00
|
|
|
|
}),
|
2022-04-29 13:56:39 +02:00
|
|
|
|
# photos without width/height (#2535)
|
|
|
|
|
("https://vk.com/id76957806", {
|
2022-05-02 11:16:04 +02:00
|
|
|
|
"pattern": r"https://sun\d+-\d+\.userapi\.com/",
|
|
|
|
|
"range": "1-9",
|
|
|
|
|
"count": 9,
|
2022-04-29 13:56:39 +02:00
|
|
|
|
}),
|
2021-03-23 18:48:01 +01:00
|
|
|
|
("https://m.vk.com/albums398982326"),
|
2021-04-01 14:35:56 +02:00
|
|
|
|
("https://www.vk.com/id398982326?profile=1"),
|
2021-07-15 00:43:42 +02:00
|
|
|
|
("https://vk.com/albums-165740836"),
|
2021-03-23 18:48:01 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def __init__(self, match):
|
2021-10-23 00:46:20 +02:00
|
|
|
|
VkExtractor.__init__(self, match)
|
2021-07-15 00:43:42 +02:00
|
|
|
|
self.user_id, self.user_name = match.groups()
|
2021-03-23 18:48:01 +01:00
|
|
|
|
|
2021-10-23 00:46:20 +02:00
|
|
|
|
def photos(self):
|
2022-04-21 14:01:50 +02:00
|
|
|
|
return self._pagination("photos" + self.user_id)
|
2021-10-23 00:46:20 +02:00
|
|
|
|
|
|
|
|
|
def metadata(self):
|
2021-07-15 00:43:42 +02:00
|
|
|
|
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)
|
2021-04-01 14:35:56 +02:00
|
|
|
|
else:
|
2021-07-15 00:43:42 +02:00
|
|
|
|
url = "{}/{}".format(self.root, self.user_name)
|
|
|
|
|
data = self._extract_profile(url)
|
2021-10-23 00:46:20 +02:00
|
|
|
|
self.user_id = data["user"]["id"]
|
|
|
|
|
return data
|
2021-07-15 00:43:42 +02:00
|
|
|
|
|
|
|
|
|
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'))),
|
2022-05-03 13:42:45 +02:00
|
|
|
|
"id" : (extr('<a href="/albums', '"') or
|
|
|
|
|
extr('data-from-id="', '"')),
|
2021-07-15 00:43:42 +02:00
|
|
|
|
}}
|
2021-10-23 00:46:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class VkAlbumExtractor(VkExtractor):
|
|
|
|
|
"""Extractor for a vk album"""
|
|
|
|
|
subcategory = "album"
|
|
|
|
|
directory_fmt = ("{category}", "{user[id]}", "{album[id]}")
|
|
|
|
|
pattern = BASE_PATTERN + r"/album(-?\d+)_(\d+)$"
|
|
|
|
|
test = (
|
2022-04-18 17:24:00 +02:00
|
|
|
|
("https://vk.com/album232175027_00", {
|
|
|
|
|
"count": 8,
|
2021-10-23 00:46:20 +02:00
|
|
|
|
}),
|
|
|
|
|
("https://vk.com/album-165740836_281339889", {
|
|
|
|
|
"count": 12,
|
|
|
|
|
}),
|
2022-05-04 11:46:30 +02:00
|
|
|
|
# "Access denied" (#2556)
|
|
|
|
|
("https://vk.com/album-53775183_00", {
|
|
|
|
|
"exception": exception.AuthorizationError,
|
|
|
|
|
}),
|
2021-10-23 00:46:20 +02:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
|
VkExtractor.__init__(self, match)
|
|
|
|
|
self.user_id, self.album_id = match.groups()
|
|
|
|
|
|
|
|
|
|
def photos(self):
|
2022-04-21 14:01:50 +02:00
|
|
|
|
return self._pagination("album{}_{}".format(
|
|
|
|
|
self.user_id, self.album_id))
|
2021-10-23 00:46:20 +02:00
|
|
|
|
|
|
|
|
|
def metadata(self):
|
|
|
|
|
return {
|
|
|
|
|
"user": {"id": self.user_id},
|
|
|
|
|
"album": {"id": self.album_id},
|
|
|
|
|
}
|