mirror of
https://github.com/davegallant/rfd.git
synced 2025-08-05 08:23:38 +00:00
Set default of threads command to 1 page and 40 threads (#80)
* Set default of threads command to 1 page and 40 threads * Echo posts in a pager
This commit is contained in:
24
rfd/api.py
24
rfd/api.py
@@ -33,28 +33,30 @@ def create_user_map(users):
|
||||
return m
|
||||
|
||||
|
||||
def get_threads(forum_id, limit, page=1):
|
||||
def get_threads(forum_id, pages):
|
||||
"""Get threads from rfd api
|
||||
|
||||
Arguments:
|
||||
forum_id {int} -- forum id
|
||||
limit {[type]} -- limit number of threads returned
|
||||
pages {int} -- the number of pages of threads to collect
|
||||
|
||||
Returns:
|
||||
dict -- api response
|
||||
"""
|
||||
threads = []
|
||||
try:
|
||||
response = requests.get(
|
||||
"{}/api/topics?forum_id={}&per_page={}&page={}".format(
|
||||
API_BASE_URL, forum_id, get_safe_per_page(limit), page
|
||||
for page in range(1, pages + 1):
|
||||
response = requests.get(
|
||||
"{}/api/topics?forum_id={}&per_page=40&page={}".format(
|
||||
API_BASE_URL, forum_id, page
|
||||
)
|
||||
)
|
||||
)
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
logging.error("Unable to retrieve threads. %s", response.text)
|
||||
if response.status_code != 200:
|
||||
raise Exception("When collecting threads, received a status code: %s" % response.status_code)
|
||||
threads += response.json().get("topics")
|
||||
except JSONDecodeError as err:
|
||||
logging.error("Unable to retrieve threads. %s", err)
|
||||
return None
|
||||
logging.error("Unable to decode threads. %s", err)
|
||||
return threads
|
||||
|
||||
|
||||
def get_posts(post):
|
||||
|
39
rfd/cli.py
39
rfd/cli.py
@@ -2,13 +2,12 @@ from __future__ import unicode_literals
|
||||
|
||||
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import click
|
||||
from colorama import init, Fore, Style
|
||||
from colorama import init
|
||||
from .api import get_threads, get_posts
|
||||
from .threads import parse_threads, search_threads, sort_threads, generate_thread_output
|
||||
from .scores import get_vote_color
|
||||
from .posts import generate_posts_output
|
||||
from .__version__ import version as current_version
|
||||
|
||||
init()
|
||||
@@ -21,12 +20,6 @@ logging.getLogger().addHandler(logging.StreamHandler())
|
||||
def get_version():
|
||||
return "rfd v" + current_version
|
||||
|
||||
|
||||
def get_terminal_width():
|
||||
_, columns = os.popen("stty size", "r").read().split()
|
||||
return int(columns)
|
||||
|
||||
|
||||
def print_version(ctx, value):
|
||||
if not value or ctx.resilient_parsing:
|
||||
return
|
||||
@@ -64,18 +57,7 @@ def posts(post_id):
|
||||
"""
|
||||
|
||||
try:
|
||||
click.echo("-" * get_terminal_width())
|
||||
for post in get_posts(post=post_id):
|
||||
click.echo(
|
||||
" -"
|
||||
+ get_vote_color(post.score)
|
||||
+ Fore.RESET
|
||||
+ post.body
|
||||
+ Fore.YELLOW
|
||||
+ " ({})".format(post.user)
|
||||
)
|
||||
click.echo(Style.RESET_ALL)
|
||||
click.echo("-" * get_terminal_width())
|
||||
click.echo_via_pager(generate_posts_output(get_posts(post=post_id)))
|
||||
except ValueError:
|
||||
click.echo("Invalid post id.")
|
||||
sys.exit(1)
|
||||
@@ -86,9 +68,9 @@ def posts(post_id):
|
||||
|
||||
@cli.command(short_help="Displays threads in the forum. Defaults to hot deals.")
|
||||
@click.option("--forum-id", default=9, help="The forum id number")
|
||||
@click.option("--limit", default=10, help="Number of threads.")
|
||||
@click.option("--pages", default=1, help="Number of pages to show. Defaults to 1.")
|
||||
@click.option("--sort-by", default=None, help="Sort threads by")
|
||||
def threads(limit, forum_id, sort_by):
|
||||
def threads(forum_id, pages, sort_by):
|
||||
"""Display threads in the specified forum id. Defaults to 9 (hot deals).
|
||||
|
||||
Popular forum ids:
|
||||
@@ -105,9 +87,7 @@ def threads(limit, forum_id, sort_by):
|
||||
74 \t shopping discussion
|
||||
88 \t cell phones
|
||||
"""
|
||||
_threads = sort_threads(
|
||||
parse_threads(get_threads(forum_id, limit), limit), sort_by=sort_by
|
||||
)
|
||||
_threads = sort_threads(parse_threads(get_threads(forum_id, pages)), sort_by=sort_by)
|
||||
click.echo_via_pager(generate_thread_output(_threads))
|
||||
|
||||
|
||||
@@ -138,10 +118,9 @@ def search(pages, forum_id, sort_by, regex):
|
||||
|
||||
matched_threads = []
|
||||
|
||||
for page in range(1, pages):
|
||||
_threads = parse_threads(get_threads(forum_id, 100, page=page), limit=100)
|
||||
for thread in search_threads(threads=_threads, regex=regex):
|
||||
matched_threads.append(thread)
|
||||
_threads = parse_threads(get_threads(forum_id, pages=pages))
|
||||
for thread in search_threads(threads=_threads, regex=regex):
|
||||
matched_threads.append(thread)
|
||||
click.echo_via_pager(
|
||||
generate_thread_output(sort_threads(matched_threads, sort_by=sort_by))
|
||||
)
|
||||
|
26
rfd/posts.py
26
rfd/posts.py
@@ -1,8 +1,32 @@
|
||||
# pylint: disable=old-style-class
|
||||
|
||||
import os
|
||||
from colorama import Fore, Style
|
||||
from .scores import get_vote_color
|
||||
|
||||
class Post:
|
||||
def __init__(self, body, score, user):
|
||||
self.body = body
|
||||
self.score = score
|
||||
self.user = user
|
||||
|
||||
def get_terminal_width():
|
||||
_, columns = os.popen("stty size", "r").read().split()
|
||||
return int(columns)
|
||||
|
||||
def generate_posts_output(posts):
|
||||
output = ""
|
||||
output += ("-" * get_terminal_width())
|
||||
for post in posts:
|
||||
output += (
|
||||
" -"
|
||||
+ get_vote_color(post.score)
|
||||
+ Fore.RESET
|
||||
+ post.body
|
||||
+ Fore.YELLOW
|
||||
+ " ({})".format(post.user)
|
||||
)
|
||||
output += (Style.RESET_ALL)
|
||||
output += "\n"
|
||||
output += ("-" * get_terminal_width())
|
||||
output += "\n"
|
||||
return output
|
||||
|
@@ -27,12 +27,11 @@ def get_dealer(topic):
|
||||
return dealer
|
||||
|
||||
|
||||
def parse_threads(threads, limit):
|
||||
def parse_threads(threads):
|
||||
"""Parse topics list api response into digestible list.
|
||||
|
||||
Arguments:
|
||||
threads {dict} -- topics response from rfd api
|
||||
limit {int} -- limit number of threads returned
|
||||
|
||||
Returns:
|
||||
list(dict) -- digestible list of threads
|
||||
@@ -40,9 +39,7 @@ def parse_threads(threads, limit):
|
||||
parsed_threads = []
|
||||
if threads is None:
|
||||
return []
|
||||
for count, topic in enumerate(threads.get("topics"), start=1):
|
||||
if count > limit:
|
||||
break
|
||||
for topic in threads:
|
||||
parsed_threads.append(
|
||||
Thread(
|
||||
title=topic.get("title"),
|
||||
|
@@ -19,11 +19,10 @@ def test_extract_post_id():
|
||||
|
||||
def test_parse_threads(threads_api_response):
|
||||
|
||||
limit = 10
|
||||
threads = parse_threads(threads_api_response, limit)
|
||||
assert len(threads) == limit
|
||||
threads = parse_threads(threads_api_response.get("topics"))
|
||||
assert len(threads) == 10
|
||||
|
||||
|
||||
def test_parse_threads_empty():
|
||||
|
||||
assert parse_threads(None, 10) == []
|
||||
assert parse_threads(None) == []
|
||||
|
Reference in New Issue
Block a user