Skip to content

Commit

Permalink
[twitter] handle missing 'expanded_url' fields (#5463, #5490)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed Apr 19, 2024
1 parent c9d3b5e commit 347af7f
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions gallery_dl/extractor/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ def _extract_twitpic(self, tweet, files):

# collect URLs from entities
for url in tweet["entities"].get("urls") or ():
url = url["expanded_url"]
if "//twitpic.com/" not in url or "/photos/" in url:
url = url.get("expanded_url") or url.get("url") or ""
if not url or "//twitpic.com/" not in url or "/photos/" in url:
continue
if url.startswith("http:"):
url = "https" + url[4:]
Expand Down Expand Up @@ -336,7 +336,10 @@ def _transform_tweet(self, tweet):
urls = entities.get("urls")
if urls:
for url in urls:
content = content.replace(url["url"], url["expanded_url"])
try:
content = content.replace(url["url"], url["expanded_url"])
except KeyError:
pass
txt, _, tco = content.rpartition(" ")
tdata["content"] = txt if tco.startswith("https://t.co/") else content

Expand Down Expand Up @@ -403,7 +406,10 @@ def _transform_user(self, user):
urls = entities["description"].get("urls")
if urls:
for url in urls:
descr = descr.replace(url["url"], url["expanded_url"])
try:
descr = descr.replace(url["url"], url["expanded_url"])
except KeyError:
pass
udata["description"] = descr

if "url" in entities:
Expand Down

0 comments on commit 347af7f

Please sign in to comment.