mirror of
https://github.com/davegallant/rfd.git
synced 2025-08-07 09:02:32 +00:00
@@ -1,4 +1,4 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
version = "0.2.1"
|
||||
version = "0.3.0"
|
||||
|
18
rfd/api.py
18
rfd/api.py
@@ -26,15 +26,15 @@ def get_safe_per_page(limit):
|
||||
return limit
|
||||
|
||||
|
||||
def users_to_dict(users):
|
||||
"""Create a dictionary of user ids to usernames."""
|
||||
users_dict = {}
|
||||
def create_user_map(users):
|
||||
"""Create a map of user ids to usernames."""
|
||||
m = dict()
|
||||
for user in users:
|
||||
users_dict[user.get("user_id")] = user.get("username")
|
||||
return users_dict
|
||||
m[user.get("user_id")] = user.get("username")
|
||||
return m
|
||||
|
||||
|
||||
def get_threads(forum_id, limit):
|
||||
def get_threads(forum_id, limit, page=1):
|
||||
"""Get threads from rfd api
|
||||
|
||||
Arguments:
|
||||
@@ -46,8 +46,8 @@ def get_threads(forum_id, limit):
|
||||
"""
|
||||
try:
|
||||
response = requests.get(
|
||||
"{}/api/topics?forum_id={}&per_page={}".format(
|
||||
API_BASE_URL, forum_id, get_safe_per_page(limit)
|
||||
"{}/api/topics?forum_id={}&per_page={}&page={}".format(
|
||||
API_BASE_URL, forum_id, get_safe_per_page(limit), page
|
||||
)
|
||||
)
|
||||
if response.status_code == 200:
|
||||
@@ -86,7 +86,7 @@ def get_posts(post):
|
||||
API_BASE_URL, post_id, 40, page
|
||||
)
|
||||
)
|
||||
users = users_to_dict(response.json().get("users"))
|
||||
users = create_user_map(response.json().get("users"))
|
||||
|
||||
posts = response.json().get("posts")
|
||||
|
||||
|
48
rfd/cli.py
48
rfd/cli.py
@@ -7,6 +7,7 @@ import sys
|
||||
import click
|
||||
from colorama import init, Fore, Style
|
||||
from .api import get_threads, get_posts
|
||||
from .search import search_threads
|
||||
from .parsing import parse_threads
|
||||
from .__version__ import version as current_version
|
||||
|
||||
@@ -88,7 +89,7 @@ def posts(post_id):
|
||||
|
||||
@cli.command(short_help="Displays threads in the specified forum.")
|
||||
@click.option("--limit", default=10, help="Number of topics.")
|
||||
@click.argument("forum_id", default=9)
|
||||
@click.option("--forum-id", default=9, help="The forum id number")
|
||||
def threads(limit, forum_id):
|
||||
"""Displays threads in the specified forum id. Defaults to 9.
|
||||
|
||||
@@ -107,10 +108,10 @@ def threads(limit, forum_id):
|
||||
88 \t cell phones
|
||||
"""
|
||||
_threads = parse_threads(get_threads(forum_id, limit), limit)
|
||||
for i, thread in enumerate(_threads, 1):
|
||||
for count, thread in enumerate(_threads, 1):
|
||||
click.echo(
|
||||
" "
|
||||
+ str(i)
|
||||
+ str(count)
|
||||
+ "."
|
||||
+ get_vote_color(thread.score)
|
||||
+ Fore.RESET
|
||||
@@ -120,5 +121,46 @@ def threads(limit, forum_id):
|
||||
click.echo(Style.RESET_ALL)
|
||||
|
||||
|
||||
@cli.command(short_help="Displays threads in the specified forum.")
|
||||
@click.option("--num-pages", default=5, help="Number of pages to search.")
|
||||
@click.option(
|
||||
"--forum-id", default=9, help="The forum id number. Defaults to 9 (hot deals)."
|
||||
)
|
||||
@click.argument("keyword")
|
||||
def search(num_pages, forum_id, keyword):
|
||||
"""Searches for deals based on a keyword in the specified forum id.
|
||||
|
||||
Popular forum ids:
|
||||
|
||||
\b
|
||||
9 \t hot deals
|
||||
14 \t computer and electronics
|
||||
15 \t offtopic
|
||||
17 \t entertainment
|
||||
18 \t food and drink
|
||||
40 \t automotive
|
||||
53 \t home and garden
|
||||
67 \t fashion and apparel
|
||||
74 \t shopping discussion
|
||||
88 \t cell phones
|
||||
"""
|
||||
|
||||
count = 0
|
||||
for page in range(1, num_pages):
|
||||
_threads = parse_threads(get_threads(forum_id, 100, page=page), limit=100)
|
||||
for thread in search_threads(threads=_threads, keyword=keyword):
|
||||
count += 1
|
||||
click.echo(
|
||||
" "
|
||||
+ str(count)
|
||||
+ "."
|
||||
+ get_vote_color(thread.score)
|
||||
+ Fore.RESET
|
||||
+ "[%s] %s" % (thread.dealer_name, thread.title)
|
||||
)
|
||||
click.echo(Fore.BLUE + " {}".format(thread.url))
|
||||
click.echo(Style.RESET_ALL)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
cli()
|
||||
|
@@ -6,6 +6,9 @@ class Thread:
|
||||
self.title = title
|
||||
self.url = url
|
||||
|
||||
def __repr__(self):
|
||||
return "Thread(%s)" % self.title
|
||||
|
||||
|
||||
class Post:
|
||||
def __init__(self, body, score, user):
|
||||
|
13
rfd/search.py
Normal file
13
rfd/search.py
Normal file
@@ -0,0 +1,13 @@
|
||||
def search_threads(threads, keyword=None):
|
||||
"""Match deal title and dealer names with keyword specified."""
|
||||
|
||||
if keyword is None:
|
||||
return
|
||||
|
||||
keyword = str(keyword)
|
||||
|
||||
for deal in threads:
|
||||
if keyword.lower() in deal.title.lower() or (
|
||||
deal.dealer_name and keyword.lower() in deal.dealer_name.lower()
|
||||
):
|
||||
yield deal
|
Reference in New Issue
Block a user