[pinterest] move file extraction into separate method
This commit is contained in:
@@ -30,12 +30,11 @@ class PinterestExtractor(Extractor):
|
|||||||
self.root = text.ensure_http_scheme(domain)
|
self.root = text.ensure_http_scheme(domain)
|
||||||
|
|
||||||
self.api = PinterestAPI(self)
|
self.api = PinterestAPI(self)
|
||||||
|
self.videos = self.config("videos", True)
|
||||||
|
|
||||||
def items(self):
|
def items(self):
|
||||||
data = self.metadata()
|
data = self.metadata()
|
||||||
videos = self.config("videos", True)
|
|
||||||
|
|
||||||
yield Message.Directory, data
|
|
||||||
for pin in self.pins():
|
for pin in self.pins():
|
||||||
|
|
||||||
if isinstance(pin, tuple):
|
if isinstance(pin, tuple):
|
||||||
@@ -43,41 +42,33 @@ class PinterestExtractor(Extractor):
|
|||||||
yield Message.Queue, url, data
|
yield Message.Queue, url, data
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
try:
|
||||||
|
files = self._extract_files(pin)
|
||||||
|
except Exception as exc:
|
||||||
|
self.log.debug("", exc_info=exc)
|
||||||
|
self.log.warning(
|
||||||
|
"%s: Error when extracting download URLs (%s: %s)",
|
||||||
|
pin.get("id"), exc.__class__.__name__, exc)
|
||||||
|
continue
|
||||||
|
|
||||||
pin.update(data)
|
pin.update(data)
|
||||||
|
pin["count"] = len(files)
|
||||||
|
|
||||||
carousel_data = pin.get("carousel_data")
|
yield Message.Directory, pin
|
||||||
if carousel_data:
|
for pin["num"], file in enumerate(files, 1):
|
||||||
pin["count"] = len(carousel_data["carousel_slots"])
|
url = file["url"]
|
||||||
for num, slot in enumerate(carousel_data["carousel_slots"], 1):
|
text.nameext_from_url(url, pin)
|
||||||
slot["media_id"] = slot.pop("id")
|
pin.update(file)
|
||||||
pin.update(slot)
|
|
||||||
pin["num"] = num
|
|
||||||
size, image = next(iter(slot["images"].items()))
|
|
||||||
url = image["url"].replace("/" + size + "/", "/originals/")
|
|
||||||
yield Message.Url, url, text.nameext_from_url(url, pin)
|
|
||||||
|
|
||||||
else:
|
if "media_id" not in file:
|
||||||
try:
|
|
||||||
media = self._media_from_pin(pin)
|
|
||||||
except Exception:
|
|
||||||
self.log.debug("Unable to fetch download URL for pin %s",
|
|
||||||
pin.get("id"))
|
|
||||||
continue
|
|
||||||
|
|
||||||
if videos or media.get("duration") is None:
|
|
||||||
pin.update(media)
|
|
||||||
pin["num"] = pin["count"] = 1
|
|
||||||
pin["media_id"] = ""
|
pin["media_id"] = ""
|
||||||
|
|
||||||
url = media["url"]
|
if pin["extension"] == "m3u8":
|
||||||
text.nameext_from_url(url, pin)
|
url = "ytdl:" + url
|
||||||
|
pin["_ytdl_manifest"] = "hls"
|
||||||
|
pin["extension"] = "mp4"
|
||||||
|
|
||||||
if pin["extension"] == "m3u8":
|
yield Message.Url, url, pin
|
||||||
url = "ytdl:" + url
|
|
||||||
pin["_ytdl_manifest"] = "hls"
|
|
||||||
pin["extension"] = "mp4"
|
|
||||||
|
|
||||||
yield Message.Url, url, pin
|
|
||||||
|
|
||||||
def metadata(self):
|
def metadata(self):
|
||||||
"""Return general metadata"""
|
"""Return general metadata"""
|
||||||
@@ -85,26 +76,38 @@ class PinterestExtractor(Extractor):
|
|||||||
def pins(self):
|
def pins(self):
|
||||||
"""Return all relevant pin objects"""
|
"""Return all relevant pin objects"""
|
||||||
|
|
||||||
@staticmethod
|
def _extract_files(self, pin):
|
||||||
def _media_from_pin(pin):
|
carousel_data = pin.get("carousel_data")
|
||||||
|
if carousel_data:
|
||||||
|
files = []
|
||||||
|
for slot in carousel_data["carousel_slots"]:
|
||||||
|
size, image = next(iter(slot["images"].items()))
|
||||||
|
slot["media_id"] = slot.pop("id")
|
||||||
|
slot["url"] = image["url"].replace(
|
||||||
|
"/" + size + "/", "/originals/", 1)
|
||||||
|
files.append(slot)
|
||||||
|
return files
|
||||||
|
|
||||||
videos = pin.get("videos")
|
videos = pin.get("videos")
|
||||||
if videos:
|
if videos:
|
||||||
video_formats = videos["video_list"]
|
if not self.videos:
|
||||||
|
return ()
|
||||||
|
pass
|
||||||
|
|
||||||
|
video_formats = videos["video_list"]
|
||||||
for fmt in ("V_HLSV4", "V_HLSV3_WEB", "V_HLSV3_MOBILE"):
|
for fmt in ("V_HLSV4", "V_HLSV3_WEB", "V_HLSV3_MOBILE"):
|
||||||
if fmt in video_formats:
|
if fmt in video_formats:
|
||||||
media = video_formats[fmt]
|
file = video_formats[fmt]
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
media = max(video_formats.values(),
|
file = max(video_formats.values(),
|
||||||
key=lambda x: x.get("width", 0))
|
key=lambda x: x.get("width", 0))
|
||||||
|
|
||||||
if "V_720P" in video_formats:
|
if "V_720P" in video_formats:
|
||||||
media["_fallback"] = (video_formats["V_720P"]["url"],)
|
file["_fallback"] = (video_formats["V_720P"]["url"],)
|
||||||
|
return (file,)
|
||||||
|
|
||||||
return media
|
return (pin["images"]["orig"],)
|
||||||
|
|
||||||
return pin["images"]["orig"]
|
|
||||||
|
|
||||||
|
|
||||||
class PinterestPinExtractor(PinterestExtractor):
|
class PinterestPinExtractor(PinterestExtractor):
|
||||||
|
|||||||
Reference in New Issue
Block a user