Skip to content

Commit

Permalink
[twitter] improve error handling
Browse files Browse the repository at this point in the history
- handle accounts without 'rest_id'
- handle timelines with empty 'instructions'
  • Loading branch information
mikf committed Jan 23, 2022
1 parent 729b07c commit a9f78e6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
6 changes: 3 additions & 3 deletions gallery_dl/exception.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

# Copyright 2015-2021 Mike Fährmann
# Copyright 2015-2022 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
Expand Down Expand Up @@ -33,12 +33,12 @@ class GalleryDLException(Exception):
msgfmt = None
code = 1

def __init__(self, message=None):
def __init__(self, message=None, fmt=True):
if not message:
message = self.default
elif isinstance(message, Exception):
message = "{}: {}".format(message.__class__.__name__, message)
if self.msgfmt:
if self.msgfmt and fmt:
message = self.msgfmt.format(message)
Exception.__init__(self, message)

Expand Down
31 changes: 21 additions & 10 deletions gallery_dl/extractor/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,15 +891,23 @@ def user_by_screen_name(self, screen_name):
"withSafetyModeUserFields": True,
"withSuperFollowsUserFields": True,
})}
try:
return self._call(endpoint, params)["data"]["user"]["result"]
except KeyError:
raise exception.NotFoundError("user")
return self._call(endpoint, params)["data"]["user"]["result"]

def _user_id_by_screen_name(self, screen_name):
if screen_name.startswith("id:"):
return screen_name[3:]
return self.user_by_screen_name(screen_name)["rest_id"]

user = ()
try:
user = self.user_by_screen_name(screen_name)
return user["rest_id"]
except KeyError:
if "unavailable_message" in user:
raise exception.NotFoundError("{} ({})".format(
user["unavailable_message"].get("text"),
user.get("reason")), False)
else:
raise exception.NotFoundError("user")

@cache(maxage=3600)
def _guest_token(self):
Expand Down Expand Up @@ -1053,8 +1061,6 @@ def _pagination_tweets(self, endpoint, variables, path=None):
pinned_tweet = self.extractor.pinned

while True:
tweets = []
cursor = tweet = stop = None
params = {"variables": self._json_dumps(variables)}
data = self._call(endpoint, params)["data"]

Expand All @@ -1066,16 +1072,21 @@ def _pagination_tweets(self, endpoint, variables, path=None):
for key in path:
data = data[key]
instructions = data["instructions"]
except KeyError:

entries = instructions[0]["entries"]
except (KeyError, IndexError):
return

tweets = []
tweet = cursor = None

if pinned_tweet:
pinned_tweet = False
if instructions[-1]["type"] == "TimelinePinEntry":
tweets.append(instructions[-1]["entry"]["content"]
["itemContent"]["tweet_results"]["result"])

for entry in instructions[0]["entries"]:
for entry in entries:
esw = entry["entryId"].startswith

if esw("tweet-"):
Expand Down Expand Up @@ -1130,7 +1141,7 @@ def _pagination_tweets(self, endpoint, variables, path=None):
quoted["legacy"]["quoted_by_id_str"] = tweet["rest_id"]
yield quoted

if stop or not cursor or not tweet:
if not tweet or not cursor:
return
variables["cursor"] = cursor

Expand Down

0 comments on commit a9f78e6

Please sign in to comment.