[pinterest] improve PinterestAPI code; remove OAuth mentions

on another note: access_tokens have been set to only allow for
10 requests per hour (from 200 yesterday)
This commit is contained in:
Mike Fährmann
2018-04-17 17:12:42 +02:00
parent 4bd182c107
commit d10579edb5
6 changed files with 56 additions and 43 deletions

View File

@@ -48,6 +48,9 @@ class PinterestPinExtractor(PinterestExtractor):
("https://www.pinterest.com/pin/858146903966145188/", {
"exception": exception.StopExtraction,
}),
("https://www.pinterest.com/pin/85814690396614518/", {
"exception": exception.NotFoundError,
}),
]
def __init__(self, match):
@@ -134,59 +137,58 @@ class PinterestPinitExtractor(PinterestExtractor):
class PinterestAPI():
"""Minimal interface for the pinterest API"""
CLIENT_ID = "4959725425749142746"
CLIENT_SECRET = ("2ea77dc64ca02974a728e46c5a9d2adf"
"cdd42f4d4ffb40ad064072165ad4b10d")
def __init__(self, extractor, access_token="AfyIXxi1MJ6et0NlIl_vBchHbex-"
"FSWylPyr2GJE2uu3W8A97QAAAAA"):
access_token = extractor.config("access-token", access_token)
self.session = extractor.session
self.session.params["access_token"] = access_token
def __init__(self, extractor, access_token=None):
self.log = extractor.log
self.session = extractor.session
self.access_token = (
access_token or
extractor.config("access-token") or
"AfyIXxi1MJ6et0NlIl_vBchHbex-FSWylPyr2GJE2uu3W8A97QAAAAA"
)
def pin(self, pin_id, fields="id,image,note"):
"""Query information about a pin"""
endpoint = "pins/{}/".format(pin_id)
params = {"fields": fields}
response = self.session.get(
"https://api.pinterest.com/v1/pins/{pin}/".format(pin=pin_id),
params=params
)
return self._parse(response)["data"]
return self._call(endpoint, params)["data"]
def board(self, user, board, fields="id,name,counts"):
"""Query information about a board"""
endpoint = "boards/{}/{}/".format(user, 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"]
return self._call(endpoint, params)["data"]
def board_pins(self, user, board, fields="id,image,note"):
def board_pins(self, user, board, fields="id,image,note", limit=100):
"""Yield all pins of a specific board"""
params = {"fields": fields, "limit": 100}
url = ("https://api.pinterest.com/v1/boards/{user}/{board}/pins/"
.format(user=user, board=board))
endpoint = "boards/{}/{}/pins/".format(user, board)
params = {"fields": fields, "limit": limit}
return self._pagination(endpoint, params)
def _call(self, endpoint, params):
params["access_token"] = self.access_token
url = "https://api.pinterest.com/v1/" + endpoint
response = self.session.get(url, params=params)
status = response.status_code
data = response.json()
if 200 <= status < 400 and data.get("data"):
return data
msg = data.get("message", "")
if status == 404:
msg = msg.partition(" ")[0].lower()
raise exception.NotFoundError(msg)
self.log.error("API request failed: %s", msg or "")
raise exception.StopExtraction()
def _pagination(self, endpoint, params):
while True:
response = self._parse(self.session.get(url, params=params))
response = self._call(endpoint, params)
yield from response["data"]
cursor = response["page"]["cursor"]
if not cursor:
return
params["cursor"] = cursor
def _parse(self, response):
"""Parse an API response"""
data = response.json()
if 200 <= response.status_code < 400 and data.get("data"):
return data
msg = data.get("message")
if response.status_code == 404:
msg = msg.partition(" ")[0].lower()
raise exception.NotFoundError(msg)
else:
self.log.error("API request failed: %s", msg or "")
raise exception.StopExtraction()