mirror of
https://github.com/davegallant/rfd.git
synced 2025-08-07 17:12:29 +00:00
Compare commits
21 Commits
bump-versi
...
dependabot
Author | SHA1 | Date | |
---|---|---|---|
|
4e8a302253 | ||
|
dcd2a49037 | ||
|
224680a8e1 | ||
|
1f4dc1ace9 | ||
|
e24b7fbaed | ||
|
16ca06ef58 | ||
|
40cd26166e | ||
|
f4dd94cfff | ||
|
c148897a00 | ||
|
ac10720c68 | ||
|
260ef74d4e | ||
|
3df07dc65e | ||
|
b96cab1638 | ||
|
67d3f2db14 | ||
|
b0e11ec375 | ||
|
3421e107b6 | ||
|
3b3aeac80e | ||
|
a7e20b77e6 | ||
|
90af78c5f2 | ||
|
9e3a92d548 | ||
|
9c61c419aa |
7
.github/dependabot.yml
vendored
7
.github/dependabot.yml
vendored
@@ -4,5 +4,8 @@ updates:
|
|||||||
directory: "/"
|
directory: "/"
|
||||||
schedule:
|
schedule:
|
||||||
interval: weekly
|
interval: weekly
|
||||||
time: "8:00"
|
|
||||||
open-pull-requests-limit: 10
|
- package-ecosystem: github-actions
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: weekly
|
||||||
|
2
.github/workflows/black.yml
vendored
2
.github/workflows/black.yml
vendored
@@ -6,5 +6,5 @@ jobs:
|
|||||||
lint:
|
lint:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v4
|
||||||
- uses: psf/black@stable
|
- uses: psf/black@stable
|
||||||
|
6
.github/workflows/codeql-analysis.yml
vendored
6
.github/workflows/codeql-analysis.yml
vendored
@@ -13,7 +13,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
fetch-depth: 2
|
fetch-depth: 2
|
||||||
|
|
||||||
@@ -21,7 +21,7 @@ jobs:
|
|||||||
if: ${{ github.event_name == 'pull_request' }}
|
if: ${{ github.event_name == 'pull_request' }}
|
||||||
|
|
||||||
- name: Initialize CodeQL
|
- name: Initialize CodeQL
|
||||||
uses: github/codeql-action/init@v1
|
uses: github/codeql-action/init@v3
|
||||||
|
|
||||||
- name: Perform CodeQL Analysis
|
- name: Perform CodeQL Analysis
|
||||||
uses: github/codeql-action/analyze@v1
|
uses: github/codeql-action/analyze@v3
|
||||||
|
4
.github/workflows/pypi.yml
vendored
4
.github/workflows/pypi.yml
vendored
@@ -7,9 +7,9 @@ jobs:
|
|||||||
if: startsWith(github.ref, 'refs/tags')
|
if: startsWith(github.ref, 'refs/tags')
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@master
|
- uses: actions/checkout@v4
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
uses: actions/setup-python@v1
|
uses: actions/setup-python@v5
|
||||||
with:
|
with:
|
||||||
python-version: 3.9
|
python-version: 3.9
|
||||||
- name: Install pypa/build
|
- name: Install pypa/build
|
||||||
|
4
.github/workflows/tests.yml
vendored
4
.github/workflows/tests.yml
vendored
@@ -16,10 +16,10 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Set up Python ${{ matrix.python-version }}
|
- name: Set up Python ${{ matrix.python-version }}
|
||||||
uses: actions/setup-python@v1
|
uses: actions/setup-python@v5
|
||||||
with:
|
with:
|
||||||
python-version: ${{ matrix.python-version }}
|
python-version: ${{ matrix.python-version }}
|
||||||
|
|
||||||
|
@@ -1,4 +1,2 @@
|
|||||||
include rfd/VERSION
|
|
||||||
|
|
||||||
recursive-exclude * __pycache__
|
recursive-exclude * __pycache__
|
||||||
recursive-include tests *
|
recursive-include tests *
|
||||||
|
16
README.md
16
README.md
@@ -12,8 +12,9 @@
|
|||||||
- [View Hot Deals](#view-hot-deals)
|
- [View Hot Deals](#view-hot-deals)
|
||||||
- [View and Sort Hot Deals](#view-and-sort-hot-deals)
|
- [View and Sort Hot Deals](#view-and-sort-hot-deals)
|
||||||
- [Search](#search)
|
- [Search](#search)
|
||||||
- [Advanced](#advanced)
|
- [Regex](#regex)
|
||||||
- [View Posts](#view-posts)
|
- [View Posts](#view-posts)
|
||||||
|
- [JSON Output](#json-output)
|
||||||
- [Shell Completion](#shell-completion)
|
- [Shell Completion](#shell-completion)
|
||||||
- [bash](#bash)
|
- [bash](#bash)
|
||||||
- [zsh](#zsh)
|
- [zsh](#zsh)
|
||||||
@@ -47,7 +48,6 @@ If you have [brew](https://brew.sh):
|
|||||||
brew install davegallant/public/rfd
|
brew install davegallant/public/rfd
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
All commands open up in a [terminal pager](https://en.wikipedia.org/wiki/Terminal_pager).
|
All commands open up in a [terminal pager](https://en.wikipedia.org/wiki/Terminal_pager).
|
||||||
@@ -93,7 +93,7 @@ rfd threads --sort-by views --pages 10
|
|||||||
rfd search 'pizza'
|
rfd search 'pizza'
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Advanced
|
#### Regex
|
||||||
|
|
||||||
Regular expressions can be used for search.
|
Regular expressions can be used for search.
|
||||||
|
|
||||||
@@ -111,6 +111,16 @@ rfd posts https://forums.redflagdeals.com/kobo-vs-kindle-2396227/
|
|||||||
|
|
||||||
This allows for easy grepping and searching for desired expressions.
|
This allows for easy grepping and searching for desired expressions.
|
||||||
|
|
||||||
|
### JSON Output
|
||||||
|
|
||||||
|
All commands support JSON output.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
rfd threads --output json
|
||||||
|
```
|
||||||
|
|
||||||
## Shell Completion
|
## Shell Completion
|
||||||
|
|
||||||
Shell completion can be enabled if using `bash` or `zsh`.
|
Shell completion can be enabled if using `bash` or `zsh`.
|
||||||
|
866
poetry.lock
generated
866
poetry.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
|||||||
[tool.poetry]
|
[tool.poetry]
|
||||||
name = "rfd"
|
name = "rfd"
|
||||||
version = "0.8.0"
|
version = "0.9.0"
|
||||||
description = "view RedFlagDeals.com from the command line"
|
description = "view RedFlagDeals.com from the command line"
|
||||||
authors = ["Dave Gallant <davegallant@gmail.com>"]
|
authors = ["Dave Gallant <davegallant@gmail.com>"]
|
||||||
license = "GPL-3.0-or-later"
|
license = "GPL-3.0-or-later"
|
||||||
@@ -15,10 +15,10 @@ requests = ">=2.22.0"
|
|||||||
soupsieve = "<3.0"
|
soupsieve = "<3.0"
|
||||||
|
|
||||||
[tool.poetry.dev-dependencies]
|
[tool.poetry.dev-dependencies]
|
||||||
pre-commit = "1.21.0"
|
pre-commit = "2.1.1"
|
||||||
pylint = "^2.12.0"
|
pylint = "^2.12.0"
|
||||||
pytest = ">=4.6.6"
|
pytest = ">=4.6.6"
|
||||||
rope = "1.0.0"
|
rope = "1.1.1"
|
||||||
|
|
||||||
[tool.poetry.scripts]
|
[tool.poetry.scripts]
|
||||||
rfd = "rfd.__main__:cli"
|
rfd = "rfd.__main__:cli"
|
||||||
|
24
rfd/api.py
24
rfd/api.py
@@ -65,7 +65,7 @@ def get_posts(post):
|
|||||||
Args:
|
Args:
|
||||||
post (str): either full url or postid
|
post (str): either full url or postid
|
||||||
|
|
||||||
Yields:
|
Returns:
|
||||||
list(Post): Posts
|
list(Post): Posts
|
||||||
"""
|
"""
|
||||||
if is_valid_url(post):
|
if is_valid_url(post):
|
||||||
@@ -81,22 +81,28 @@ def get_posts(post):
|
|||||||
|
|
||||||
total_pages = response.json().get("pager").get("total_pages")
|
total_pages = response.json().get("pager").get("total_pages")
|
||||||
|
|
||||||
|
posts = []
|
||||||
|
|
||||||
for page in range(0, total_pages + 1):
|
for page in range(0, total_pages + 1):
|
||||||
response = requests.get(
|
response = requests.get(
|
||||||
f"{API_BASE_URL}/api/topics/{post_id}/posts?per_page=40&page={page}"
|
f"{API_BASE_URL}/api/topics/{post_id}/posts?per_page=40&page={page}"
|
||||||
)
|
)
|
||||||
users = create_user_map(response.json().get("users"))
|
users = create_user_map(response.json().get("users"))
|
||||||
|
|
||||||
posts = response.json().get("posts")
|
current_posts = response.json().get("posts")
|
||||||
|
|
||||||
for i in posts:
|
for _post in current_posts:
|
||||||
# Sometimes votes is null
|
# Sometimes votes is null
|
||||||
if i.get("votes") is not None:
|
if _post.get("votes") is not None:
|
||||||
calculated_score = calculate_score(i)
|
calculated_score = calculate_score(_post)
|
||||||
else:
|
else:
|
||||||
calculated_score = 0
|
calculated_score = 0
|
||||||
yield Post(
|
posts.append(
|
||||||
body=strip_html(i.get("body")),
|
Post(
|
||||||
score=calculated_score,
|
body=strip_html(_post.get("body")),
|
||||||
user=users[i.get("author_id")],
|
score=calculated_score,
|
||||||
|
user=users[_post.get("author_id")],
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return posts
|
||||||
|
73
rfd/cli.py
73
rfd/cli.py
@@ -3,6 +3,7 @@ from __future__ import unicode_literals
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
|
import json
|
||||||
import click
|
import click
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -11,8 +12,14 @@ except ImportError: # for Python<3.8
|
|||||||
import importlib_metadata as metadata
|
import importlib_metadata as metadata
|
||||||
from colorama import init
|
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, generate_thread_output
|
from .threads import (
|
||||||
from .posts import generate_posts_output
|
parse_threads,
|
||||||
|
search_threads,
|
||||||
|
sort_threads,
|
||||||
|
generate_thread_output,
|
||||||
|
ThreadEncoder,
|
||||||
|
)
|
||||||
|
from .posts import generate_posts_output, PostEncoder
|
||||||
|
|
||||||
|
|
||||||
init()
|
init()
|
||||||
@@ -51,7 +58,10 @@ def cli(ctx):
|
|||||||
|
|
||||||
@cli.command(short_help="Display all posts in a thread.")
|
@cli.command(short_help="Display all posts in a thread.")
|
||||||
@click.argument("post_id")
|
@click.argument("post_id")
|
||||||
def posts(post_id):
|
@click.option(
|
||||||
|
"--output", default=None, help="Defaults to custom formatting. Other options: json"
|
||||||
|
)
|
||||||
|
def posts(post_id, output):
|
||||||
"""Iterate all pages and display all posts in a thread.
|
"""Iterate all pages and display all posts in a thread.
|
||||||
|
|
||||||
post_id can be a full url or post id only
|
post_id can be a full url or post id only
|
||||||
@@ -63,12 +73,23 @@ def posts(post_id):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
click.echo_via_pager(generate_posts_output(get_posts(post=post_id)))
|
if output == "json":
|
||||||
|
click.echo_via_pager(
|
||||||
|
json.dumps(
|
||||||
|
get_posts(post=post_id),
|
||||||
|
cls=PostEncoder,
|
||||||
|
indent=2,
|
||||||
|
sort_keys=True,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
click.echo_via_pager(generate_posts_output(get_posts(post=post_id)))
|
||||||
|
|
||||||
except ValueError:
|
except ValueError:
|
||||||
click.echo("Invalid post id.")
|
click.echo("Invalid post id.")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
except AttributeError:
|
except AttributeError as err:
|
||||||
click.echo("The RFD API did not return the expected data.")
|
click.echo("The RFD API did not return the expected data. %s", err)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
@@ -76,7 +97,10 @@ def posts(post_id):
|
|||||||
@click.option("--forum-id", default=9, help="The forum id number")
|
@click.option("--forum-id", default=9, help="The forum id number")
|
||||||
@click.option("--pages", default=1, help="Number of pages to show. Defaults to 1.")
|
@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(forum_id, pages, sort_by):
|
@click.option(
|
||||||
|
"--output", default=None, help="Defaults to custom formatting. Other options: json"
|
||||||
|
)
|
||||||
|
def threads(forum_id, pages, sort_by, output):
|
||||||
"""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:
|
||||||
@@ -96,7 +120,18 @@ def threads(forum_id, pages, sort_by):
|
|||||||
_threads = sort_threads(
|
_threads = sort_threads(
|
||||||
parse_threads(get_threads(forum_id, pages)), sort_by=sort_by
|
parse_threads(get_threads(forum_id, pages)), sort_by=sort_by
|
||||||
)
|
)
|
||||||
click.echo_via_pager(generate_thread_output(_threads))
|
if output == "json":
|
||||||
|
|
||||||
|
click.echo_via_pager(
|
||||||
|
json.dumps(
|
||||||
|
sort_threads(_threads, sort_by=sort_by),
|
||||||
|
cls=ThreadEncoder,
|
||||||
|
indent=2,
|
||||||
|
sort_keys=True,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
click.echo_via_pager(generate_thread_output(_threads))
|
||||||
|
|
||||||
|
|
||||||
@cli.command(short_help="Search deals based on a regular expression.")
|
@cli.command(short_help="Search deals based on a regular expression.")
|
||||||
@@ -105,8 +140,11 @@ def threads(forum_id, pages, sort_by):
|
|||||||
"--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.option("--sort-by", default=None, help="Sort threads by")
|
||||||
|
@click.option(
|
||||||
|
"--output", default=None, help="Defaults to custom formatting. Other options: json"
|
||||||
|
)
|
||||||
@click.argument("regex")
|
@click.argument("regex")
|
||||||
def search(pages, forum_id, sort_by, regex):
|
def search(pages, forum_id, sort_by, output, regex):
|
||||||
"""Search deals based on regex.
|
"""Search deals based on regex.
|
||||||
|
|
||||||
Popular forum ids:
|
Popular forum ids:
|
||||||
@@ -129,6 +167,17 @@ def search(pages, forum_id, sort_by, regex):
|
|||||||
_threads = parse_threads(get_threads(forum_id, pages=pages))
|
_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):
|
||||||
matched_threads.append(thread)
|
matched_threads.append(thread)
|
||||||
click.echo_via_pager(
|
|
||||||
generate_thread_output(sort_threads(matched_threads, sort_by=sort_by))
|
if output == "json":
|
||||||
)
|
click.echo_via_pager(
|
||||||
|
json.dumps(
|
||||||
|
sort_threads(matched_threads, sort_by=sort_by),
|
||||||
|
indent=2,
|
||||||
|
sort_keys=True,
|
||||||
|
cls=ThreadEncoder,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
click.echo_via_pager(
|
||||||
|
generate_thread_output(sort_threads(matched_threads, sort_by=sort_by))
|
||||||
|
)
|
||||||
|
12
rfd/posts.py
12
rfd/posts.py
@@ -1,5 +1,6 @@
|
|||||||
# pylint: disable=old-style-class
|
# pylint: disable=old-style-class
|
||||||
import os
|
import os
|
||||||
|
import json
|
||||||
from colorama import Fore, Style
|
from colorama import Fore, Style
|
||||||
from .scores import get_vote_color
|
from .scores import get_vote_color
|
||||||
|
|
||||||
@@ -11,6 +12,17 @@ class Post:
|
|||||||
self.user = user
|
self.user = user
|
||||||
|
|
||||||
|
|
||||||
|
class PostEncoder(json.JSONEncoder):
|
||||||
|
def default(self, o):
|
||||||
|
if isinstance(o, Post):
|
||||||
|
return dict(
|
||||||
|
body=o.body,
|
||||||
|
score=o.score,
|
||||||
|
user=o.user,
|
||||||
|
)
|
||||||
|
return json.JSONEncoder.default(self, o)
|
||||||
|
|
||||||
|
|
||||||
def get_terminal_width():
|
def get_terminal_width():
|
||||||
_, columns = os.popen("stty size", "r").read().split()
|
_, columns = os.popen("stty size", "r").read().split()
|
||||||
return int(columns)
|
return int(columns)
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
import re
|
import re
|
||||||
|
import json
|
||||||
from colorama import Fore, Style
|
from colorama import Fore, Style
|
||||||
from . import API_BASE_URL
|
from . import API_BASE_URL
|
||||||
from .scores import calculate_score, get_vote_color
|
from .scores import calculate_score, get_vote_color
|
||||||
@@ -16,6 +17,19 @@ class Thread:
|
|||||||
return f"Thread({self.title})"
|
return f"Thread({self.title})"
|
||||||
|
|
||||||
|
|
||||||
|
class ThreadEncoder(json.JSONEncoder):
|
||||||
|
def default(self, o):
|
||||||
|
if isinstance(o, Thread):
|
||||||
|
return dict(
|
||||||
|
dealer_name=o.dealer_name,
|
||||||
|
score=o.score,
|
||||||
|
title=o.title,
|
||||||
|
url=o.url,
|
||||||
|
views=o.views,
|
||||||
|
)
|
||||||
|
return json.JSONEncoder.default(self, o)
|
||||||
|
|
||||||
|
|
||||||
def build_web_path(slug):
|
def build_web_path(slug):
|
||||||
return f"{API_BASE_URL}{slug}"
|
return f"{API_BASE_URL}{slug}"
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user