Skip to content

Commit

Permalink
Better error for non-existing user, closes #37
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Apr 17, 2020
1 parent 9d61313 commit 73947d5
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion twitter_to_sqlite/utils.py
Expand Up @@ -34,6 +34,11 @@
source_re = re.compile('<a href="(?P<url>.*?)".*?>(?P<name>.*?)</a>')


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)
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 73947d5

Please sign in to comment.