diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b8033f..81b1cfe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +### Release +### 0.3.9.0 + +#### Fixed +- Remove duplicate code (thanks to Jesse Weinstein) +- Upgrade to Mastodon.py version to Mastodon.py 1.8.0 ### Release ### [0.3.8.1] - 2020-01-22 diff --git a/requirements.txt b/requirements.txt index 004fd7f..95f56d8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ click>=6.7 -Mastodon.py>=1.5.1 +Mastodon.py>=1.0.0 colored>=1.3.93 humanize>=0.5.1 emoji>=0.4.5 diff --git a/setup.py b/setup.py index fd94fb6..16621af 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from setuptools import setup, find_packages setup( name="tootstream", - version="0.3.8.2", + version="0.3.9.0", python_requires=">=3", install_requires=[line.strip() for line in open('requirements.txt')], diff --git a/src/tootstream/toot.py b/src/tootstream/toot.py index 258092a..7f6a006 100644 --- a/src/tootstream/toot.py +++ b/src/tootstream/toot.py @@ -67,6 +67,11 @@ RESERVED = ("theme", "global") +class AlreadyPrintedException(Exception): + """An exception that has already been shown to the user, so doesn't need to be printed again.""" + pass + + class IdDict: """Represents a mapping of local (tootstream) ID's to global (mastodon) IDs.""" @@ -189,10 +194,22 @@ def get_userid(mastodon, rest): return users[0]['id'] +def get_userid2(mastodon, rest): + userid = get_userid(mastodon, rest) + if isinstance(userid, list): + cprint(" multiple matches found:", fg('red')) + printUsersShort(userid) + raise AlreadyPrintedException() + elif userid == -1: + raise Exception(" username '{}' not found".format(rest)) + + return userid + + def get_list_id(mastodon, rest): """Get the ID for a list""" - if not rest: - return -1 + if not rest or not rest.strip(): + raise Exception("List argument missing.") # maybe it's already an int try: @@ -200,13 +217,14 @@ def get_list_id(mastodon, rest): except ValueError: pass - rest = rest.strip() - lists = mastodon.lists() + desired_title = rest.strip().lower() for item in lists: - if item['title'].lower() == rest.lower(): + if item['title'].lower() == desired_title: return item['id'] + raise Exception("List '{}' is not found.".format(rest)) + def flaghandler_note(mastodon, rest): """Parse input for flagsr. """ @@ -1406,10 +1424,6 @@ def say_error(*args, **kwargs): print("list stream must have a list ID.") return item = get_list_id(mastodon, items[-1]) - if not item or item == -1: - cprint("List {} is not found".format(items[-1]), fg('red')) - return - handle = mastodon.stream_list(item, toot_listener, run_async=True, reconnect_async=True) elif rest.startswith('#'): @@ -1601,19 +1615,10 @@ def block(mastodon, rest): ex: block 23 block @user block @user@instance.example.com""" - userid = get_userid(mastodon, rest) - if isinstance(userid, list): - cprint(" multiple matches found:", fg('red')) - printUsersShort(userid) - elif userid == -1: - cprint(" username not found", fg('red')) - else: - try: - relations = mastodon.account_block(userid) - if relations['blocking']: - cprint(" user " + str(userid) + " is now blocked", fg('blue')) - except: - cprint(" Error, unable to block.", fg('red')) + userid = get_userid2(mastodon, rest) + relations = mastodon.account_block(userid) + if relations['blocking']: + cprint(" user " + str(userid) + " is now blocked", fg('blue')) block.__argstr__ = '' @@ -1627,20 +1632,11 @@ def unblock(mastodon, rest): ex: unblock 23 unblock @user unblock @user@instance.example.com""" - userid = get_userid(mastodon, rest) - if isinstance(userid, list): - cprint(" multiple matches found:", fg('red')) - printUsersShort(userid) - elif userid == -1: - cprint(" username not found", fg('red')) - else: - try: - relations = mastodon.account_unblock(userid) - if not relations['blocking']: - cprint(" user " + str(userid) + - " is now unblocked", fg('blue')) - except: - cprint(" Error, unable to unblock.", fg('red')) + userid = get_userid2(mastodon, rest) + relations = mastodon.account_unblock(userid) + if not relations['blocking']: + cprint(" user " + str(userid) + + " is now unblocked", fg('blue')) unblock.__argstr__ = '' @@ -1654,23 +1650,14 @@ def follow(mastodon, rest): ex: follow 23 follow @user follow @user@instance.example.com""" - userid = get_userid(mastodon, rest) - if isinstance(userid, list): - cprint(" multiple matches found:", fg('red')) - printUsersShort(userid) - elif userid == -1: - cprint(" username not found", fg('red')) - else: - try: - relations = mastodon.account_follow(userid) - if relations['following']: - cprint(" user " + str(userid) + - " is now followed", fg('blue')) - username = '@' + mastodon.account(userid)['acct'] - if username not in completion_list: - bisect.insort(completion_list, username) - except: - cprint(" Error, unable to follow.", fg('red')) + userid = get_userid2(mastodon, rest) + relations = mastodon.account_follow(userid) + if relations['following']: + cprint(" user " + str(userid) + + " is now followed", fg('blue')) + username = '@' + mastodon.account(userid)['acct'] + if username not in completion_list: + bisect.insort(completion_list, username) follow.__argstr__ = '' @@ -1684,23 +1671,14 @@ def unfollow(mastodon, rest): ex: unfollow 23 unfollow @user unfollow @user@instance.example.com""" - userid = get_userid(mastodon, rest) - if isinstance(userid, list): - cprint(" multiple matches found:", fg('red')) - printUsersShort(userid) - elif userid == -1: - cprint(" username not found", fg('red')) - else: - try: - relations = mastodon.account_unfollow(userid) - if not relations['following']: - cprint(" user " + str(userid) + - " is now unfollowed", fg('blue')) - username = '@' + mastodon.account(userid)['acct'] - if username in completion_list: - completion_list.remove(username) - except: - cprint(" Error, unable to unfollow.", fg('red')) + userid = get_userid2(mastodon, rest) + relations = mastodon.account_unfollow(userid) + if not relations['following']: + cprint(" user " + str(userid) + + " is now unfollowed", fg('blue')) + username = '@' + mastodon.account(userid)['acct'] + if username in completion_list: + completion_list.remove(username) unfollow.__argstr__ = '' @@ -1714,19 +1692,10 @@ def mute(mastodon, rest): ex: mute 23 mute @user mute @user@instance.example.com""" - userid = get_userid(mastodon, rest) - if isinstance(userid, list): - cprint(" multiple matches found:", fg('red')) - printUsersShort(userid) - elif userid == -1: - cprint(" username not found", fg('red')) - else: - try: - relations = mastodon.account_mute(userid) - if relations['muting']: - cprint(" user " + str(userid) + " is now muted", fg('blue')) - except: - cprint(" Error, unable to mute.", fg('red')) + userid = get_userid2(mastodon, rest) + relations = mastodon.account_mute(userid) + if relations['muting']: + cprint(" user " + str(userid) + " is now muted", fg('blue')) mute.__argstr__ = '' @@ -1740,19 +1709,10 @@ def unmute(mastodon, rest): ex: unmute 23 unmute @user unmute @user@instance.example.com""" - userid = get_userid(mastodon, rest) - if isinstance(userid, list): - cprint(" multiple matches found:", fg('red')) - printUsersShort(userid) - elif userid == -1: - cprint(" username not found", fg('red')) - else: - try: - relations = mastodon.account_unmute(userid) - if not relations['muting']: - cprint(" user " + str(userid) + " is now unmuted", fg('blue')) - except: - cprint(" Error, unable to unmute.", fg('red')) + userid = get_userid2(mastodon, rest) + relations = mastodon.account_unmute(userid) + if not relations['muting']: + cprint(" user " + str(userid) + " is now unmuted", fg('blue')) unmute.__argstr__ = '' @@ -1823,17 +1783,9 @@ def view(mastodon, rest): return # validate userid argument - userid = get_userid(mastodon, userid) - if isinstance(userid, list): - cprint(" multiple matches found:", fg('red')) - printUsersShort(userid) - elif userid == -1: - cprint(" username not found", fg('red')) - else: - - print_toots(mastodon, mastodon.account_statuses(userid, limit=count), - ctx_name="user timeline", add_completion=False) - return + userid = get_userid2(mastodon, userid) + print_toots(mastodon, mastodon.account_statuses(userid, limit=count), + ctx_name="user timeline", add_completion=False) view.__argstr__ = ' []' @@ -1947,22 +1899,9 @@ def accept(mastodon, rest): ex: accept 23 accept @user accept @user@instance.example.com""" - userid = get_userid(mastodon, rest) - if isinstance(userid, list): - cprint(" multiple matches found:", fg('red')) - printUsersShort(userid) - elif userid == -1: - cprint(" username not found", fg('red')) - else: - try: - mastodon.follow_request_authorize(userid) - except: - cprint(" Error, unable to accept request.", fg('red')) - return - - # assume it worked if no exception - cprint(" user {}'s request is accepted".format(userid), fg('blue')) - return + userid = get_userid2(mastodon, rest) + mastodon.follow_request_authorize(userid) + cprint(" user {}'s request is accepted".format(userid), fg('blue')) accept.__argstr__ = '' @@ -1976,22 +1915,9 @@ def reject(mastodon, rest): ex: reject 23 reject @user reject @user@instance.example.com""" - userid = get_userid(mastodon, rest) - if isinstance(userid, list): - cprint(" multiple matches found:", fg('red')) - printUsersShort(userid) - elif userid == -1: - cprint(" username not found", fg('red')) - else: - try: - mastodon.follow_request_reject(userid) - except: - cprint(" Error, unable to reject request.", fg('red')) - return - - # assume it worked if no exception - cprint(" user {}'s request is rejected".format(userid), fg('blue')) - return + userid = get_userid2(mastodon, rest) + mastodon.follow_request_reject(userid) + cprint(" user {}'s request is rejected".format(userid), fg('blue')) reject.__argstr__ = '' @@ -2068,13 +1994,8 @@ def listcreate(mastodon, rest): """Creates a list.""" if not(list_support(mastodon)): return - try: - mastodon.list_create(rest) - cprint("List {} created.".format(rest), fg('green')) - except Exception as e: - cprint("error while creating list: {}".format( - type(e).__name__), fg('red')) - return + mastodon.list_create(rest) + cprint("List {} created.".format(rest), fg('green')) listcreate.__argstr__ = '' @@ -2099,16 +2020,8 @@ def listrename(mastodon, rest): list_id = get_list_id(mastodon, items[0]) updated_name = items[1] - if not list_id: - cprint("List {} is not found".format(items[0]), fg('red')) - return - - try: - mastodon.list_update(list_id, updated_name) - cprint("Renamed {} to {}.".format(items[1], items[0]), fg('green')) - except Exception as e: - cprint("error while updating list: {}".format( - type(e).__name__), fg('red')) + mastodon.list_update(list_id, updated_name) + cprint("Renamed {} to {}.".format(items[1], items[0]), fg('green')) listrename.__argstr__ = ' ' @@ -2123,17 +2036,10 @@ def listdestroy(mastodon, rest): if not(list_support(mastodon)): return item = get_list_id(mastodon, rest) - if not item or item == -1: - cprint("List {} is not found".format(rest), fg('red')) - return - try: - mastodon.list_delete(item) - cprint("List {} deleted.".format(rest), fg('green')) - except Exception as e: - cprint("error while creating list: {}".format( - type(e).__name__), fg('red')) - return + mastodon.list_delete(item) + cprint("List {} deleted.".format(rest), fg('green')) + listdestroy.__argstr__ = '' @@ -2152,17 +2058,10 @@ def listhome(mastodon, rest): return stepper, rest = step_flag(rest) - try: - item = get_list_id(mastodon, rest) - if not item or item == -1: - cprint("List {} is not found".format(rest), fg('red')) - return - list_toots = mastodon.timeline_list(item) - print_toots(mastodon, list_toots, stepper, ctx_name='list') + item = get_list_id(mastodon, rest) + list_toots = mastodon.timeline_list(item) + print_toots(mastodon, list_toots, stepper, ctx_name='list') - except Exception as e: - cprint("error while displaying list: {}".format( - type(e).__name__), fg('red')) listhome.__argstr__ = '' @@ -2177,9 +2076,6 @@ def listaccounts(mastodon, rest): if not(list_support(mastodon)): return item = get_list_id(mastodon, rest) - if not item: - cprint("List {} is not found".format(rest), fg('red')) - return list_accounts = mastodon.list_accounts(item) cprint("List: %s" % rest, fg('green')) @@ -2207,22 +2103,9 @@ def listadd(mastodon, rest): return list_id = get_list_id(mastodon, items[0]) - account_id = get_userid(mastodon, items[1]) - - if not list_id: - cprint("List {} is not found".format(items[0]), fg('red')) - return - - if not account_id: - cprint("Account {} is not found".format(items[1]), fg('red')) - return - - try: - mastodon.list_accounts_add(list_id, account_id) - cprint("Added {} to list {}.".format(items[1], items[0]), fg('green')) - except Exception as e: - cprint("error while adding to list: {}".format( - type(e).__name__), fg('red')) + account_id = get_userid2(mastodon, items[1]) + mastodon.list_accounts_add(list_id, account_id) + cprint("Added {} to list {}.".format(items[1], items[0]), fg('green')) listadd.__argstr__ = ' ' @@ -2246,23 +2129,10 @@ def listremove(mastodon, rest): return list_id = get_list_id(mastodon, items[0]) - account_id = get_userid(mastodon, items[1]) - - if not list_id: - cprint("List {} is not found".format(items[0]), fg('red')) - return - - if not account_id: - cprint("Account {} is not found".format(items[1]), fg('red')) - return - - try: - mastodon.list_accounts_delete(list_id, account_id) - cprint("Removed {} from list {}.".format( - items[1], items[0]), fg('green')) - except Exception as e: - cprint("error while deleting from list: {}".format( - type(e).__name__), fg('red')) + account_id = get_userid2(mastodon, items[1]) + mastodon.list_accounts_delete(list_id, account_id) + cprint("Removed {} from list {}.".format( + items[1], items[0]), fg('green')) listremove.__argstr__ = ' ' @@ -2366,6 +2236,8 @@ def say_error(a, b): return cprint("Invalid command. Use 'help' for a list of co command = command[0] cmd_func = commands.get(command, say_error) cmd_func(mastodon, rest) + except AlreadyPrintedException: + pass except Exception as e: cprint(e, fg('red'))