[reddit] support comment embeds (#5366)
This commit is contained in:
@@ -74,8 +74,8 @@ class RedditExtractor(Extractor):
|
|||||||
yield Message.Url, url, submission
|
yield Message.Url, url, submission
|
||||||
|
|
||||||
elif "gallery_data" in media:
|
elif "gallery_data" in media:
|
||||||
for submission["num"], url in enumerate(
|
for url in self._extract_gallery(media):
|
||||||
self._extract_gallery(media), 1):
|
submission["num"] += 1
|
||||||
text.nameext_from_url(url, submission)
|
text.nameext_from_url(url, submission)
|
||||||
yield Message.Url, url, submission
|
yield Message.Url, url, submission
|
||||||
|
|
||||||
@@ -99,7 +99,10 @@ class RedditExtractor(Extractor):
|
|||||||
urls.append((url, submission))
|
urls.append((url, submission))
|
||||||
for comment in comments:
|
for comment in comments:
|
||||||
html = comment["body_html"] or ""
|
html = comment["body_html"] or ""
|
||||||
if ' href="' in html:
|
href = (' href="' in html)
|
||||||
|
media = ("media_metadata" in comment)
|
||||||
|
|
||||||
|
if media or href:
|
||||||
comment["date"] = text.parse_timestamp(
|
comment["date"] = text.parse_timestamp(
|
||||||
comment["created_utc"])
|
comment["created_utc"])
|
||||||
if submission:
|
if submission:
|
||||||
@@ -107,6 +110,14 @@ class RedditExtractor(Extractor):
|
|||||||
data["comment"] = comment
|
data["comment"] = comment
|
||||||
else:
|
else:
|
||||||
data = comment
|
data = comment
|
||||||
|
|
||||||
|
if media:
|
||||||
|
for embed in self._extract_embed(comment):
|
||||||
|
submission["num"] += 1
|
||||||
|
text.nameext_from_url(embed, submission)
|
||||||
|
yield Message.Url, embed, submission
|
||||||
|
|
||||||
|
if href:
|
||||||
for url in text.extract_iter(html, ' href="', '"'):
|
for url in text.extract_iter(html, ' href="', '"'):
|
||||||
urls.append((url, data))
|
urls.append((url, data))
|
||||||
|
|
||||||
@@ -118,6 +129,7 @@ class RedditExtractor(Extractor):
|
|||||||
if url.startswith((
|
if url.startswith((
|
||||||
"https://www.reddit.com/message/compose",
|
"https://www.reddit.com/message/compose",
|
||||||
"https://reddit.com/message/compose",
|
"https://reddit.com/message/compose",
|
||||||
|
"https://preview.redd.it/",
|
||||||
)):
|
)):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@@ -172,6 +184,27 @@ class RedditExtractor(Extractor):
|
|||||||
submission["id"], item["media_id"])
|
submission["id"], item["media_id"])
|
||||||
self.log.debug(src)
|
self.log.debug(src)
|
||||||
|
|
||||||
|
def _extract_embed(self, submission):
|
||||||
|
meta = submission["media_metadata"]
|
||||||
|
if not meta:
|
||||||
|
return
|
||||||
|
|
||||||
|
for mid, data in meta.items():
|
||||||
|
if data["status"] != "valid" or "s" not in data:
|
||||||
|
self.log.warning(
|
||||||
|
"embed %s: skipping item %s (status: %s)",
|
||||||
|
submission["id"], mid, data.get("status"))
|
||||||
|
continue
|
||||||
|
src = data["s"]
|
||||||
|
url = src.get("u") or src.get("gif") or src.get("mp4")
|
||||||
|
if url:
|
||||||
|
yield url.partition("?")[0].replace("/preview.", "/i.", 1)
|
||||||
|
else:
|
||||||
|
self.log.error(
|
||||||
|
"embed %s: unable to fetch download URL for item %s",
|
||||||
|
submission["id"], mid)
|
||||||
|
self.log.debug(src)
|
||||||
|
|
||||||
def _extract_video_ytdl(self, submission):
|
def _extract_video_ytdl(self, submission):
|
||||||
return "https://www.reddit.com" + submission["permalink"]
|
return "https://www.reddit.com" + submission["permalink"]
|
||||||
|
|
||||||
|
|||||||
@@ -168,6 +168,18 @@ __tests__ = (
|
|||||||
"#count" : 0,
|
"#count" : 0,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
"#url" : "https://www.reddit.com/r/RobloxArt/comments/15ko0qu/",
|
||||||
|
"#comment" : "comment embeds (#5366)",
|
||||||
|
"#category": ("", "reddit", "submission"),
|
||||||
|
"#class" : reddit.RedditSubmissionExtractor,
|
||||||
|
"#options" : {"comments": 10},
|
||||||
|
"#urls" : (
|
||||||
|
"https://i.redd.it/ppt5yciyipgb1.jpg",
|
||||||
|
"https://i.redd.it/u0ojzd69kpgb1.png",
|
||||||
|
),
|
||||||
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"#url" : "https://www.reddit.com/user/TheSpiritTree/comments/srilyf/",
|
"#url" : "https://www.reddit.com/user/TheSpiritTree/comments/srilyf/",
|
||||||
"#comment" : "user page submission (#2301)",
|
"#comment" : "user page submission (#2301)",
|
||||||
|
|||||||
Reference in New Issue
Block a user