mirror of
https://github.com/davegallant/rfd.git
synced 2025-08-07 09:02:32 +00:00
Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
ee6939aafe | ||
|
ad4a072325 |
11
Makefile
11
Makefile
@@ -35,6 +35,15 @@ lint:
|
|||||||
test: tmp/.tests-passed.sentinel
|
test: tmp/.tests-passed.sentinel
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
|
|
||||||
|
## examples: Run basic commands
|
||||||
|
examples: tmp/.tests-passed.sentinel
|
||||||
|
> rfd --version
|
||||||
|
> rfd threads >/dev/null
|
||||||
|
> rfd threads --sort-by score >/dev/null
|
||||||
|
> rfd search 'pizza' >/dev/null
|
||||||
|
> rfd search '(coffee|starbucks)' >/dev/null
|
||||||
|
.PHONY: examples
|
||||||
|
|
||||||
# Tests - re-ran if any file under src has been changed since tmp/.tests-passed.sentinel was last touched
|
# 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)
|
tmp/.tests-passed.sentinel: $(shell find ${SRC} -type f)
|
||||||
> mkdir -p $(@D)
|
> mkdir -p $(@D)
|
||||||
@@ -45,7 +54,7 @@ tmp/.tests-passed.sentinel: $(shell find ${SRC} -type f)
|
|||||||
pr: precommit lint test
|
pr: precommit lint test
|
||||||
.PHONY: pr
|
.PHONY: pr
|
||||||
|
|
||||||
ci: lint test
|
ci: lint test examples
|
||||||
.PHONY: ci
|
.PHONY: ci
|
||||||
|
|
||||||
## help: Print this help message
|
## help: Print this help message
|
||||||
|
@@ -7,10 +7,8 @@ Hot deals on the command line.
|
|||||||
[](https://dependabot.com/)
|
[](https://dependabot.com/)
|
||||||
[](https://lgtm.com/projects/g/davegallant/rfd/context:python)
|
[](https://lgtm.com/projects/g/davegallant/rfd/context:python)
|
||||||
|
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -37,6 +35,7 @@ Commands:
|
|||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
### View Hot Deals
|
### View Hot Deals
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ rfd threads
|
$ rfd threads
|
||||||
```
|
```
|
||||||
@@ -48,10 +47,11 @@ $ rfd threads --sort-by score
|
|||||||
```
|
```
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ rfd threads --sort-by total_views --limit 40
|
$ rfd threads --sort-by views --limit 40
|
||||||
```
|
```
|
||||||
|
|
||||||
### Simple Search
|
### Simple Search
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ rfd search 'pizza'
|
$ rfd search 'pizza'
|
||||||
```
|
```
|
||||||
@@ -61,7 +61,7 @@ $ rfd search 'pizza'
|
|||||||
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
|
||||||
```
|
```
|
||||||
|
|
||||||
## Shell Completion
|
## Shell Completion
|
||||||
|
@@ -1 +1 @@
|
|||||||
0.4.0
|
0.5.0
|
||||||
|
61
rfd/cli.py
61
rfd/cli.py
@@ -42,25 +42,29 @@ def print_version(ctx, value):
|
|||||||
ctx.exit()
|
ctx.exit()
|
||||||
|
|
||||||
|
|
||||||
def display_thread(click, thread, count): # pylint: disable=redefined-outer-name
|
def generate_thread_output(_threads):
|
||||||
dealer = thread.dealer_name
|
for count, thread in enumerate(_threads, 1):
|
||||||
if dealer and dealer is not None:
|
output = ""
|
||||||
dealer = "[" + dealer + "] "
|
dealer = thread.dealer_name
|
||||||
else:
|
if dealer and dealer is not None:
|
||||||
dealer = ""
|
dealer = "[" + dealer + "] "
|
||||||
click.echo(
|
else:
|
||||||
" "
|
dealer = ""
|
||||||
+ str(count)
|
output += (
|
||||||
+ "."
|
" "
|
||||||
+ get_vote_color(thread.score)
|
+ str(count)
|
||||||
+ Fore.RESET
|
+ "."
|
||||||
+ "%s%s" % (dealer, thread.title)
|
+ get_vote_color(thread.score)
|
||||||
+ Fore.LIGHTYELLOW_EX
|
+ Fore.RESET
|
||||||
+ " (%d views)" % thread.total_views
|
+ "%s%s" % (dealer, thread.title)
|
||||||
+ Fore.RESET
|
+ Fore.LIGHTYELLOW_EX
|
||||||
)
|
+ " (%d views)" % thread.views
|
||||||
click.echo(Fore.BLUE + " {}".format(thread.url))
|
+ Fore.RESET
|
||||||
click.echo(Style.RESET_ALL)
|
)
|
||||||
|
output += Fore.BLUE + " {}".format(thread.url)
|
||||||
|
output += Style.RESET_ALL
|
||||||
|
output += "\n\n"
|
||||||
|
yield output
|
||||||
|
|
||||||
|
|
||||||
@click.group(invoke_without_command=True)
|
@click.group(invoke_without_command=True)
|
||||||
@@ -137,17 +141,17 @@ def threads(limit, forum_id, sort_by):
|
|||||||
_threads = sort_threads(
|
_threads = sort_threads(
|
||||||
parse_threads(get_threads(forum_id, limit), limit), sort_by=sort_by
|
parse_threads(get_threads(forum_id, limit), limit), sort_by=sort_by
|
||||||
)
|
)
|
||||||
for count, thread in enumerate(_threads, 1):
|
click.echo_via_pager(generate_thread_output(_threads))
|
||||||
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 +169,12 @@ 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):
|
|
||||||
|
for page in range(1, pages):
|
||||||
_threads = parse_threads(get_threads(forum_id, 100, page=page), limit=100)
|
_threads = parse_threads(get_threads(forum_id, 100, page=page), limit=100)
|
||||||
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))
|
||||||
|
)
|
||||||
|
@@ -4,12 +4,12 @@ from .scores import calculate_score
|
|||||||
|
|
||||||
# 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
|
||||||
@@ -48,7 +48,7 @@ def parse_threads(threads, limit):
|
|||||||
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 +58,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
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user