2016-09-02 19:11:16 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-01-30 22:49:16 +01:00
|
|
|
# Copyright 2016-2018 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.
|
|
|
|
|
|
|
|
"""Extract images from https://www.pinterest.com"""
|
|
|
|
|
|
|
|
from .common import Extractor, Message
|
2018-04-24 22:17:25 +02:00
|
|
|
from .. import text, exception
|
|
|
|
import json
|
2016-09-02 19:11:16 +02:00
|
|
|
|
2017-02-01 00:53:19 +01: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"
|
2018-04-24 22:17:25 +02:00
|
|
|
filename_fmt = "{category}_{id}.{extension}"
|
|
|
|
archive_fmt = "{id}"
|
2016-09-02 19:11:16 +02:00
|
|
|
|
2016-09-03 10:00:32 +02:00
|
|
|
def __init__(self):
|
2016-09-02 19:11:16 +02:00
|
|
|
Extractor.__init__(self)
|
2017-09-09 17:31:42 +02:00
|
|
|
self.api = PinterestAPI(self)
|
2016-09-02 19:11:16 +02:00
|
|
|
|
2016-09-03 10:00:32 +02:00
|
|
|
def data_from_pin(self, pin):
|
|
|
|
"""Get image url and metadata from a pin-object"""
|
2018-04-24 22:17:25 +02:00
|
|
|
img = pin["images"]["orig"]
|
2016-09-03 10:00:32 +02:00
|
|
|
url = img["url"]
|
2018-04-24 22:17:25 +02:00
|
|
|
pin["width"] = img["width"]
|
|
|
|
pin["height"] = img["height"]
|
|
|
|
return url, text.nameext_from_url(url, pin)
|
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"
|
2017-05-08 15:08:39 +02:00
|
|
|
pattern = [r"(?:https?://)?(?:[^./]+\.)?pinterest\.[^/]+/pin/([^/?#&]+)"]
|
2017-01-03 12:12:36 +01:00
|
|
|
test = [
|
|
|
|
("https://www.pinterest.com/pin/858146903966145189/", {
|
2018-03-06 14:28:21 +01:00
|
|
|
"url": "afb3c26719e3a530bb0e871c480882a801a4e8a5",
|
2018-04-30 18:19:28 +02:00
|
|
|
# image version depends on CDN server used
|
|
|
|
# "content": "d3e24bc9f7af585e8c23b9136956bd45a4d9b947",
|
|
|
|
# "content": "4c435a66f6bb82bb681db2ecc888f76cf6c5f9ca",
|
2017-01-03 12:12:36 +01:00
|
|
|
}),
|
|
|
|
("https://www.pinterest.com/pin/858146903966145188/", {
|
2018-04-17 17:12:42 +02:00
|
|
|
"exception": exception.NotFoundError,
|
|
|
|
}),
|
2017-01-03 12:12:36 +01:00
|
|
|
]
|
2016-09-03 10:00:32 +02:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
PinterestExtractor.__init__(self)
|
|
|
|
self.pin_id = match.group(1)
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
pin = self.api.pin(self.pin_id)
|
|
|
|
url, data = self.data_from_pin(pin)
|
|
|
|
yield Message.Version, 1
|
|
|
|
yield Message.Directory, data
|
|
|
|
yield Message.Url, url, data
|
|
|
|
|
|
|
|
|
|
|
|
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"
|
2018-04-24 22:17:25 +02:00
|
|
|
directory_fmt = ["{category}", "{board[owner][username]}", "{board[name]}"]
|
|
|
|
archive_fmt = "{board[id]}_{id}"
|
2017-05-08 15:08:39 +02:00
|
|
|
pattern = [r"(?:https?://)?(?:[^./]+\.)?pinterest\.[^/]+/"
|
|
|
|
r"(?!pin/)([^/?#&]+)/([^/?#&]+)"]
|
2017-01-03 12:12:36 +01:00
|
|
|
test = [
|
|
|
|
("https://www.pinterest.com/g1952849/test-/", {
|
2018-03-06 14:28:21 +01:00
|
|
|
"url": "85911dfca313f3f7f48c2aa0bc684f539d1d80a6",
|
2017-01-03 12:12:36 +01:00
|
|
|
}),
|
|
|
|
("https://www.pinterest.com/g1952848/test/", {
|
2018-05-11 17:28:34 +02:00
|
|
|
"exception": exception.GalleryDLException,
|
2017-01-03 12:12:36 +01:00
|
|
|
}),
|
|
|
|
]
|
2016-09-03 10:00:32 +02:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
PinterestExtractor.__init__(self)
|
2018-04-26 16:36:42 +02:00
|
|
|
self.user = text.unquote(match.group(1))
|
|
|
|
self.board = text.unquote(match.group(2))
|
2016-09-03 10:00:32 +02:00
|
|
|
|
|
|
|
def items(self):
|
|
|
|
board = self.api.board(self.user, self.board)
|
2018-04-24 22:17:25 +02:00
|
|
|
data = {"board": board, "count": board["pin_count"]}
|
2016-09-02 19:11:16 +02:00
|
|
|
yield Message.Version, 1
|
|
|
|
yield Message.Directory, data
|
2018-04-24 22:17:25 +02:00
|
|
|
for pin in self.api.board_pins(board["id"]):
|
2018-04-25 16:04:30 +02:00
|
|
|
url, pin_data = self.data_from_pin(pin)
|
|
|
|
pin_data.update(data)
|
|
|
|
yield Message.Url, url, pin_data
|
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"
|
2017-05-08 15:08:39 +02:00
|
|
|
pattern = [r"(?:https?://)?(pin\.it/[^/?#&]+)"]
|
2017-01-27 21:51:28 +01:00
|
|
|
test = [
|
|
|
|
("https://pin.it/Hvt8hgT", {
|
|
|
|
"url": "8daad8558382c68f0868bdbd17d05205184632fa",
|
|
|
|
}),
|
|
|
|
("https://pin.it/Hvt8hgS", {
|
|
|
|
"exception": exception.NotFoundError,
|
2017-05-08 15:08:39 +02:00
|
|
|
}),
|
2017-01-27 21:51:28 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
PinterestExtractor.__init__(self)
|
|
|
|
self.url = "https://" + match.group(1)
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
response = self.session.head(self.url)
|
|
|
|
location = response.headers.get("Location")
|
2017-01-27 22:39:44 +01:00
|
|
|
if not location or location in ("https://api.pinterest.com/None",
|
|
|
|
"https://www.pinterest.com"):
|
2017-01-27 21:51:28 +01:00
|
|
|
raise exception.NotFoundError("pin")
|
2017-09-12 16:19:00 +02:00
|
|
|
yield Message.Queue, location, {}
|
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
|
|
|
|
"""
|
|
|
|
|
|
|
|
BASE_URL = "https://uk.pinterest.com"
|
|
|
|
HEADERS = {
|
|
|
|
"Accept" : "application/json, text/javascript, "
|
|
|
|
"*/*, q=0.01",
|
|
|
|
"Accept-Language" : "en-US,en;q=0.5",
|
|
|
|
"X-Pinterest-AppState": "active",
|
|
|
|
"X-APP-VERSION" : "cb1c7f9",
|
|
|
|
"X-Requested-With" : "XMLHttpRequest",
|
|
|
|
"Origin" : BASE_URL + "/",
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, extractor):
|
2018-04-26 16:36:42 +02:00
|
|
|
self.extractor = extractor
|
2016-09-02 19:11:16 +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-04-24 22:17:25 +02:00
|
|
|
def board(self, user, board):
|
2016-09-03 10:00:32 +02:00
|
|
|
"""Query information about a board"""
|
2018-04-24 22:17:25 +02:00
|
|
|
options = {"slug": board, "username": user,
|
|
|
|
"field_set_key": "detailed"}
|
|
|
|
return self._call("Board", options)["resource_response"]["data"]
|
2016-09-03 10:00:32 +02:00
|
|
|
|
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)
|
|
|
|
|
|
|
|
def _call(self, resource, options):
|
2018-04-25 16:04:30 +02:00
|
|
|
url = "{}/resource/{}Resource/get/".format(self.BASE_URL, resource)
|
|
|
|
params = {"data": 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(
|
|
|
|
url, params=params, headers=self.HEADERS, 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
|
|
|
|
2018-04-26 16:36:42 +02:00
|
|
|
if 200 <= response.status_code < 400 and not response.history:
|
2018-04-16 15:43:31 +02:00
|
|
|
return data
|
|
|
|
|
2018-04-26 16:36:42 +02:00
|
|
|
if response.status_code == 404 or response.history:
|
|
|
|
raise exception.NotFoundError(self.extractor.subcategory)
|
|
|
|
self.extractor.log.error("API request failed")
|
2018-05-11 17:28:34 +02:00
|
|
|
self.extractor.log.debug("%s", response.text)
|
2018-04-17 17:12:42 +02:00
|
|
|
raise exception.StopExtraction()
|
|
|
|
|
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)
|
|
|
|
yield from data["resource_response"]["data"]
|
|
|
|
|
|
|
|
try:
|
|
|
|
bookmarks = data["resource"]["options"]["bookmarks"]
|
|
|
|
if not bookmarks or bookmarks[0] == "-end-":
|
|
|
|
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
|