2016-09-02 19:11:16 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2023-01-03 19:11:20 +01:00
|
|
|
# Copyright 2016-2023 Mike Fährmann
|
2016-09-02 19:11:16 +02: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.
|
|
|
|
|
2020-06-16 14:41:05 +02:00
|
|
|
"""Extractors for https://www.pinterest.com/"""
|
2016-09-02 19:11:16 +02:00
|
|
|
|
|
|
|
from .common import Extractor, Message
|
2020-10-15 00:51:53 +02:00
|
|
|
from .. import text, util, exception
|
2020-06-16 14:41:05 +02:00
|
|
|
import itertools
|
2016-09-02 19:11:16 +02:00
|
|
|
|
2020-07-27 14:41:34 +02:00
|
|
|
BASE_PATTERN = r"(?:https?://)?(?:\w+\.)?pinterest\.[\w.]+"
|
2018-08-15 21:28:27 +02:00
|
|
|
|
|
|
|
|
2016-09-03 10:00:32 +02:00
|
|
|
class PinterestExtractor(Extractor):
|
|
|
|
"""Base class for pinterest extractors"""
|
2016-09-02 19:11:16 +02:00
|
|
|
category = "pinterest"
|
2024-10-19 17:42:01 +02:00
|
|
|
filename_fmt = "{category}_{id}{media_id|page_id:?_//}.{extension}"
|
|
|
|
archive_fmt = "{id}{media_id|page_id}"
|
2020-12-29 16:57:03 +01:00
|
|
|
root = "https://www.pinterest.com"
|
2016-09-02 19:11:16 +02:00
|
|
|
|
2023-07-25 20:09:44 +02:00
|
|
|
def _init(self):
|
2023-01-04 16:40:03 +01:00
|
|
|
domain = self.config("domain")
|
|
|
|
if not domain or domain == "auto" :
|
2023-07-25 20:09:44 +02:00
|
|
|
self.root = text.root_from_url(self.url)
|
2023-01-04 16:40:03 +01:00
|
|
|
else:
|
|
|
|
self.root = text.ensure_http_scheme(domain)
|
|
|
|
|
2017-09-09 17:31:42 +02:00
|
|
|
self.api = PinterestAPI(self)
|
2024-10-19 17:42:01 +02:00
|
|
|
self.stories = self.config("stories", True)
|
2024-10-18 20:55:20 +02:00
|
|
|
self.videos = self.config("videos", True)
|
2016-09-02 19:11:16 +02:00
|
|
|
|
2018-08-15 21:28:27 +02:00
|
|
|
def items(self):
|
|
|
|
data = self.metadata()
|
|
|
|
|
|
|
|
for pin in self.pins():
|
2022-07-03 18:12:16 +02:00
|
|
|
|
|
|
|
if isinstance(pin, tuple):
|
|
|
|
url, data = pin
|
|
|
|
yield Message.Queue, url, data
|
|
|
|
continue
|
|
|
|
|
2024-10-18 20:55:20 +02:00
|
|
|
try:
|
|
|
|
files = self._extract_files(pin)
|
|
|
|
except Exception as exc:
|
|
|
|
self.log.debug("", exc_info=exc)
|
|
|
|
self.log.warning(
|
|
|
|
"%s: Error when extracting download URLs (%s: %s)",
|
|
|
|
pin.get("id"), exc.__class__.__name__, exc)
|
|
|
|
continue
|
|
|
|
|
2022-04-06 21:21:33 +02:00
|
|
|
pin.update(data)
|
2024-10-18 20:55:20 +02:00
|
|
|
pin["count"] = len(files)
|
2022-04-06 21:21:33 +02:00
|
|
|
|
2024-10-18 20:55:20 +02:00
|
|
|
yield Message.Directory, pin
|
|
|
|
for pin["num"], file in enumerate(files, 1):
|
|
|
|
url = file["url"]
|
|
|
|
text.nameext_from_url(url, pin)
|
|
|
|
pin.update(file)
|
2020-12-21 16:09:06 +01:00
|
|
|
|
2024-10-18 20:55:20 +02:00
|
|
|
if "media_id" not in file:
|
2022-04-06 21:21:33 +02:00
|
|
|
pin["media_id"] = ""
|
2024-10-19 17:42:01 +02:00
|
|
|
if "page_id" not in file:
|
|
|
|
pin["page_id"] = ""
|
2020-12-21 16:09:06 +01:00
|
|
|
|
2024-10-18 20:55:20 +02:00
|
|
|
if pin["extension"] == "m3u8":
|
|
|
|
url = "ytdl:" + url
|
|
|
|
pin["_ytdl_manifest"] = "hls"
|
|
|
|
pin["extension"] = "mp4"
|
2020-12-21 16:09:06 +01:00
|
|
|
|
2024-10-18 20:55:20 +02:00
|
|
|
yield Message.Url, url, pin
|
2018-08-15 21:28:27 +02:00
|
|
|
|
2018-08-16 18:11:39 +02:00
|
|
|
def metadata(self):
|
|
|
|
"""Return general metadata"""
|
|
|
|
|
|
|
|
def pins(self):
|
2020-12-21 16:09:06 +01:00
|
|
|
"""Return all relevant pin objects"""
|
2018-08-16 18:11:39 +02:00
|
|
|
|
2024-10-18 20:55:20 +02:00
|
|
|
def _extract_files(self, pin):
|
2024-10-19 17:42:01 +02:00
|
|
|
story_pin_data = pin.get("story_pin_data")
|
|
|
|
if story_pin_data and self.stories:
|
|
|
|
return self._extract_story(pin, story_pin_data)
|
|
|
|
|
2024-10-18 20:55:20 +02:00
|
|
|
carousel_data = pin.get("carousel_data")
|
|
|
|
if carousel_data:
|
2024-10-19 17:42:01 +02:00
|
|
|
return self._extract_carousel(pin, carousel_data)
|
2024-10-18 20:55:20 +02:00
|
|
|
|
2020-12-21 16:09:06 +01:00
|
|
|
videos = pin.get("videos")
|
2024-10-19 17:42:01 +02:00
|
|
|
if videos and self.videos:
|
|
|
|
return (self._extract_video(videos),)
|
|
|
|
|
|
|
|
try:
|
|
|
|
return (pin["images"]["orig"],)
|
|
|
|
except Exception:
|
|
|
|
self.log.debug("%s: No files found", pin.get("id"))
|
|
|
|
return ()
|
|
|
|
|
|
|
|
def _extract_story(self, pin, story):
|
|
|
|
files = []
|
|
|
|
story_id = story.get("id")
|
|
|
|
|
|
|
|
for page in story["pages"]:
|
|
|
|
page_id = page.get("id")
|
|
|
|
|
|
|
|
for block in page["blocks"]:
|
|
|
|
type = block.get("type")
|
|
|
|
|
|
|
|
if type == "story_pin_image_block":
|
|
|
|
if 1 == len(page["blocks"]) == len(story["pages"]):
|
|
|
|
try:
|
|
|
|
media = pin["images"]["orig"]
|
|
|
|
except Exception:
|
|
|
|
media = self._extract_image(page, block)
|
|
|
|
else:
|
|
|
|
media = self._extract_image(page, block)
|
|
|
|
|
2024-11-05 14:08:55 +01:00
|
|
|
elif type == "story_pin_video_block" or "video" in block:
|
2024-10-19 17:42:01 +02:00
|
|
|
video = block["video"]
|
|
|
|
media = self._extract_video(video)
|
|
|
|
media["media_id"] = video.get("id") or ""
|
|
|
|
|
2024-11-05 14:08:55 +01:00
|
|
|
elif type == "story_pin_music_block" or "audio" in block:
|
2024-11-05 13:57:25 +01:00
|
|
|
media = block["audio"]
|
|
|
|
media["url"] = media["audio_url"]
|
|
|
|
media["media_id"] = media.get("id") or ""
|
|
|
|
|
2024-10-19 17:42:01 +02:00
|
|
|
elif type == "story_pin_paragraph_block":
|
|
|
|
media = {"url": "text:" + block["text"],
|
|
|
|
"extension": "txt",
|
|
|
|
"media_id": block.get("id")}
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.log.warning("%s: Unsupported story block '%s'",
|
|
|
|
pin.get("id"), type)
|
2024-11-05 14:08:55 +01:00
|
|
|
try:
|
|
|
|
media = self._extract_image(page, block)
|
|
|
|
except Exception:
|
|
|
|
continue
|
2024-10-19 17:42:01 +02:00
|
|
|
|
|
|
|
media["story_id"] = story_id
|
|
|
|
media["page_id"] = page_id
|
|
|
|
files.append(media)
|
|
|
|
|
|
|
|
return files
|
|
|
|
|
|
|
|
def _extract_carousel(self, pin, carousel_data):
|
|
|
|
files = []
|
|
|
|
for slot in carousel_data["carousel_slots"]:
|
|
|
|
size, image = next(iter(slot["images"].items()))
|
|
|
|
slot["media_id"] = slot.pop("id")
|
|
|
|
slot["url"] = image["url"].replace(
|
|
|
|
"/" + size + "/", "/originals/", 1)
|
|
|
|
files.append(slot)
|
|
|
|
return files
|
|
|
|
|
|
|
|
def _extract_image(self, page, block):
|
|
|
|
sig = block.get("image_signature") or page["image_signature"]
|
|
|
|
url_base = "https://i.pinimg.com/originals/{}/{}/{}/{}.".format(
|
|
|
|
sig[0:2], sig[2:4], sig[4:6], sig)
|
|
|
|
url_jpg = url_base + "jpg"
|
|
|
|
url_png = url_base + "png"
|
|
|
|
url_webp = url_base + "webp"
|
|
|
|
|
|
|
|
try:
|
|
|
|
media = block["image"]["images"]["originals"]
|
|
|
|
except Exception:
|
|
|
|
media = {"url": url_jpg, "_fallback": (url_png, url_webp,)}
|
|
|
|
|
|
|
|
if media["url"] == url_jpg:
|
|
|
|
media["_fallback"] = (url_png, url_webp,)
|
|
|
|
else:
|
|
|
|
media["_fallback"] = (url_jpg, url_png, url_webp,)
|
|
|
|
media["media_id"] = sig
|
|
|
|
|
|
|
|
return media
|
|
|
|
|
|
|
|
def _extract_video(self, video):
|
|
|
|
video_formats = video["video_list"]
|
|
|
|
for fmt in ("V_HLSV4", "V_HLSV3_WEB", "V_HLSV3_MOBILE"):
|
|
|
|
if fmt in video_formats:
|
|
|
|
media = video_formats[fmt]
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
media = max(video_formats.values(),
|
|
|
|
key=lambda x: x.get("width", 0))
|
|
|
|
if "V_720P" in video_formats:
|
|
|
|
media["_fallback"] = (video_formats["V_720P"]["url"],)
|
|
|
|
return media
|
2016-09-03 10:00:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
class PinterestPinExtractor(PinterestExtractor):
|
2016-09-12 10:20:57 +02:00
|
|
|
"""Extractor for images from a single pin from pinterest.com"""
|
2016-09-03 10:00:32 +02:00
|
|
|
subcategory = "pin"
|
2023-05-03 20:26:25 +02:00
|
|
|
pattern = BASE_PATTERN + r"/pin/([^/?#]+)(?!.*#related$)"
|
2023-09-11 16:30:55 +02:00
|
|
|
example = "https://www.pinterest.com/pin/12345/"
|
2016-09-03 10:00:32 +02:00
|
|
|
|
|
|
|
def __init__(self, match):
|
2019-02-11 13:31:10 +01:00
|
|
|
PinterestExtractor.__init__(self, match)
|
2016-09-03 10:00:32 +02:00
|
|
|
self.pin_id = match.group(1)
|
2018-08-15 21:28:27 +02:00
|
|
|
self.pin = None
|
2016-09-03 10:00:32 +02:00
|
|
|
|
2018-08-15 21:28:27 +02:00
|
|
|
def metadata(self):
|
|
|
|
self.pin = self.api.pin(self.pin_id)
|
2020-12-21 16:09:06 +01:00
|
|
|
return self.pin
|
2018-08-15 21:28:27 +02:00
|
|
|
|
|
|
|
def pins(self):
|
|
|
|
return (self.pin,)
|
2016-09-03 10:00:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
class PinterestBoardExtractor(PinterestExtractor):
|
2016-09-12 10:20:57 +02:00
|
|
|
"""Extractor for images from a board from pinterest.com"""
|
2016-09-03 10:00:32 +02:00
|
|
|
subcategory = "board"
|
2019-02-08 13:45:40 +01:00
|
|
|
directory_fmt = ("{category}", "{board[owner][username]}", "{board[name]}")
|
2018-04-24 22:17:25 +02:00
|
|
|
archive_fmt = "{board[id]}_{id}"
|
2023-05-03 20:26:25 +02:00
|
|
|
pattern = (BASE_PATTERN + r"/(?!pin/)([^/?#]+)"
|
|
|
|
"/(?!_saved|_created|pins/)([^/?#]+)/?$")
|
2023-09-11 16:30:55 +02:00
|
|
|
example = "https://www.pinterest.com/USER/BOARD/"
|
2016-09-03 10:00:32 +02:00
|
|
|
|
|
|
|
def __init__(self, match):
|
2019-02-11 13:31:10 +01:00
|
|
|
PinterestExtractor.__init__(self, match)
|
2018-04-26 16:36:42 +02:00
|
|
|
self.user = text.unquote(match.group(1))
|
2020-06-16 14:41:05 +02:00
|
|
|
self.board_name = text.unquote(match.group(2))
|
|
|
|
self.board = None
|
2016-09-03 10:00:32 +02:00
|
|
|
|
2018-08-15 21:28:27 +02:00
|
|
|
def metadata(self):
|
2020-06-16 14:41:05 +02:00
|
|
|
self.board = self.api.board(self.user, self.board_name)
|
|
|
|
return {"board": self.board}
|
2018-08-15 21:28:27 +02:00
|
|
|
|
|
|
|
def pins(self):
|
2020-06-16 14:41:05 +02:00
|
|
|
board = self.board
|
2022-07-03 18:12:16 +02:00
|
|
|
pins = self.api.board_pins(board["id"])
|
2020-06-16 14:41:05 +02:00
|
|
|
|
|
|
|
if board["section_count"] and self.config("sections", True):
|
2024-02-05 15:54:06 +01:00
|
|
|
base = "{}{}id:".format(self.root, board["url"])
|
2022-07-03 18:12:16 +02:00
|
|
|
data = {"_extractor": PinterestSectionExtractor}
|
|
|
|
sections = [(base + section["id"], data)
|
|
|
|
for section in self.api.board_sections(board["id"])]
|
|
|
|
pins = itertools.chain(pins, sections)
|
|
|
|
|
|
|
|
return pins
|
2018-08-15 21:28:27 +02:00
|
|
|
|
|
|
|
|
2021-01-02 02:36:53 +01:00
|
|
|
class PinterestUserExtractor(PinterestExtractor):
|
|
|
|
"""Extractor for a user's boards"""
|
|
|
|
subcategory = "user"
|
2023-05-03 20:26:25 +02:00
|
|
|
pattern = BASE_PATTERN + r"/(?!pin/)([^/?#]+)(?:/_saved)?/?$"
|
2023-09-11 16:30:55 +02:00
|
|
|
example = "https://www.pinterest.com/USER/"
|
2020-12-29 16:57:03 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
PinterestExtractor.__init__(self, match)
|
|
|
|
self.user = text.unquote(match.group(1))
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
for board in self.api.boards(self.user):
|
|
|
|
url = board.get("url")
|
|
|
|
if url:
|
|
|
|
board["_extractor"] = PinterestBoardExtractor
|
|
|
|
yield Message.Queue, self.root + url, board
|
|
|
|
|
|
|
|
|
2023-01-03 19:11:20 +01:00
|
|
|
class PinterestAllpinsExtractor(PinterestExtractor):
|
|
|
|
"""Extractor for a user's 'All Pins' feed"""
|
|
|
|
subcategory = "allpins"
|
|
|
|
directory_fmt = ("{category}", "{user}")
|
2023-05-03 20:26:25 +02:00
|
|
|
pattern = BASE_PATTERN + r"/(?!pin/)([^/?#]+)/pins/?$"
|
2023-09-11 16:30:55 +02:00
|
|
|
example = "https://www.pinterest.com/USER/pins/"
|
2023-01-03 19:11:20 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
PinterestExtractor.__init__(self, match)
|
|
|
|
self.user = text.unquote(match.group(1))
|
|
|
|
|
|
|
|
def metadata(self):
|
|
|
|
return {"user": self.user}
|
|
|
|
|
|
|
|
def pins(self):
|
|
|
|
return self.api.user_pins(self.user)
|
|
|
|
|
|
|
|
|
2022-04-01 16:59:58 +02:00
|
|
|
class PinterestCreatedExtractor(PinterestExtractor):
|
|
|
|
"""Extractor for a user's created pins"""
|
|
|
|
subcategory = "created"
|
|
|
|
directory_fmt = ("{category}", "{user}")
|
2023-05-03 20:26:25 +02:00
|
|
|
pattern = BASE_PATTERN + r"/(?!pin/)([^/?#]+)/_created/?$"
|
2023-09-11 16:30:55 +02:00
|
|
|
example = "https://www.pinterest.com/USER/_created/"
|
2022-04-01 16:59:58 +02:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
PinterestExtractor.__init__(self, match)
|
|
|
|
self.user = text.unquote(match.group(1))
|
|
|
|
|
|
|
|
def metadata(self):
|
|
|
|
return {"user": self.user}
|
|
|
|
|
|
|
|
def pins(self):
|
|
|
|
return self.api.user_activity_pins(self.user)
|
|
|
|
|
|
|
|
|
2020-06-20 23:22:40 +02:00
|
|
|
class PinterestSectionExtractor(PinterestExtractor):
|
|
|
|
"""Extractor for board sections on pinterest.com"""
|
|
|
|
subcategory = "section"
|
|
|
|
directory_fmt = ("{category}", "{board[owner][username]}",
|
|
|
|
"{board[name]}", "{section[title]}")
|
|
|
|
archive_fmt = "{board[id]}_{id}"
|
2023-05-03 20:26:25 +02:00
|
|
|
pattern = BASE_PATTERN + r"/(?!pin/)([^/?#]+)/([^/?#]+)/([^/?#]+)"
|
2024-02-05 15:54:06 +01:00
|
|
|
example = "https://www.pinterest.com/USER/BOARD/SECTION"
|
2020-06-20 23:22:40 +02:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
PinterestExtractor.__init__(self, match)
|
|
|
|
self.user = text.unquote(match.group(1))
|
|
|
|
self.board_slug = text.unquote(match.group(2))
|
|
|
|
self.section_slug = text.unquote(match.group(3))
|
|
|
|
self.section = None
|
|
|
|
|
|
|
|
def metadata(self):
|
2022-07-03 18:12:16 +02:00
|
|
|
if self.section_slug.startswith("id:"):
|
|
|
|
section = self.section = self.api.board_section(
|
|
|
|
self.section_slug[3:])
|
|
|
|
else:
|
|
|
|
section = self.section = self.api.board_section_by_name(
|
|
|
|
self.user, self.board_slug, self.section_slug)
|
2020-06-20 23:22:40 +02:00
|
|
|
section.pop("preview_pins", None)
|
|
|
|
return {"board": section.pop("board"), "section": section}
|
|
|
|
|
|
|
|
def pins(self):
|
|
|
|
return self.api.board_section_pins(self.section["id"])
|
|
|
|
|
|
|
|
|
2021-03-29 01:41:28 +02:00
|
|
|
class PinterestSearchExtractor(PinterestExtractor):
|
|
|
|
"""Extractor for Pinterest search results"""
|
|
|
|
subcategory = "search"
|
|
|
|
directory_fmt = ("{category}", "Search", "{search}")
|
|
|
|
pattern = BASE_PATTERN + r"/search/pins/?\?q=([^&#]+)"
|
2023-09-11 16:30:55 +02:00
|
|
|
example = "https://www.pinterest.com/search/pins/?q=QUERY"
|
2021-03-29 01:41:28 +02:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
PinterestExtractor.__init__(self, match)
|
2023-02-15 15:44:20 +01:00
|
|
|
self.search = text.unquote(match.group(1))
|
2021-03-29 01:41:28 +02:00
|
|
|
|
|
|
|
def metadata(self):
|
|
|
|
return {"search": self.search}
|
|
|
|
|
|
|
|
def pins(self):
|
|
|
|
return self.api.search(self.search)
|
|
|
|
|
|
|
|
|
2018-08-15 21:28:27 +02:00
|
|
|
class PinterestRelatedPinExtractor(PinterestPinExtractor):
|
|
|
|
"""Extractor for related pins of another pin from pinterest.com"""
|
|
|
|
subcategory = "related-pin"
|
2019-02-08 13:45:40 +01:00
|
|
|
directory_fmt = ("{category}", "related {original_pin[id]}")
|
2023-05-03 20:26:25 +02:00
|
|
|
pattern = BASE_PATTERN + r"/pin/([^/?#]+).*#related$"
|
2023-09-11 16:30:55 +02:00
|
|
|
example = "https://www.pinterest.com/pin/12345/#related"
|
2018-08-15 21:28:27 +02:00
|
|
|
|
|
|
|
def metadata(self):
|
2020-12-21 16:09:06 +01:00
|
|
|
return {"original_pin": self.api.pin(self.pin_id)}
|
2018-08-15 21:28:27 +02:00
|
|
|
|
|
|
|
def pins(self):
|
|
|
|
return self.api.pin_related(self.pin_id)
|
|
|
|
|
|
|
|
|
|
|
|
class PinterestRelatedBoardExtractor(PinterestBoardExtractor):
|
|
|
|
"""Extractor for related pins of a board from pinterest.com"""
|
|
|
|
subcategory = "related-board"
|
2019-02-08 13:45:40 +01:00
|
|
|
directory_fmt = ("{category}", "{board[owner][username]}",
|
|
|
|
"{board[name]}", "related")
|
2023-05-03 20:26:25 +02:00
|
|
|
pattern = BASE_PATTERN + r"/(?!pin/)([^/?#]+)/([^/?#]+)/?#related$"
|
2023-09-11 16:30:55 +02:00
|
|
|
example = "https://www.pinterest.com/USER/BOARD/#related"
|
2018-08-15 21:28:27 +02:00
|
|
|
|
|
|
|
def pins(self):
|
2023-05-03 18:41:09 +02:00
|
|
|
return self.api.board_content_recommendation(self.board["id"])
|
2016-09-03 10:00:32 +02:00
|
|
|
|
2016-09-02 19:11:16 +02:00
|
|
|
|
2017-01-27 21:51:28 +01:00
|
|
|
class PinterestPinitExtractor(PinterestExtractor):
|
2017-05-08 15:08:39 +02:00
|
|
|
"""Extractor for images from a pin.it URL"""
|
2017-01-27 21:51:28 +01:00
|
|
|
subcategory = "pinit"
|
2023-05-03 20:26:25 +02:00
|
|
|
pattern = r"(?:https?://)?pin\.it/([^/?#]+)"
|
2023-09-11 16:30:55 +02:00
|
|
|
example = "https://pin.it/abcde"
|
2017-01-27 21:51:28 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
2019-02-11 13:31:10 +01:00
|
|
|
PinterestExtractor.__init__(self, match)
|
2018-12-02 19:37:50 +01:00
|
|
|
self.shortened_id = match.group(1)
|
2017-01-27 21:51:28 +01:00
|
|
|
|
|
|
|
def items(self):
|
2023-05-03 18:53:56 +02:00
|
|
|
url = "https://api.pinterest.com/url_shortener/{}/redirect/".format(
|
2018-12-02 19:37:50 +01:00
|
|
|
self.shortened_id)
|
|
|
|
response = self.request(url, method="HEAD", allow_redirects=False)
|
2017-01-27 21:51:28 +01:00
|
|
|
location = response.headers.get("Location")
|
2020-01-18 21:06:44 +01:00
|
|
|
if not location or not PinterestPinExtractor.pattern.match(location):
|
2017-01-27 21:51:28 +01:00
|
|
|
raise exception.NotFoundError("pin")
|
2019-12-29 23:37:34 +01:00
|
|
|
yield Message.Queue, location, {"_extractor": PinterestPinExtractor}
|
2017-01-27 21:51:28 +01:00
|
|
|
|
|
|
|
|
2016-09-02 19:11:16 +02:00
|
|
|
class PinterestAPI():
|
2018-04-24 22:17:25 +02:00
|
|
|
"""Minimal interface for the Pinterest Web API
|
|
|
|
|
|
|
|
For a better and more complete implementation in PHP, see
|
|
|
|
- https://github.com/seregazhuk/php-pinterest-bot
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, extractor):
|
2021-01-11 22:12:40 +01:00
|
|
|
csrf_token = util.generate_token()
|
2023-01-04 16:40:03 +01:00
|
|
|
|
|
|
|
self.extractor = extractor
|
|
|
|
self.root = extractor.root
|
2020-10-15 00:51:53 +02:00
|
|
|
self.cookies = {"csrftoken": csrf_token}
|
2023-01-04 16:40:03 +01:00
|
|
|
self.headers = {
|
|
|
|
"Accept" : "application/json, text/javascript, "
|
|
|
|
"*/*, q=0.01",
|
|
|
|
"Accept-Language" : "en-US,en;q=0.5",
|
|
|
|
"X-Requested-With" : "XMLHttpRequest",
|
|
|
|
"X-APP-VERSION" : "0c4af40",
|
|
|
|
"X-CSRFToken" : csrf_token,
|
|
|
|
"X-Pinterest-AppState": "active",
|
|
|
|
"Origin" : self.root,
|
|
|
|
}
|
2020-10-15 00:51:53 +02:00
|
|
|
|
2018-04-24 22:17:25 +02:00
|
|
|
def pin(self, pin_id):
|
2016-09-02 19:11:16 +02:00
|
|
|
"""Query information about a pin"""
|
2018-04-24 22:17:25 +02:00
|
|
|
options = {"id": pin_id, "field_set_key": "detailed"}
|
|
|
|
return self._call("Pin", options)["resource_response"]["data"]
|
2016-09-02 19:11:16 +02:00
|
|
|
|
2018-08-15 21:28:27 +02:00
|
|
|
def pin_related(self, pin_id):
|
|
|
|
"""Yield related pins of another pin"""
|
|
|
|
options = {"pin": pin_id, "add_vase": True, "pins_only": True}
|
|
|
|
return self._pagination("RelatedPinFeed", options)
|
|
|
|
|
2020-06-16 14:41:05 +02:00
|
|
|
def board(self, user, board_name):
|
2016-09-03 10:00:32 +02:00
|
|
|
"""Query information about a board"""
|
2020-06-16 14:41:05 +02:00
|
|
|
options = {"slug": board_name, "username": user,
|
2018-04-24 22:17:25 +02:00
|
|
|
"field_set_key": "detailed"}
|
|
|
|
return self._call("Board", options)["resource_response"]["data"]
|
2016-09-03 10:00:32 +02:00
|
|
|
|
2020-12-29 16:57:03 +01:00
|
|
|
def boards(self, user):
|
|
|
|
"""Yield all boards from 'user'"""
|
|
|
|
options = {
|
|
|
|
"sort" : "last_pinned_to",
|
|
|
|
"field_set_key" : "profile_grid_item",
|
|
|
|
"filter_stories" : False,
|
|
|
|
"username" : user,
|
|
|
|
"page_size" : 25,
|
|
|
|
"include_archived": True,
|
|
|
|
}
|
|
|
|
return self._pagination("Boards", options)
|
|
|
|
|
2018-04-24 22:17:25 +02:00
|
|
|
def board_pins(self, board_id):
|
2016-09-02 19:11:16 +02:00
|
|
|
"""Yield all pins of a specific board"""
|
2018-04-24 22:17:25 +02:00
|
|
|
options = {"board_id": board_id}
|
|
|
|
return self._pagination("BoardFeed", options)
|
|
|
|
|
2022-07-03 18:12:16 +02:00
|
|
|
def board_section(self, section_id):
|
2020-06-20 23:22:40 +02:00
|
|
|
"""Yield a specific board section"""
|
2022-07-03 18:12:16 +02:00
|
|
|
options = {"section_id": section_id}
|
|
|
|
return self._call("BoardSection", options)["resource_response"]["data"]
|
|
|
|
|
|
|
|
def board_section_by_name(self, user, board_slug, section_slug):
|
|
|
|
"""Yield a board section by name"""
|
2020-06-20 23:22:40 +02:00
|
|
|
options = {"board_slug": board_slug, "section_slug": section_slug,
|
|
|
|
"username": user}
|
|
|
|
return self._call("BoardSection", options)["resource_response"]["data"]
|
|
|
|
|
2020-06-16 14:41:05 +02:00
|
|
|
def board_sections(self, board_id):
|
|
|
|
"""Yield all sections of a specific board"""
|
|
|
|
options = {"board_id": board_id}
|
|
|
|
return self._pagination("BoardSections", options)
|
|
|
|
|
|
|
|
def board_section_pins(self, section_id):
|
|
|
|
"""Yield all pins from a board section"""
|
|
|
|
options = {"section_id": section_id}
|
|
|
|
return self._pagination("BoardSectionPins", options)
|
|
|
|
|
2023-05-03 18:41:09 +02:00
|
|
|
def board_content_recommendation(self, board_id):
|
2018-08-15 21:28:27 +02:00
|
|
|
"""Yield related pins of a specific board"""
|
2023-05-03 18:41:09 +02:00
|
|
|
options = {"id": board_id, "type": "board", "add_vase": True}
|
|
|
|
return self._pagination("BoardContentRecommendation", options)
|
2018-08-15 21:28:27 +02:00
|
|
|
|
2023-01-03 19:11:20 +01:00
|
|
|
def user_pins(self, user):
|
|
|
|
"""Yield all pins from 'user'"""
|
|
|
|
options = {
|
|
|
|
"is_own_profile_pins": False,
|
|
|
|
"username" : user,
|
|
|
|
"field_set_key" : "grid_item",
|
|
|
|
"pin_filter" : None,
|
|
|
|
}
|
|
|
|
return self._pagination("UserPins", options)
|
|
|
|
|
2022-04-01 16:59:58 +02:00
|
|
|
def user_activity_pins(self, user):
|
|
|
|
"""Yield pins created by 'user'"""
|
|
|
|
options = {
|
|
|
|
"exclude_add_pin_rep": True,
|
|
|
|
"field_set_key" : "grid_item",
|
|
|
|
"is_own_profile_pins": False,
|
|
|
|
"username" : user,
|
|
|
|
}
|
|
|
|
return self._pagination("UserActivityPins", options)
|
|
|
|
|
2021-03-29 01:41:28 +02:00
|
|
|
def search(self, query):
|
|
|
|
"""Yield pins from searches"""
|
|
|
|
options = {"query": query, "scope": "pins", "rs": "typed"}
|
|
|
|
return self._pagination("BaseSearch", options)
|
|
|
|
|
2018-04-24 22:17:25 +02:00
|
|
|
def _call(self, resource, options):
|
2023-01-04 16:40:03 +01:00
|
|
|
url = "{}/resource/{}Resource/get/".format(self.root, resource)
|
2023-02-09 15:50:55 +01:00
|
|
|
params = {
|
|
|
|
"data" : util.json_dumps({"options": options}),
|
|
|
|
"source_url": "",
|
|
|
|
}
|
2016-09-02 19:11:16 +02:00
|
|
|
|
2018-04-26 16:36:42 +02:00
|
|
|
response = self.extractor.request(
|
2020-10-15 00:51:53 +02:00
|
|
|
url, params=params, headers=self.headers,
|
|
|
|
cookies=self.cookies, fatal=False)
|
2018-05-11 17:28:34 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
data = response.json()
|
|
|
|
except ValueError:
|
|
|
|
data = {}
|
2018-04-17 17:12:42 +02:00
|
|
|
|
2023-01-04 16:40:03 +01:00
|
|
|
if response.history:
|
|
|
|
self.root = text.root_from_url(response.url)
|
|
|
|
if response.status_code < 400:
|
2018-04-16 15:43:31 +02:00
|
|
|
return data
|
2023-01-04 16:40:03 +01:00
|
|
|
if response.status_code == 404:
|
2018-08-15 21:28:27 +02:00
|
|
|
resource = self.extractor.subcategory.rpartition("-")[2]
|
|
|
|
raise exception.NotFoundError(resource)
|
2019-12-10 21:30:08 +01:00
|
|
|
self.extractor.log.debug("Server response: %s", response.text)
|
2019-10-28 16:06:36 +01:00
|
|
|
raise exception.StopExtraction("API request failed")
|
2018-04-17 17:12:42 +02:00
|
|
|
|
2018-04-25 16:04:30 +02:00
|
|
|
def _pagination(self, resource, options):
|
2018-04-17 17:12:42 +02:00
|
|
|
while True:
|
2018-04-24 22:17:25 +02:00
|
|
|
data = self._call(resource, options)
|
2021-03-29 01:41:28 +02:00
|
|
|
results = data["resource_response"]["data"]
|
|
|
|
if isinstance(results, dict):
|
|
|
|
results = results["results"]
|
|
|
|
yield from results
|
2018-04-24 22:17:25 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
bookmarks = data["resource"]["options"]["bookmarks"]
|
2018-08-16 18:11:39 +02:00
|
|
|
if (not bookmarks or bookmarks[0] == "-end-" or
|
|
|
|
bookmarks[0].startswith("Y2JOb25lO")):
|
2018-04-24 22:17:25 +02:00
|
|
|
return
|
2018-04-25 16:04:30 +02:00
|
|
|
options["bookmarks"] = bookmarks
|
2018-04-24 22:17:25 +02:00
|
|
|
except KeyError:
|
2018-04-17 17:12:42 +02:00
|
|
|
return
|