diff --git a/twitter_to_sqlite/utils.py b/twitter_to_sqlite/utils.py index 6c5bbc4..bbebea9 100644 --- a/twitter_to_sqlite/utils.py +++ b/twitter_to_sqlite/utils.py @@ -34,6 +34,11 @@ source_re = re.compile('(?P.*?)') +class UserDoesNotExist(click.ClickException): + def __init__(self, identifier): + super().__init__("User '{}' does not exist".format(identifier)) + + def open_database(db_path): db = sqlite_utils.Database(db_path) # Only run migrations if this is an existing DB (has tables) @@ -120,7 +125,10 @@ def get_profile(db, session, user_id=None, screen_name=None): url = "https://api.twitter.com/1.1/users/show.json" if args: url += "?" + urllib.parse.urlencode(args) - profile = session.get(url).json() + response = session.get(url) + if response.status_code == 404: + raise UserDoesNotExist(screen_name or user_id) + profile = response.json() save_users(db, [profile]) return profile