Skip to content

Commit

Permalink
[pinterest] add 'domain' option (#3484)
Browse files Browse the repository at this point in the history
use input URL domain by default
  • Loading branch information
mikf committed Jan 4, 2023
1 parent f1a715d commit 9116398
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
13 changes: 13 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/gallery-dl.conf
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@
},
"pinterest":
{
"domain": "auto",
"sections": true,
"videos": true
},
Expand Down
49 changes: 27 additions & 22 deletions gallery_dl/extractor/pinterest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"""
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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)
Expand Down

0 comments on commit 9116398

Please sign in to comment.