[pinterest] add board extractor

This commit is contained in:
Mike Fährmann
2016-09-03 10:00:32 +02:00
parent 79721a65ff
commit 517e88680f

View File

@@ -11,34 +11,83 @@
from .common import Extractor, Message from .common import Extractor, Message
from .. import text from .. import text
class PinterestPinExtractor(Extractor): class PinterestExtractor(Extractor):
"""Extract an image from a single pin from https://www.pinterest.com""" """Base class for pinterest extractors"""
category = "pinterest" category = "pinterest"
subcategory = "pin"
directory_fmt = ["{category}"] directory_fmt = ["{category}"]
filename_fmt = "{category}_{id}.{extension}" filename_fmt = "{category}_{pin-id}.{extension}"
pattern = [r"(?:https?://)?(?:www\.)?pinterest\.com/pin/([^/]+)"]
def __init__(self, match): def __init__(self):
Extractor.__init__(self) Extractor.__init__(self)
self.pin_id = match.group(1)
self.api = PinterestAPI(self.session) self.api = PinterestAPI(self.session)
def items(self): def data_from_pin(self, pin):
pin = self.api.pin(self.pin_id) """Get image url and metadata from a pin-object"""
img = pin["image"]["original"] img = pin["image"]["original"]
url = img["url"]
data = { data = {
"category": self.category, "category": self.category,
"subcategory": self.subcategory, "subcategory": self.subcategory,
"id": pin["id"], "pin-id": pin["id"],
"note": pin["note"], "note": pin["note"],
"width": img["width"], "width": img["width"],
"height": img["height"], "height": img["height"],
} }
text.nameext_from_url(img["url"], data) return url, text.nameext_from_url(url, data)
class PinterestPinExtractor(PinterestExtractor):
"""Extract an image from a single pin from https://www.pinterest.com"""
subcategory = "pin"
pattern = [r"(?:https?://)?(?:www\.)?pinterest\.com/pin/([^/]+)"]
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.Version, 1
yield Message.Directory, data yield Message.Directory, data
yield Message.Url, img["url"], data yield Message.Url, url, data
class PinterestBoardExtractor(PinterestExtractor):
"""Extract an image from a single pin from https://www.pinterest.com"""
category = "pinterest"
subcategory = "board"
directory_fmt = ["{category}", "{user}", "{board}"]
pattern = [r"(?:https?://)?(?:www\.)?pinterest\.com/(?!pin/)([^/]+)/([^/]+)"]
def __init__(self, match):
PinterestExtractor.__init__(self)
self.user, self.board = match.groups()
def items(self):
board = self.api.board(self.user, self.board)
data = self.data_from_board(board)
num = data["count"]
yield Message.Version, 1
yield Message.Directory, data
for pin in self.api.board_pins(self.user, self.board):
url, pdata = self.data_from_pin(pin)
data.update(pdata)
data["num"] = num
num -= 1
yield Message.Url, url, data
def data_from_board(self, board):
"""Get metadata from a board-object"""
data = {
"category": self.category,
"subcategory": self.subcategory,
"user": self.user,
"board-id": board["id"],
"board": board["name"],
"count": board["counts"]["pins"],
}
return data
class PinterestAPI(): class PinterestAPI():
@@ -48,7 +97,7 @@ class PinterestAPI():
self.session = session self.session = session
self.session.params["access_token"] = access_token self.session.params["access_token"] = access_token
def pin(self, pin_id, fields="image,note"): def pin(self, pin_id, fields="id,image,note"):
"""Query information about a pin""" """Query information about a pin"""
params = {"fields": fields} params = {"fields": fields}
response = self.session.get( response = self.session.get(
@@ -57,7 +106,16 @@ class PinterestAPI():
) )
return self._parse(response)["data"] return self._parse(response)["data"]
def board_pins(self, user, board, fields="image,note"): def board(self, user, board, fields="id,name,counts"):
"""Query information about a board"""
params = {"fields": fields}
response = self.session.get(
"https://api.pinterest.com/v1/boards/{user}/{board}/"
.format(user=user, board=board), params=params
)
return self._parse(response)["data"]
def board_pins(self, user, board, fields="id,image,note"):
"""Yield all pins of a specific board""" """Yield all pins of a specific board"""
params = {"fields": fields} params = {"fields": fields}
url = ("https://api.pinterest.com/v1/boards/{user}/{board}/pins/" url = ("https://api.pinterest.com/v1/boards/{user}/{board}/pins/"