Skip to content

Commit

Permalink
[twitter] support 'grok' cards content (#7040)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed Feb 25, 2025
1 parent 876169d commit d2cad59
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
13 changes: 12 additions & 1 deletion gallery_dl/extractor/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ def _image_fallback(self, base):
for fmt in self._size_fallback:
yield base + fmt

def _extract_components(self, tweet, data, files):
for component_id in data["components"]:
com = data["component_objects"][component_id]
for conv in com["data"]["conversation_preview"]:
for url in conv.get("mediaUrls") or ():
files.append({"url": url})

def _extract_card(self, tweet, files):
card = tweet["card"]
if "legacy" in card:
Expand Down Expand Up @@ -272,7 +279,11 @@ def _extract_card(self, tweet, files):
return
elif name == "unified_card":
data = util.json_loads(bvals["unified_card"]["string_value"])
self._extract_media(tweet, data["media_entities"].values(), files)
if "media_entities" in data:
self._extract_media(
tweet, data["media_entities"].values(), files)
if "component_objects" in data:
self._extract_components(tweet, data, files)
return

if self.cards == "ytdl":
Expand Down
11 changes: 10 additions & 1 deletion test/results/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@
"#comment" : "'birdwatch' note (#5317)",
"#category": ("", "twitter", "tweet"),
"#class" : twitter.TwitterTweetExtractor,
"#options" : {"text-tweets": True},
"#options" : {"text-tweets": True},

"birdwatch": "In addition to the known harm of lead exposure, especially to children, Mr. Kobach is incorrect when he states the mandate is unfunded. In fact, the BIPARTISAN Infrastructure Law Joe Biden signed into law in Nov 2021 provides $15B toward lead service line replacement projects. epa.gov/ground-water-a…",
"content" : "Biden wants to replace lead pipes. He failed to mention that the unfunded mandate sets an almost impossible timeline, will cost billions, infringe on the rights of the States and their residents – all for benefits that may be entirely speculative. #sotu https://ag.ks.gov/media-center/news-releases/2024/02/09/kobach-leads-coalition-demanding-biden-drop-unnecessary-epa-rule",
Expand All @@ -642,6 +642,15 @@
"#count" : 0,
},

{
"#url" : "https://x.com/fw_rion_/status/1866737025824829544",
"#comment" : "grok share (#7040)",
"#category": ("", "twitter", "tweet"),
"#class" : twitter.TwitterTweetExtractor,
"#options" : {"cards": True},
"#urls" : "https://pbs.twimg.com/grok-img-share/1866736156786008064.jpg",
},

{
"#url" : "https://twitter.com/playpokemon/status/1263832915173048321/quotes",
"#category": ("", "twitter", "quotes"),
Expand Down

0 comments on commit d2cad59

Please sign in to comment.