mirror of
https://github.com/davegallant/rfd.git
synced 2025-08-05 08:23:38 +00:00
Add --sort-by flag to threads sub-command (#75)
* Add --sort-by flag to threads sub-command * Add some sanity test commands to tox
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
repos:
|
repos:
|
||||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||||
rev: v2.3.0
|
rev: v2.5.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: check-added-large-files
|
- id: check-added-large-files
|
||||||
- id: check-ast
|
- id: check-ast
|
||||||
|
44
README.md
44
README.md
@@ -5,7 +5,6 @@ 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/alerts/)
|
|
||||||
[](https://lgtm.com/projects/g/davegallant/rfd/context:python)
|
[](https://lgtm.com/projects/g/davegallant/rfd/context:python)
|
||||||
|
|
||||||
|
|
||||||
@@ -37,29 +36,46 @@ Commands:
|
|||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
### view hot deals
|
### View Hot Deals
|
||||||
```shell
|
```console
|
||||||
rfd threads
|
$ rfd threads
|
||||||
```
|
```
|
||||||
|
|
||||||
### search for pizza
|
### View and Sort Hot Deals
|
||||||
```shell
|
|
||||||
rfd search 'pizza'
|
```console
|
||||||
|
$ rfd threads --sort-by score
|
||||||
```
|
```
|
||||||
|
|
||||||
## Tab Completion
|
```console
|
||||||
|
$ rfd threads --sort-by total_views --limit 40
|
||||||
|
```
|
||||||
|
|
||||||
To enable:
|
### Simple Search
|
||||||
|
```console
|
||||||
|
$ rfd search 'pizza'
|
||||||
|
```
|
||||||
|
|
||||||
|
### RegEx Search
|
||||||
|
|
||||||
|
Regular expressions can be used for search.
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ rfd search '(coffee|starbucks)' --num-pages 100
|
||||||
|
```
|
||||||
|
|
||||||
|
## Shell Completion
|
||||||
|
|
||||||
|
Completion can be enabled if using `bash` or `zsh`.
|
||||||
|
|
||||||
### bash
|
### bash
|
||||||
|
|
||||||
```bash
|
```console
|
||||||
echo 'eval "$(_RFD_COMPLETE=source rfd)"' >> ~/.profile
|
$ echo 'eval "$(_RFD_COMPLETE=source rfd)"' >> ~/.profile
|
||||||
```
|
```
|
||||||
|
|
||||||
### zsh
|
### zsh
|
||||||
|
|
||||||
|
```console
|
||||||
```zsh
|
$ echo 'eval "$(_RFD_COMPLETE=source_zsh rfd)"' >> ~/.zshrc
|
||||||
echo 'eval "$(_RFD_COMPLETE=source_zsh rfd)"' >> ~/.zshrc
|
|
||||||
```
|
```
|
||||||
|
@@ -1 +1 @@
|
|||||||
0.3.6
|
0.4.0
|
||||||
|
@@ -6,3 +6,5 @@ __title__ = "RFD CLI"
|
|||||||
__author__ = "Dave Gallant"
|
__author__ = "Dave Gallant"
|
||||||
__license__ = "Apache 2.0"
|
__license__ = "Apache 2.0"
|
||||||
__copyright__ = "(c) 2018 Dave Gallant"
|
__copyright__ = "(c) 2018 Dave Gallant"
|
||||||
|
|
||||||
|
API_BASE_URL = "https://forums.redflagdeals.com"
|
||||||
|
@@ -6,11 +6,10 @@ except ImportError:
|
|||||||
JSONDecodeError = ValueError
|
JSONDecodeError = ValueError
|
||||||
import logging
|
import logging
|
||||||
import requests
|
import requests
|
||||||
from .constants import API_BASE_URL
|
from . import API_BASE_URL
|
||||||
from .format import strip_html, is_valid_url
|
from .posts import Post
|
||||||
from .models import Post
|
|
||||||
from .scores import calculate_score
|
from .scores import calculate_score
|
||||||
from .utils import is_int
|
from .utils import is_int, strip_html, is_valid_url
|
||||||
|
|
||||||
|
|
||||||
def extract_post_id(url):
|
def extract_post_id(url):
|
||||||
|
16
rfd/cli.py
16
rfd/cli.py
@@ -7,8 +7,7 @@ import sys
|
|||||||
import click
|
import click
|
||||||
from colorama import init, Fore, Style
|
from colorama import init, Fore, Style
|
||||||
from .api import get_threads, get_posts
|
from .api import get_threads, get_posts
|
||||||
from .search import search_threads
|
from .threads import parse_threads, search_threads, sort_threads
|
||||||
from .parsing import parse_threads
|
|
||||||
from .__version__ import version as current_version
|
from .__version__ import version as current_version
|
||||||
|
|
||||||
init()
|
init()
|
||||||
@@ -20,7 +19,7 @@ logging.getLogger().addHandler(logging.StreamHandler())
|
|||||||
|
|
||||||
|
|
||||||
def get_version():
|
def get_version():
|
||||||
return "rfd " + current_version
|
return "rfd v" + current_version
|
||||||
|
|
||||||
|
|
||||||
def get_terminal_width():
|
def get_terminal_width():
|
||||||
@@ -39,7 +38,7 @@ def get_vote_color(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
|
||||||
click.echo(get_version())
|
click.echo(get_version(), nl=False)
|
||||||
ctx.exit()
|
ctx.exit()
|
||||||
|
|
||||||
|
|
||||||
@@ -115,9 +114,10 @@ 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("--limit", default=10, help="Number of topics.")
|
|
||||||
@click.option("--forum-id", default=9, help="The forum id number")
|
@click.option("--forum-id", default=9, help="The forum id number")
|
||||||
def threads(limit, forum_id):
|
@click.option("--limit", default=10, help="Number of threads.")
|
||||||
|
@click.option("--sort-by", default=None, help="Sort threads by")
|
||||||
|
def threads(limit, forum_id, 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,7 +134,9 @@ def threads(limit, forum_id):
|
|||||||
74 \t shopping discussion
|
74 \t shopping discussion
|
||||||
88 \t cell phones
|
88 \t cell phones
|
||||||
"""
|
"""
|
||||||
_threads = parse_threads(get_threads(forum_id, limit), limit)
|
_threads = sort_threads(
|
||||||
|
parse_threads(get_threads(forum_id, limit), limit), sort_by=sort_by
|
||||||
|
)
|
||||||
for count, thread in enumerate(_threads, 1):
|
for count, thread in enumerate(_threads, 1):
|
||||||
display_thread(click, thread, count)
|
display_thread(click, thread, count)
|
||||||
|
|
||||||
|
@@ -1 +0,0 @@
|
|||||||
API_BASE_URL = "https://forums.redflagdeals.com"
|
|
@@ -1,16 +0,0 @@
|
|||||||
"""Formatting utils"""
|
|
||||||
|
|
||||||
try:
|
|
||||||
from urllib.parse import urlparse # python 3
|
|
||||||
except ImportError:
|
|
||||||
from urlparse import urlparse # python 2
|
|
||||||
from bs4 import BeautifulSoup
|
|
||||||
|
|
||||||
|
|
||||||
def strip_html(text):
|
|
||||||
return BeautifulSoup(text, "html.parser").get_text()
|
|
||||||
|
|
||||||
|
|
||||||
def is_valid_url(url):
|
|
||||||
result = urlparse(url)
|
|
||||||
return all([result.scheme, result.netloc, result.path])
|
|
@@ -1,18 +0,0 @@
|
|||||||
# pylint: disable=old-style-class
|
|
||||||
class Thread:
|
|
||||||
def __init__(self, title, dealer_name, score, url, total_views):
|
|
||||||
self.dealer_name = dealer_name
|
|
||||||
self.score = score
|
|
||||||
self.title = title
|
|
||||||
self.url = url
|
|
||||||
self.total_views = total_views
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return "Thread(%s)" % self.title
|
|
||||||
|
|
||||||
|
|
||||||
class Post:
|
|
||||||
def __init__(self, body, score, user):
|
|
||||||
self.body = body
|
|
||||||
self.score = score
|
|
||||||
self.user = user
|
|
@@ -1,42 +0,0 @@
|
|||||||
from .constants import API_BASE_URL
|
|
||||||
from .scores import calculate_score
|
|
||||||
from .models import Thread
|
|
||||||
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
Arguments:
|
|
||||||
threads {dict} -- topics response from rfd api
|
|
||||||
limit {int} -- limit number of threads returned
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
list(dict) -- digestible list of threads
|
|
||||||
"""
|
|
||||||
parsed_threads = []
|
|
||||||
if threads is None:
|
|
||||||
return []
|
|
||||||
for count, topic in enumerate(threads.get("topics"), start=1):
|
|
||||||
if count > limit:
|
|
||||||
break
|
|
||||||
parsed_threads.append(
|
|
||||||
Thread(
|
|
||||||
title=topic.get("title"),
|
|
||||||
dealer_name=get_dealer(topic),
|
|
||||||
score=calculate_score(topic),
|
|
||||||
url=build_web_path(topic.get("web_path")),
|
|
||||||
total_views=topic.get("total_views"),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return parsed_threads
|
|
8
rfd/posts.py
Normal file
8
rfd/posts.py
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# pylint: disable=old-style-class
|
||||||
|
|
||||||
|
|
||||||
|
class Post:
|
||||||
|
def __init__(self, body, score, user):
|
||||||
|
self.body = body
|
||||||
|
self.score = score
|
||||||
|
self.user = user
|
@@ -1,14 +0,0 @@
|
|||||||
import re
|
|
||||||
|
|
||||||
|
|
||||||
def search_threads(threads, regex):
|
|
||||||
"""Match deal title and dealer names with regex specified."""
|
|
||||||
|
|
||||||
regexp = re.compile(str(regex).lower())
|
|
||||||
|
|
||||||
for deal in threads:
|
|
||||||
|
|
||||||
if regexp.search(deal.title.lower()) or (
|
|
||||||
deal.dealer_name and regexp.search(deal.dealer_name.lower())
|
|
||||||
):
|
|
||||||
yield deal
|
|
76
rfd/threads.py
Normal file
76
rfd/threads.py
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
import re
|
||||||
|
from . import API_BASE_URL
|
||||||
|
from .scores import calculate_score
|
||||||
|
|
||||||
|
# pylint: disable=old-style-class
|
||||||
|
class Thread:
|
||||||
|
def __init__(self, title, dealer_name, score, url, total_views):
|
||||||
|
self.dealer_name = dealer_name
|
||||||
|
self.score = score
|
||||||
|
self.title = title
|
||||||
|
self.url = url
|
||||||
|
self.total_views = total_views
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "Thread(%s)" % self.title
|
||||||
|
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
threads {dict} -- topics response from rfd api
|
||||||
|
limit {int} -- limit number of threads returned
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
list(dict) -- digestible list of threads
|
||||||
|
"""
|
||||||
|
parsed_threads = []
|
||||||
|
if threads is None:
|
||||||
|
return []
|
||||||
|
for count, topic in enumerate(threads.get("topics"), start=1):
|
||||||
|
if count > limit:
|
||||||
|
break
|
||||||
|
parsed_threads.append(
|
||||||
|
Thread(
|
||||||
|
title=topic.get("title"),
|
||||||
|
dealer_name=get_dealer(topic),
|
||||||
|
score=calculate_score(topic),
|
||||||
|
url=build_web_path(topic.get("web_path")),
|
||||||
|
total_views=topic.get("total_views"),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return parsed_threads
|
||||||
|
|
||||||
|
|
||||||
|
def sort_threads(threads, sort_by):
|
||||||
|
"""Sort threads by an attribute"""
|
||||||
|
if sort_by is None:
|
||||||
|
return threads
|
||||||
|
assert sort_by in ["total_views", "score", "title"]
|
||||||
|
threads = sorted(threads, key=lambda x: getattr(x, sort_by), reverse=True)
|
||||||
|
return threads
|
||||||
|
|
||||||
|
|
||||||
|
def search_threads(threads, regex):
|
||||||
|
"""Match deal title and dealer names with regex specified."""
|
||||||
|
|
||||||
|
regexp = re.compile(str(regex).lower())
|
||||||
|
|
||||||
|
for deal in threads:
|
||||||
|
|
||||||
|
if regexp.search(deal.title.lower()) or (
|
||||||
|
deal.dealer_name and regexp.search(deal.dealer_name.lower())
|
||||||
|
):
|
||||||
|
yield deal
|
14
rfd/utils.py
14
rfd/utils.py
@@ -1,4 +1,18 @@
|
|||||||
"""This module provides utility functions that are used within rfd"""
|
"""This module provides utility functions that are used within rfd"""
|
||||||
|
try:
|
||||||
|
from urllib.parse import urlparse # python 2
|
||||||
|
except ImportError:
|
||||||
|
from urlparse import urlparse # python 1
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
|
|
||||||
|
def strip_html(text):
|
||||||
|
return BeautifulSoup(text, "html.parser").get_text()
|
||||||
|
|
||||||
|
|
||||||
|
def is_valid_url(url):
|
||||||
|
result = urlparse(url)
|
||||||
|
return all([result.scheme, result.netloc, result.path])
|
||||||
|
|
||||||
|
|
||||||
def is_int(number):
|
def is_int(number):
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
from rfd.api import extract_post_id
|
from rfd.api import extract_post_id
|
||||||
from rfd.parsing import build_web_path, parse_threads
|
from rfd.threads import build_web_path, parse_threads
|
||||||
|
|
||||||
|
|
||||||
def test_build_web_path():
|
def test_build_web_path():
|
||||||
|
Reference in New Issue
Block a user