[twitter] extract 'source_id' and 'source_user' metadata (#7470 #7640)

This commit is contained in:
Mike Fährmann
2025-06-12 18:54:34 +02:00
parent 06e2f2cd91
commit e2d104a110
2 changed files with 42 additions and 1 deletions

View File

@@ -123,6 +123,11 @@ class TwitterExtractor(Extractor):
tdata.update(metadata)
tdata["count"] = len(files)
yield Message.Directory, tdata
del tdata["source_id"]
if "source_user" in tdata:
del tdata["source_user"]
for tdata["num"], file in enumerate(files, 1):
file.update(tdata)
url = file.pop("url")
@@ -212,8 +217,22 @@ class TwitterExtractor(Extractor):
file["width"] = media["original_info"].get("width", 0)
file["height"] = media["original_info"].get("height", 0)
file["description"] = media.get("ext_alt_text")
self._extract_media_source(file, media)
files.append(file)
def _extract_media_source(self, dest, media):
dest["source_id"] = 0
if "source_status_id_str" in media:
try:
dest["source_id"] = text.parse_int(
media["source_status_id_str"])
dest["source_user"] = self._transform_user(
media["additional_media_info"]["source_user"]
["user_results"]["result"])
except Exception:
pass
def _image_fallback(self, base):
for fmt in self._size_fallback:
yield base + fmt
@@ -338,6 +357,7 @@ class TwitterExtractor(Extractor):
tget("in_reply_to_status_id_str")),
"conversation_id": text.parse_int(
tget("conversation_id_str")),
"source_id" : 0,
"date" : date,
"author" : author,
"user" : self._user or author,
@@ -401,6 +421,9 @@ class TwitterExtractor(Extractor):
tdata["reply_to"] = legacy["in_reply_to_screen_name"]
if "quoted_by" in legacy:
tdata["quote_by"] = legacy["quoted_by"]
if "extended_entities" in legacy:
self._extract_media_source(
tdata, legacy["extended_entities"]["media"][0])
if tdata["retweet_id"]:
tdata["content"] = "RT @{}: {}".format(
author["name"], tdata["content"])

View File

@@ -334,7 +334,9 @@ __tests__ = (
"#class" : twitter.TwitterTweetExtractor,
"#sha1_url": "3a2a43dc5fb79dd5432c701d8e55e87c4e551f47",
"type": "photo",
"type" : "photo",
"source_id" : 0,
"!source_user": dict,
},
{
@@ -657,6 +659,22 @@ The Washington Post writes, "Three weeks after the toxic train derailment in Ohi
"#urls" : "https://pbs.twimg.com/grok-img-share/1866736156786008064.jpg",
},
{
"#url" : "https://x.com/gdldev/status/1932109706354733077",
"#comment" : "'source_id' and 'source_user' metadata (#7470, #7640)",
"#category": ("", "twitter", "tweet"),
"#class" : twitter.TwitterTweetExtractor,
"#urls" : (
"https://video.twimg.com/amplify_video/1932079443264376832/vid/avc1/640x336/7xo7NCPkMLRWb8NZ.mp4?tag=14",
"https://video.twimg.com/ext_tw_video/1930425322333229056/pu/vid/avc1/1024x576/6f_cdEPY3a5CcbZP.mp4?tag=12",
),
"source_id" : {1932079546590982508, 1930425346404274416},
"source_user": {
"name": {"Satorin69", "Derlan144p_"},
},
},
{
"#url" : "https://twitter.com/playpokemon/status/1263832915173048321/quotes",
"#category": ("", "twitter", "quotes"),