black files and remove version symlink. (#13)

This commit is contained in:
Dave Gallant
2018-10-08 23:07:23 -04:00
committed by GitHub
parent 56ab030423
commit 5b56e0d6c9
8 changed files with 84 additions and 67 deletions

View File

@@ -1,2 +1,2 @@
include requirements.txt include requirements.txt
include version.py include rfd/__version__.py

View File

@@ -1,4 +1,4 @@
beautifulsoup4>=4.6.0 beautifulsoup4>=4.6.0
click>=6.0 click>=7.0
colorama>=0.3.9 colorama>=0.3.9
requests>=2.18.0 requests>=2.18.0

View File

@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
__version__ = '0.1.6' version = '0.1.7'

View File

@@ -21,7 +21,7 @@ def build_web_path(slug):
def extract_post_id(url): def extract_post_id(url):
return url.split('/')[3].split('-')[-1] return url.split("/")[3].split("-")[-1]
def is_int(number): def is_int(number):
@@ -43,8 +43,9 @@ def calculate_score(post):
""" """
score = 0 score = 0
try: try:
score = int(post.get('votes').get('total_up')) - \ score = int(post.get("votes").get("total_up")) - int(
int(post.get('votes').get('total_down')) post.get("votes").get("total_down")
)
except AttributeError: except AttributeError:
pass pass
@@ -62,7 +63,7 @@ def get_safe_per_page(limit):
def users_to_dict(users): def users_to_dict(users):
users_dict = {} users_dict = {}
for user in users: for user in users:
users_dict[user.get('user_id')] = user.get('username') users_dict[user.get("user_id")] = user.get("username")
return users_dict return users_dict
@@ -87,9 +88,10 @@ def get_threads(forum_id, limit):
""" """
try: try:
response = requests.get( response = requests.get(
"{}/api/topics?forum_id={}&per_page={}".format(API_BASE_URL, "{}/api/topics?forum_id={}&per_page={}".format(
forum_id, API_BASE_URL, forum_id, get_safe_per_page(limit)
get_safe_per_page(limit))) )
)
if response.status_code == 200: if response.status_code == 200:
return response.json() return response.json()
logging.error("Unable to retrieve threads. %s", response.text) logging.error("Unable to retrieve threads. %s", response.text)
@@ -111,12 +113,14 @@ def parse_threads(api_response, limit):
threads = [] threads = []
if api_response is None: if api_response is None:
return threads return threads
for topic in api_response.get('topics'): for topic in api_response.get("topics"):
threads.append({ threads.append(
'title': topic.get('title'), {
'score': calculate_score(topic), "title": topic.get("title"),
'url': build_web_path(topic.get('web_path')), "score": calculate_score(topic),
}) "url": build_web_path(topic.get("web_path")),
}
)
return threads[:limit] return threads[:limit]
@@ -138,10 +142,10 @@ def get_posts(post, count=5, tail=False, per_page=40):
raise ValueError() raise ValueError()
response = requests.get( response = requests.get(
"{}/api/topics/{}/posts?per_page=40&page=1".format(API_BASE_URL, "{}/api/topics/{}/posts?per_page=40&page=1".format(API_BASE_URL, post_id)
post_id)) )
total_posts = response.json().get('pager').get('total') total_posts = response.json().get("pager").get("total")
total_pages = response.json().get('pager').get('total_pages') total_pages = response.json().get("pager").get("total_pages")
if count == 0: if count == 0:
pages = total_pages pages = total_pages
@@ -166,20 +170,19 @@ def get_posts(post, count=5, tail=False, per_page=40):
# Go through as many pages as necessary # Go through as many pages as necessary
for page in range(start_page, pages + 1): for page in range(start_page, pages + 1):
response = requests.get( response = requests.get(
"{}/api/topics/{}/posts?per_page={}&page={}".format(API_BASE_URL, "{}/api/topics/{}/posts?per_page={}&page={}".format(
post_id, API_BASE_URL, post_id, get_safe_per_page(per_page), page
get_safe_per_page( )
per_page), )
page))
users = users_to_dict(response.json().get('users')) users = users_to_dict(response.json().get("users"))
_posts = response.json().get('posts') _posts = response.json().get("posts")
# Determine which post to start with (for --tail) # Determine which post to start with (for --tail)
if page == start_page and not start_post == 0: if page == start_page and not start_post == 0:
if tail: if tail:
_posts = _posts[start_post - 1:] _posts = _posts[start_post - 1 :]
else: else:
_posts = _posts[:start_post] _posts = _posts[:start_post]
@@ -188,12 +191,12 @@ def get_posts(post, count=5, tail=False, per_page=40):
if count < 0: if count < 0:
return return
# Sometimes votes is null # Sometimes votes is null
if _post.get('votes') is not None: if _post.get("votes") is not None:
calculated_score = calculate_score(_post) calculated_score = calculate_score(_post)
else: else:
calculated_score = 0 calculated_score = 0
yield{ yield {
'body': strip_html(_post.get('body')), "body": strip_html(_post.get("body")),
'score': calculated_score, "score": calculated_score,
'user': users[_post.get('author_id')], "user": users[_post.get("author_id")],
} }

View File

@@ -1,2 +1 @@
API_BASE_URL = "https://forums.redflagdeals.com"
API_BASE_URL = 'https://forums.redflagdeals.com'

View File

@@ -7,7 +7,7 @@ import sys
import click import click
from colorama import init, Fore, Style from colorama import init, Fore, Style
from rfd.api import parse_threads, get_threads, get_posts from rfd.api import parse_threads, get_threads, get_posts
from rfd.__version__ import __version__ from rfd.__version__ import version as current_version
init() init()
print() print()
@@ -18,11 +18,11 @@ logging.getLogger().addHandler(logging.StreamHandler())
def get_version(): def get_version():
return 'rfd ' + __version__ return "rfd " + current_version
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)
@@ -35,7 +35,7 @@ def get_vote_color(score):
@click.group(invoke_without_command=True) @click.group(invoke_without_command=True)
@click.option('--version/--no-version', default=False) @click.option("--version/--no-version", default=False)
@click.pass_context @click.pass_context
def cli(ctx, version): def cli(ctx, version):
"""Welcome to the RFD CLI. (RedFlagDeals.com)""" """Welcome to the RFD CLI. (RedFlagDeals.com)"""
@@ -45,15 +45,21 @@ def cli(ctx, version):
click.echo(ctx.get_help()) click.echo(ctx.get_help())
@cli.command('version') @cli.command("version")
def display_version(): def display_version():
click.echo(get_version()) click.echo(get_version())
@cli.command(short_help="Displays posts in a specific thread.") @cli.command(short_help="Displays posts in a specific thread.")
@click.option('--head', default=0, help='Number of topics. Default is 0, for all topics') @click.option(
@click.option('--tail', default=0, help='Number of topics. Default is disabled. This will override head.') "--head", default=0, help="Number of topics. Default is 0, for all topics"
@click.argument('post_id') )
@click.option(
"--tail",
default=0,
help="Number of topics. Default is disabled. This will override head.",
)
@click.argument("post_id")
def posts(post_id, head, tail): def posts(post_id, head, tail):
"""Displays posts in a specific thread. """Displays posts in a specific thread.
@@ -82,8 +88,14 @@ def posts(post_id, head, tail):
try: try:
click.echo("-" * get_terminal_width()) click.echo("-" * get_terminal_width())
for post in get_posts(post=post_id, count=count, tail=tail > 0): for post in get_posts(post=post_id, count=count, tail=tail > 0):
click.echo(" -" + get_vote_color(post.get('score')) + Fore.RESET + click.echo(
post.get('body') + Fore.YELLOW + " ({})".format(post.get('user'))) " -"
+ get_vote_color(post.get("score"))
+ Fore.RESET
+ post.get("body")
+ Fore.YELLOW
+ " ({})".format(post.get("user"))
)
click.echo(Style.RESET_ALL) click.echo(Style.RESET_ALL)
click.echo("-" * get_terminal_width()) click.echo("-" * get_terminal_width())
except ValueError: except ValueError:
@@ -94,8 +106,8 @@ def posts(post_id, head, tail):
@cli.command(short_help="Displays threads in the specified forum.") @cli.command(short_help="Displays threads in the specified forum.")
@click.option('--limit', default=10, help='Number of topics.') @click.option("--limit", default=10, help="Number of topics.")
@click.argument('forum_id', default=9) @click.argument("forum_id", default=9)
def threads(limit, forum_id): def threads(limit, forum_id):
"""Displays threads in the specified forum id. Defaults to 9. """Displays threads in the specified forum id. Defaults to 9.
@@ -115,11 +127,17 @@ def threads(limit, forum_id):
""" """
_threads = parse_threads(get_threads(forum_id, limit), limit) _threads = parse_threads(get_threads(forum_id, limit), limit)
for i, thread in enumerate(_threads, 1): for i, thread in enumerate(_threads, 1):
click.echo(" " + str(i) + "." + click.echo(
get_vote_color(thread.get('score')) + Fore.RESET + thread.get('title')) " "
click.echo(Fore.BLUE + " {}".format(thread.get('url'))) + str(i)
+ "."
+ get_vote_color(thread.get("score"))
+ Fore.RESET
+ thread.get("title")
)
click.echo(Fore.BLUE + " {}".format(thread.get("url")))
click.echo(Style.RESET_ALL) click.echo(Style.RESET_ALL)
if __name__ == '__main__': if __name__ == "__main__":
cli() cli()

