[pinterest] add 'search' extractor (#1411)

pull/1431/head
Mike Fährmann 4 years ago
parent 058cc47e9b
commit 36291176bc
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -508,7 +508,7 @@ Consider all sites to be NSFW unless otherwise known.
<tr> <tr>
<td>Pinterest</td> <td>Pinterest</td>
<td>https://www.pinterest.com/</td> <td>https://www.pinterest.com/</td>
<td>Pins, pin.it Links, related Pins, Sections, User Profiles</td> <td>Pins, pin.it Links, related Pins, Search Results, Sections, User Profiles</td>
<td>Supported</td> <td>Supported</td>
</tr> </tr>
<tr> <tr>

@ -220,6 +220,27 @@ class PinterestSectionExtractor(PinterestExtractor):
return self.api.board_section_pins(self.section["id"]) return self.api.board_section_pins(self.section["id"])
class PinterestSearchExtractor(PinterestExtractor):
"""Extractor for Pinterest search results"""
subcategory = "search"
directory_fmt = ("{category}", "Search", "{search}")
pattern = BASE_PATTERN + r"/search/pins/?\?q=([^&#]+)"
test = ("https://www.pinterest.de/search/pins/?q=nature", {
"range": "1-50",
"count": ">= 50",
})
def __init__(self, match):
PinterestExtractor.__init__(self, match)
self.search = match.group(1)
def metadata(self):
return {"search": self.search}
def pins(self):
return self.api.search(self.search)
class PinterestRelatedPinExtractor(PinterestPinExtractor): class PinterestRelatedPinExtractor(PinterestPinExtractor):
"""Extractor for related pins of another pin from pinterest.com""" """Extractor for related pins of another pin from pinterest.com"""
subcategory = "related-pin" subcategory = "related-pin"
@ -296,7 +317,7 @@ class PinterestAPI():
"Accept-Language" : "en-US,en;q=0.5", "Accept-Language" : "en-US,en;q=0.5",
"Referer" : BASE_URL + "/", "Referer" : BASE_URL + "/",
"X-Requested-With" : "XMLHttpRequest", "X-Requested-With" : "XMLHttpRequest",
"X-APP-VERSION" : "7a20185", "X-APP-VERSION" : "31461e0",
"X-CSRFToken" : None, "X-CSRFToken" : None,
"X-Pinterest-AppState": "active", "X-Pinterest-AppState": "active",
"Origin" : BASE_URL, "Origin" : BASE_URL,
@ -364,6 +385,11 @@ class PinterestAPI():
options = {"board_id": board_id, "add_vase": True} options = {"board_id": board_id, "add_vase": True}
return self._pagination("BoardRelatedPixieFeed", options) return self._pagination("BoardRelatedPixieFeed", options)
def search(self, query):
"""Yield pins from searches"""
options = {"query": query, "scope": "pins", "rs": "typed"}
return self._pagination("BaseSearch", options)
def login(self): def login(self):
"""Login and obtain session cookies""" """Login and obtain session cookies"""
username, password = self.extractor._get_auth_info() username, password = self.extractor._get_auth_info()
@ -421,7 +447,10 @@ class PinterestAPI():
def _pagination(self, resource, options): def _pagination(self, resource, options):
while True: while True:
data = self._call(resource, options) data = self._call(resource, options)
yield from data["resource_response"]["data"] results = data["resource_response"]["data"]
if isinstance(results, dict):
results = results["results"]
yield from results
try: try:
bookmarks = data["resource"]["options"]["bookmarks"] bookmarks = data["resource"]["options"]["bookmarks"]

Loading…
Cancel
Save