diff --git a/docs/configuration.rst b/docs/configuration.rst index c88653c14d..4bfdb892e8 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -2098,6 +2098,19 @@ Description Extract media from reblogged posts. +extractor.pinterest.domain +-------------------------- +Type + ``string`` +Default + ``"auto"`` +Description + Specifies the domain used by ``pinterest`` extractots. + + Setting this option to ``"auto"`` + uses the same domain as a given input URL. + + extractor.pinterest.sections ---------------------------- Type diff --git a/docs/gallery-dl.conf b/docs/gallery-dl.conf index 98974e9bbe..8fb1ac7dae 100644 --- a/docs/gallery-dl.conf +++ b/docs/gallery-dl.conf @@ -228,6 +228,7 @@ }, "pinterest": { + "domain": "auto", "sections": true, "videos": true }, diff --git a/gallery_dl/extractor/pinterest.py b/gallery_dl/extractor/pinterest.py index 7fad798e5e..63b16ce6cc 100644 --- a/gallery_dl/extractor/pinterest.py +++ b/gallery_dl/extractor/pinterest.py @@ -26,6 +26,13 @@ class PinterestExtractor(Extractor): def __init__(self, match): Extractor.__init__(self, match) + + domain = self.config("domain") + if not domain or domain == "auto" : + self.root = text.root_from_url(match.group(0)) + else: + self.root = text.ensure_http_scheme(domain) + self.api = PinterestAPI(self) def items(self): @@ -380,26 +387,23 @@ class PinterestAPI(): - https://github.com/seregazhuk/php-pinterest-bot """ - BASE_URL = "https://www.pinterest.com" - HEADERS = { - "Accept" : "application/json, text/javascript, " - "*/*, q=0.01", - "Accept-Language" : "en-US,en;q=0.5", - "Referer" : BASE_URL + "/", - "X-Requested-With" : "XMLHttpRequest", - "X-APP-VERSION" : "31461e0", - "X-CSRFToken" : None, - "X-Pinterest-AppState": "active", - "Origin" : BASE_URL, - } - def __init__(self, extractor): - self.extractor = extractor - csrf_token = util.generate_token() - self.headers = self.HEADERS.copy() - self.headers["X-CSRFToken"] = csrf_token + + self.extractor = extractor + self.root = extractor.root self.cookies = {"csrftoken": csrf_token} + self.headers = { + "Accept" : "application/json, text/javascript, " + "*/*, q=0.01", + "Accept-Language" : "en-US,en;q=0.5", + "Referer" : self.root + "/", + "X-Requested-With" : "XMLHttpRequest", + "X-APP-VERSION" : "0c4af40", + "X-CSRFToken" : csrf_token, + "X-Pinterest-AppState": "active", + "Origin" : self.root, + } def pin(self, pin_id): """Query information about a pin""" @@ -495,7 +499,7 @@ def login(self): def _login_impl(self, username, password): self.extractor.log.info("Logging in as %s", username) - url = self.BASE_URL + "/resource/UserSessionResource/create/" + url = self.root + "/resource/UserSessionResource/create/" options = { "username_or_email": username, "password" : password, @@ -518,7 +522,7 @@ def _login_impl(self, username, password): } def _call(self, resource, options): - url = "{}/resource/{}Resource/get/".format(self.BASE_URL, resource) + url = "{}/resource/{}Resource/get/".format(self.root, resource) params = {"data": json.dumps({"options": options}), "source_url": ""} response = self.extractor.request( @@ -530,10 +534,11 @@ def _call(self, resource, options): except ValueError: data = {} - if response.status_code < 400 and not response.history: + if response.history: + self.root = text.root_from_url(response.url) + if response.status_code < 400: return data - - if response.status_code == 404 or response.history: + if response.status_code == 404: resource = self.extractor.subcategory.rpartition("-")[2] raise exception.NotFoundError(resource) self.extractor.log.debug("Server response: %s", response.text)