From 83d583d2b087449b66e65af89372c165b039bcf3 Mon Sep 17 00:00:00 2001 From: Dave Gallant Date: Wed, 15 Jul 2020 21:48:58 -0400 Subject: [PATCH] Make a display thread function for re-use (#74) --- rfd/VERSION | 2 +- rfd/cli.py | 46 +++++++++++++++++++++++----------------------- rfd/parsing.py | 9 ++++++++- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/rfd/VERSION b/rfd/VERSION index c2c0004..449d7e7 100644 --- a/rfd/VERSION +++ b/rfd/VERSION @@ -1 +1 @@ -0.3.5 +0.3.6 diff --git a/rfd/cli.py b/rfd/cli.py index c114534..0e65b77 100644 --- a/rfd/cli.py +++ b/rfd/cli.py @@ -43,6 +43,27 @@ def print_version(ctx, value): ctx.exit() +def display_thread(click, thread, count): # pylint: disable=redefined-outer-name + dealer = thread.dealer_name + if dealer and dealer is not None: + dealer = "[" + dealer + "] " + else: + dealer = "" + click.echo( + " " + + str(count) + + "." + + get_vote_color(thread.score) + + Fore.RESET + + "%s%s" % (dealer, thread.title) + + Fore.LIGHTYELLOW_EX + + " (%d views)" % thread.total_views + + Fore.RESET + ) + click.echo(Fore.BLUE + " {}".format(thread.url)) + click.echo(Style.RESET_ALL) + + @click.group(invoke_without_command=True) @click.option( "-v", @@ -115,19 +136,7 @@ def threads(limit, forum_id): """ _threads = parse_threads(get_threads(forum_id, limit), limit) for count, thread in enumerate(_threads, 1): - click.echo( - " " - + str(count) - + "." - + get_vote_color(thread.score) - + Fore.RESET - + "[%s] %s" % (thread.dealer_name, thread.title) - + Fore.LIGHTYELLOW_EX - + " (%d views)" % thread.total_views - + Fore.RESET - ) - click.echo(Fore.BLUE + " {}".format(thread.url)) - click.echo(Style.RESET_ALL) + display_thread(click, thread, count) @cli.command(short_help="Search deals based on a regular expression.") @@ -159,13 +168,4 @@ def search(num_pages, forum_id, regex): _threads = parse_threads(get_threads(forum_id, 100, page=page), limit=100) for thread in search_threads(threads=_threads, regex=regex): count += 1 - click.echo( - " " - + str(count) - + "." - + get_vote_color(thread.score) - + Fore.RESET - + "[%s] %s" % (thread.dealer_name, thread.title) - ) - click.echo(Fore.BLUE + " {}".format(thread.url)) - click.echo(Style.RESET_ALL) + display_thread(click, thread, count) diff --git a/rfd/parsing.py b/rfd/parsing.py index d80afa5..c5d85d7 100644 --- a/rfd/parsing.py +++ b/rfd/parsing.py @@ -7,6 +7,13 @@ def build_web_path(slug): return "{}{}".format(API_BASE_URL, slug) +def get_dealer(topic): + dealer = None + if topic.get("offer"): + dealer = topic.get("offer").get("dealer_name") + return dealer + + def parse_threads(threads, limit): """parse topics list api response into digestible list. @@ -26,7 +33,7 @@ def parse_threads(threads, limit): parsed_threads.append( Thread( title=topic.get("title"), - dealer_name=topic["offer"].get("dealer_name"), + dealer_name=get_dealer(topic), score=calculate_score(topic), url=build_web_path(topic.get("web_path")), total_views=topic.get("total_views"),