mirror of
https://github.com/davegallant/rfd.git
synced 2025-08-07 09:02:32 +00:00
Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
258ca59bdf | ||
|
e433a954cd | ||
|
b30a3c5b66 | ||
|
cfd7e01e43 | ||
|
f664cbd9c6 | ||
|
ee6939aafe | ||
|
ad4a072325 |
9
Makefile
9
Makefile
@@ -32,15 +32,10 @@ lint:
|
|||||||
.PHONY: lint
|
.PHONY: lint
|
||||||
|
|
||||||
## test: Run all unit tests
|
## test: Run all unit tests
|
||||||
test: tmp/.tests-passed.sentinel
|
test:
|
||||||
|
> pytest -vvv tests
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
|
|
||||||
# Tests - re-ran if any file under src has been changed since tmp/.tests-passed.sentinel was last touched
|
|
||||||
tmp/.tests-passed.sentinel: $(shell find ${SRC} -type f)
|
|
||||||
> mkdir -p $(@D)
|
|
||||||
> pytest -v
|
|
||||||
> touch $@
|
|
||||||
|
|
||||||
## pr: Run pre-commit, lint and test
|
## pr: Run pre-commit, lint and test
|
||||||
pr: precommit lint test
|
pr: precommit lint test
|
||||||
.PHONY: pr
|
.PHONY: pr
|
||||||
|
24
README.md
24
README.md
@@ -5,12 +5,10 @@ Hot deals on the command line.
|
|||||||
[](https://travis-ci.org/davegallant/rfd)
|
[](https://travis-ci.org/davegallant/rfd)
|
||||||
[](https://badge.fury.io/py/rfd)
|
[](https://badge.fury.io/py/rfd)
|
||||||
[](https://dependabot.com/)
|
[](https://dependabot.com/)
|
||||||
[](https://lgtm.com/projects/g/davegallant/rfd/context:python)
|
[](https://pepy.tech/project/rfd)
|
||||||
|
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -36,7 +34,14 @@ Commands:
|
|||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
|
All commands open up in a pager.
|
||||||
|
|
||||||
|
Search can be done using `/`.
|
||||||
|
|
||||||
|
Close pager with `q`.
|
||||||
|
|
||||||
### View Hot Deals
|
### View Hot Deals
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ rfd threads
|
$ rfd threads
|
||||||
```
|
```
|
||||||
@@ -48,20 +53,27 @@ $ rfd threads --sort-by score
|
|||||||
```
|
```
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ rfd threads --sort-by total_views --limit 40
|
$ rfd threads --sort-by views --pages 10
|
||||||
```
|
```
|
||||||
|
|
||||||
### Simple Search
|
### Simple Search
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ rfd search 'pizza'
|
$ rfd search 'pizza'
|
||||||
```
|
```
|
||||||
|
|
||||||
### RegEx Search
|
### Advanced Search
|
||||||
|
|
||||||
Regular expressions can be used for search.
|
Regular expressions can be used for search.
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ rfd search '(coffee|starbucks)' --num-pages 100
|
$ rfd search '(coffee|starbucks)' --pages 10 --sort-by views
|
||||||
|
```
|
||||||
|
|
||||||
|
### View Posts
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ rfd posts https://forums.redflagdeals.com/kobo-vs-kindle-2396227/
|
||||||
```
|
```
|
||||||
|
|
||||||
## Shell Completion
|
## Shell Completion
|
||||||
|
@@ -1 +1 @@
|
|||||||
0.4.0
|
0.6.0
|
||||||
|
24
rfd/api.py
24
rfd/api.py
@@ -33,28 +33,30 @@ def create_user_map(users):
|
|||||||
return m
|
return m
|
||||||
|
|
||||||
|
|
||||||
def get_threads(forum_id, limit, page=1):
|
def get_threads(forum_id, pages):
|
||||||
"""Get threads from rfd api
|
"""Get threads from rfd api
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
forum_id {int} -- forum id
|
forum_id {int} -- forum id
|
||||||
limit {[type]} -- limit number of threads returned
|
pages {int} -- the number of pages of threads to collect
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
dict -- api response
|
dict -- api response
|
||||||
"""
|
"""
|
||||||
|
threads = []
|
||||||
try:
|
try:
|
||||||
response = requests.get(
|
for page in range(1, pages + 1):
|
||||||
"{}/api/topics?forum_id={}&per_page={}&page={}".format(
|
response = requests.get(
|
||||||
API_BASE_URL, forum_id, get_safe_per_page(limit), page
|
"{}/api/topics?forum_id={}&per_page=40&page={}".format(
|
||||||
|
API_BASE_URL, forum_id, page
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
if response.status_code != 200:
|
||||||
if response.status_code == 200:
|
raise Exception("When collecting threads, received a status code: %s" % response.status_code)
|
||||||
return response.json()
|
threads += response.json().get("topics")
|
||||||
logging.error("Unable to retrieve threads. %s", response.text)
|
|
||||||
except JSONDecodeError as err:
|
except JSONDecodeError as err:
|
||||||
logging.error("Unable to retrieve threads. %s", err)
|
logging.error("Unable to decode threads. %s", err)
|
||||||
return None
|
return threads
|
||||||
|
|
||||||
|
|
||||||
def get_posts(post):
|
def get_posts(post):
|
||||||
|
85
rfd/cli.py
85
rfd/cli.py
@@ -2,16 +2,15 @@ from __future__ import unicode_literals
|
|||||||
|
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
|
||||||
import sys
|
import sys
|
||||||
import click
|
import click
|
||||||
from colorama import init, Fore, Style
|
from colorama import init
|
||||||
from .api import get_threads, get_posts
|
from .api import get_threads, get_posts
|
||||||
from .threads import parse_threads, search_threads, sort_threads
|
from .threads import parse_threads, search_threads, sort_threads, generate_thread_output
|
||||||
|
from .posts import generate_posts_output
|
||||||
from .__version__ import version as current_version
|
from .__version__ import version as current_version
|
||||||
|
|
||||||
init()
|
init()
|
||||||
print()
|
|
||||||
|
|
||||||
logging.getLogger()
|
logging.getLogger()
|
||||||
logging.getLogger().setLevel(logging.INFO)
|
logging.getLogger().setLevel(logging.INFO)
|
||||||
@@ -21,20 +20,6 @@ logging.getLogger().addHandler(logging.StreamHandler())
|
|||||||
def get_version():
|
def get_version():
|
||||||
return "rfd v" + current_version
|
return "rfd v" + current_version
|
||||||
|
|
||||||
|
|
||||||
def get_terminal_width():
|
|
||||||
_, columns = os.popen("stty size", "r").read().split()
|
|
||||||
return int(columns)
|
|
||||||
|
|
||||||
|
|
||||||
def get_vote_color(score):
|
|
||||||
if score > 0:
|
|
||||||
return Fore.GREEN + " [+" + str(score) + "] "
|
|
||||||
if score < 0:
|
|
||||||
return Fore.RED + " [" + str(score) + "] "
|
|
||||||
return Fore.BLUE + " [" + str(score) + "] "
|
|
||||||
|
|
||||||
|
|
||||||
def print_version(ctx, value):
|
def print_version(ctx, value):
|
||||||
if not value or ctx.resilient_parsing:
|
if not value or ctx.resilient_parsing:
|
||||||
return
|
return
|
||||||
@@ -42,27 +27,6 @@ def print_version(ctx, value):
|
|||||||
ctx.exit()
|
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.group(invoke_without_command=True)
|
||||||
@click.option(
|
@click.option(
|
||||||
"-v",
|
"-v",
|
||||||
@@ -93,18 +57,7 @@ def posts(post_id):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
click.echo("-" * get_terminal_width())
|
click.echo_via_pager(generate_posts_output(get_posts(post=post_id)))
|
||||||
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())
|
|
||||||
except ValueError:
|
except ValueError:
|
||||||
click.echo("Invalid post id.")
|
click.echo("Invalid post id.")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
@@ -115,9 +68,9 @@ def posts(post_id):
|
|||||||
|
|
||||||
@cli.command(short_help="Displays threads in the forum. Defaults to hot deals.")
|
@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("--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")
|
@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).
|
"""Display threads in the specified forum id. Defaults to 9 (hot deals).
|
||||||
|
|
||||||
Popular forum ids:
|
Popular forum ids:
|
||||||
@@ -134,20 +87,18 @@ def threads(limit, forum_id, sort_by):
|
|||||||
74 \t shopping discussion
|
74 \t shopping discussion
|
||||||
88 \t cell phones
|
88 \t cell phones
|
||||||
"""
|
"""
|
||||||
_threads = sort_threads(
|
_threads = sort_threads(parse_threads(get_threads(forum_id, pages)), sort_by=sort_by)
|
||||||
parse_threads(get_threads(forum_id, limit), limit), sort_by=sort_by
|
click.echo_via_pager(generate_thread_output(_threads))
|
||||||
)
|
|
||||||
for count, thread in enumerate(_threads, 1):
|
|
||||||
display_thread(click, thread, count)
|
|
||||||
|
|
||||||
|
|
||||||
@cli.command(short_help="Search deals based on a regular expression.")
|
@cli.command(short_help="Search deals based on a regular expression.")
|
||||||
@click.option("--num-pages", default=5, help="Number of pages to search.")
|
@click.option("--pages", default=5, help="Number of pages to search.")
|
||||||
@click.option(
|
@click.option(
|
||||||
"--forum-id", default=9, help="The forum id number. Defaults to 9 (hot deals)."
|
"--forum-id", default=9, help="The forum id number. Defaults to 9 (hot deals)."
|
||||||
)
|
)
|
||||||
|
@click.option("--sort-by", default=None, help="Sort threads by")
|
||||||
@click.argument("regex")
|
@click.argument("regex")
|
||||||
def search(num_pages, forum_id, regex):
|
def search(pages, forum_id, sort_by, regex):
|
||||||
"""Search deals based on regex.
|
"""Search deals based on regex.
|
||||||
|
|
||||||
Popular forum ids:
|
Popular forum ids:
|
||||||
@@ -165,9 +116,11 @@ def search(num_pages, forum_id, regex):
|
|||||||
88 \t cell phones
|
88 \t cell phones
|
||||||
"""
|
"""
|
||||||
|
|
||||||
count = 0
|
matched_threads = []
|
||||||
for page in range(1, num_pages):
|
|
||||||
_threads = parse_threads(get_threads(forum_id, 100, page=page), limit=100)
|
_threads = parse_threads(get_threads(forum_id, pages=pages))
|
||||||
for thread in search_threads(threads=_threads, regex=regex):
|
for thread in search_threads(threads=_threads, regex=regex):
|
||||||
count += 1
|
matched_threads.append(thread)
|
||||||
display_thread(click, thread, count)
|
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
|
# pylint: disable=old-style-class
|
||||||
|
import os
|
||||||
|
from colorama import Fore, Style
|
||||||
|
from .scores import get_vote_color
|
||||||
|
|
||||||
class Post:
|
class Post:
|
||||||
def __init__(self, body, score, user):
|
def __init__(self, body, score, user):
|
||||||
self.body = body
|
self.body = body
|
||||||
self.score = score
|
self.score = score
|
||||||
self.user = user
|
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
|
||||||
|
@@ -1,3 +1,6 @@
|
|||||||
|
from colorama import Fore
|
||||||
|
|
||||||
|
|
||||||
def calculate_score(post):
|
def calculate_score(post):
|
||||||
"""Calculate either topic or post score. If votes cannot be retrieved, the score is 0.
|
"""Calculate either topic or post score. If votes cannot be retrieved, the score is 0.
|
||||||
|
|
||||||
@@ -16,3 +19,11 @@ def calculate_score(post):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
return score
|
return score
|
||||||
|
|
||||||
|
|
||||||
|
def get_vote_color(score):
|
||||||
|
if score > 0:
|
||||||
|
return Fore.GREEN + " [+" + str(score) + "] "
|
||||||
|
if score < 0:
|
||||||
|
return Fore.RED + " [" + str(score) + "] "
|
||||||
|
return Fore.BLUE + " [" + str(score) + "] "
|
||||||
|
@@ -1,15 +1,16 @@
|
|||||||
import re
|
import re
|
||||||
|
from colorama import Fore, Style
|
||||||
from . import API_BASE_URL
|
from . import API_BASE_URL
|
||||||
from .scores import calculate_score
|
from .scores import calculate_score, get_vote_color
|
||||||
|
|
||||||
# pylint: disable=old-style-class
|
# pylint: disable=old-style-class
|
||||||
class Thread:
|
class Thread:
|
||||||
def __init__(self, title, dealer_name, score, url, total_views):
|
def __init__(self, title, dealer_name, score, url, views):
|
||||||
self.dealer_name = dealer_name
|
self.dealer_name = dealer_name
|
||||||
self.score = score
|
self.score = score
|
||||||
self.title = title
|
self.title = title
|
||||||
self.url = url
|
self.url = url
|
||||||
self.total_views = total_views
|
self.views = views
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "Thread(%s)" % self.title
|
return "Thread(%s)" % self.title
|
||||||
@@ -26,12 +27,11 @@ def get_dealer(topic):
|
|||||||
return dealer
|
return dealer
|
||||||
|
|
||||||
|
|
||||||
def parse_threads(threads, limit):
|
def parse_threads(threads):
|
||||||
"""Parse topics list api response into digestible list.
|
"""Parse topics list api response into digestible list.
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
threads {dict} -- topics response from rfd api
|
threads {dict} -- topics response from rfd api
|
||||||
limit {int} -- limit number of threads returned
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
list(dict) -- digestible list of threads
|
list(dict) -- digestible list of threads
|
||||||
@@ -39,16 +39,14 @@ def parse_threads(threads, limit):
|
|||||||
parsed_threads = []
|
parsed_threads = []
|
||||||
if threads is None:
|
if threads is None:
|
||||||
return []
|
return []
|
||||||
for count, topic in enumerate(threads.get("topics"), start=1):
|
for topic in threads:
|
||||||
if count > limit:
|
|
||||||
break
|
|
||||||
parsed_threads.append(
|
parsed_threads.append(
|
||||||
Thread(
|
Thread(
|
||||||
title=topic.get("title"),
|
title=topic.get("title"),
|
||||||
dealer_name=get_dealer(topic),
|
dealer_name=get_dealer(topic),
|
||||||
score=calculate_score(topic),
|
score=calculate_score(topic),
|
||||||
url=build_web_path(topic.get("web_path")),
|
url=build_web_path(topic.get("web_path")),
|
||||||
total_views=topic.get("total_views"),
|
views=topic.get("total_views"),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return parsed_threads
|
return parsed_threads
|
||||||
@@ -58,7 +56,7 @@ def sort_threads(threads, sort_by):
|
|||||||
"""Sort threads by an attribute"""
|
"""Sort threads by an attribute"""
|
||||||
if sort_by is None:
|
if sort_by is None:
|
||||||
return threads
|
return threads
|
||||||
assert sort_by in ["total_views", "score", "title"]
|
assert sort_by in ["views", "score", "title"]
|
||||||
threads = sorted(threads, key=lambda x: getattr(x, sort_by), reverse=True)
|
threads = sorted(threads, key=lambda x: getattr(x, sort_by), reverse=True)
|
||||||
return threads
|
return threads
|
||||||
|
|
||||||
@@ -74,3 +72,28 @@ def search_threads(threads, regex):
|
|||||||
deal.dealer_name and regexp.search(deal.dealer_name.lower())
|
deal.dealer_name and regexp.search(deal.dealer_name.lower())
|
||||||
):
|
):
|
||||||
yield deal
|
yield deal
|
||||||
|
|
||||||
|
|
||||||
|
def generate_thread_output(threads):
|
||||||
|
for count, thread in enumerate(threads, 1):
|
||||||
|
output = ""
|
||||||
|
dealer = thread.dealer_name
|
||||||
|
if dealer and dealer is not None:
|
||||||
|
dealer = "[" + dealer + "] "
|
||||||
|
else:
|
||||||
|
dealer = ""
|
||||||
|
output += (
|
||||||
|
" "
|
||||||
|
+ str(count)
|
||||||
|
+ "."
|
||||||
|
+ get_vote_color(thread.score)
|
||||||
|
+ Fore.RESET
|
||||||
|
+ "%s%s" % (dealer, thread.title)
|
||||||
|
+ Fore.LIGHTYELLOW_EX
|
||||||
|
+ " (%d views)" % thread.views
|
||||||
|
+ Fore.RESET
|
||||||
|
)
|
||||||
|
output += Fore.BLUE + " {}".format(thread.url)
|
||||||
|
output += Style.RESET_ALL
|
||||||
|
output += "\n\n"
|
||||||
|
yield output
|
||||||
|
25
tests/integration/test_cli.py
Normal file
25
tests/integration/test_cli.py
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
from subprocess import Popen, PIPE
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
def run_cli(args):
|
||||||
|
cmd = ["python", "-m", "rfd"] + args.split()
|
||||||
|
p = Popen(cmd, stdout=PIPE)
|
||||||
|
stdout, _ = p.communicate()
|
||||||
|
assert p.returncode == 0
|
||||||
|
return stdout
|
||||||
|
|
||||||
|
|
||||||
|
def test_version():
|
||||||
|
stdout = run_cli("--version")
|
||||||
|
assert b"rfd v" in stdout
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("args", ["", "--sort-by score"])
|
||||||
|
def test_threads(args):
|
||||||
|
run_cli("threads " + args)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("args", ["'pizza'", "'(coffee|starbucks)'"])
|
||||||
|
def test_search(args):
|
||||||
|
run_cli("search " + args)
|
@@ -19,11 +19,10 @@ def test_extract_post_id():
|
|||||||
|
|
||||||
def test_parse_threads(threads_api_response):
|
def test_parse_threads(threads_api_response):
|
||||||
|
|
||||||
limit = 10
|
threads = parse_threads(threads_api_response.get("topics"))
|
||||||
threads = parse_threads(threads_api_response, limit)
|
assert len(threads) == 10
|
||||||
assert len(threads) == limit
|
|
||||||
|
|
||||||
|
|
||||||
def test_parse_threads_empty():
|
def test_parse_threads_empty():
|
||||||
|
|
||||||
assert parse_threads(None, 10) == []
|
assert parse_threads(None) == []
|
||||||
|
Reference in New Issue
Block a user