View File

@@ -1,39 +1,37 @@
import io # for python2 import io # for python2
from os import path from os import path
from setuptools import setup, find_packages from setuptools import setup, find_packages
try: # for pip >= 10 try: # for pip >= 10
from pip._internal.req import parse_requirements from pip._internal.req import parse_requirements
except ImportError: # for pip <= 9.0.3 except ImportError: # for pip <= 9.0.3
from pip.req import parse_requirements from pip.req import parse_requirements
from version import __version__ as version from rfd.__version__ import version
WORKING_DIR = path.abspath(path.dirname(__file__)) WORKING_DIR = path.abspath(path.dirname(__file__))
# Get long description from README.md # Get long description from README.md
with io.open(path.join(WORKING_DIR, 'README.md'), encoding='utf-8') as f: with io.open(path.join(WORKING_DIR, "README.md"), encoding="utf-8") as f:
long_description = f.read() long_description = f.read()
# read requirements.txt and load into list # read requirements.txt and load into list
REQUIREMENTS_TXT = parse_requirements( REQUIREMENTS_TXT = parse_requirements(
path.join(WORKING_DIR, "requirements.txt"), session='my_session') path.join(WORKING_DIR, "requirements.txt"), session="my_session"
)
REQUIREMENTS = [str(r.req) for r in REQUIREMENTS_TXT] REQUIREMENTS = [str(r.req) for r in REQUIREMENTS_TXT]
setup( setup(
author='Dave Gallant', author="Dave Gallant",
author_email='davegallant@gmail.com', author_email="davegallant@gmail.com",
description='CLI for RedFlagDeals.com', description="CLI for RedFlagDeals.com",
entry_points={ entry_points={"console_scripts": ["rfd = rfd.rfd_cli:cli"]},
'console_scripts': [
'rfd = rfd.rfd_cli:cli',
],
},
install_requires=REQUIREMENTS, install_requires=REQUIREMENTS,
keywords='cli redflagdeals', keywords="cli redflagdeals",
license='Apache License, Version 2.0', license="Apache License, Version 2.0",
long_description=long_description, long_description=long_description,
long_description_content_type="text/markdown", long_description_content_type="text/markdown",
name='rfd', name="rfd",
packages=find_packages(), packages=find_packages(),
url='https://github.com/davegallant/rfd_cli', url="https://github.com/davegallant/rfd_cli",
version=version, version=version,
) )

View File

@@ -1 +0,0 @@
rfd/__version__.py