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 version.py
include rfd/__version__.py

View File

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

View File

@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-
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):
return url.split('/')[3].split('-')[-1]
return url.split("/")[3].split("-")[-1]
def is_int(number):
@@ -43,8 +43,9 @@ def calculate_score(post):
"""
score = 0
try:
score = int(post.get('votes').get('total_up')) - \
int(post.get('votes').get('total_down'))
score = int(post.get("votes").get("total_up")) - int(
post.get("votes").get("total_down")
)
except AttributeError:
pass
@@ -62,7 +63,7 @@ def get_safe_per_page(limit):
def users_to_dict(users):
users_dict = {}
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
@@ -87,9 +88,10 @@ 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={}".format(
API_BASE_URL, forum_id, get_safe_per_page(limit)
)
)
if response.status_code == 200:
return response.json()
logging.error("Unable to retrieve threads. %s", response.text)
@@ -111,12 +113,14 @@ def parse_threads(api_response, limit):
threads = []
if api_response is None:
return threads
for topic in api_response.get('topics'):
threads.append({
'title': topic.get('title'),
'score': calculate_score(topic),
'url': build_web_path(topic.get('web_path')),
})
for topic in api_response.get("topics"):
threads.append(
{
"title": topic.get("title"),
"score": calculate_score(topic),
"url": build_web_path(topic.get("web_path")),
}
)
return threads[:limit]
@@ -138,10 +142,10 @@ def get_posts(post, count=5, tail=False, per_page=40):
raise ValueError()
response = requests.get(
"{}/api/topics/{}/posts?per_page=40&page=1".format(API_BASE_URL,
post_id))
total_posts = response.json().get('pager').get('total')
total_pages = response.json().get('pager').get('total_pages')
"{}/api/topics/{}/posts?per_page=40&page=1".format(API_BASE_URL, post_id)
)
total_posts = response.json().get("pager").get("total")
total_pages = response.json().get("pager").get("total_pages")
if count == 0:
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
for page in range(start_page, pages + 1):
response = requests.get(
"{}/api/topics/{}/posts?per_page={}&page={}".format(API_BASE_URL,
post_id,
get_safe_per_page(
per_page),
page))
"{}/api/topics/{}/posts?per_page={}&page={}".format(
API_BASE_URL, post_id, 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)
if page == start_page and not start_post == 0:
if tail:
_posts = _posts[start_post - 1:]
_posts = _posts[start_post - 1 :]
else:
_posts = _posts[:start_post]
@@ -188,12 +191,12 @@ def get_posts(post, count=5, tail=False, per_page=40):
if count < 0:
return
# Sometimes votes is null
if _post.get('votes') is not None:
if _post.get("votes") is not None:
calculated_score = calculate_score(_post)
else:
calculated_score = 0
yield{
'body': strip_html(_post.get('body')),
'score': calculated_score,
'user': users[_post.get('author_id')],
yield {
"body": strip_html(_post.get("body")),
"score": calculated_score,
"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
from colorama import init, Fore, Style
from rfd.api import parse_threads, get_threads, get_posts
from rfd.__version__ import __version__
from rfd.__version__ import version as current_version
init()
print()
@@ -18,11 +18,11 @@ logging.getLogger().addHandler(logging.StreamHandler())
def get_version():
return 'rfd ' + __version__
return "rfd " + current_version
def get_terminal_width():
_, columns = os.popen('stty size', 'r').read().split()
_, columns = os.popen("stty size", "r").read().split()
return int(columns)
@@ -35,7 +35,7 @@ def get_vote_color(score):
@click.group(invoke_without_command=True)
@click.option('--version/--no-version', default=False)
@click.option("--version/--no-version", default=False)
@click.pass_context
def cli(ctx, version):
"""Welcome to the RFD CLI. (RedFlagDeals.com)"""
@@ -45,15 +45,21 @@ def cli(ctx, version):
click.echo(ctx.get_help())
@cli.command('version')
@cli.command("version")
def display_version():
click.echo(get_version())
@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('--tail', default=0, help='Number of topics. Default is disabled. This will override head.')
@click.argument('post_id')
@click.option(
"--head", default=0, help="Number of topics. Default is 0, for all topics"
)
@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):
"""Displays posts in a specific thread.
@@ -82,8 +88,14 @@ def posts(post_id, head, tail):
try:
click.echo("-" * get_terminal_width())
for post in get_posts(post=post_id, count=count, tail=tail > 0):
click.echo(" -" + get_vote_color(post.get('score')) + Fore.RESET +
post.get('body') + Fore.YELLOW + " ({})".format(post.get('user')))
click.echo(
" -"
+ get_vote_color(post.get("score"))
+ Fore.RESET
+ post.get("body")
+ Fore.YELLOW
+ " ({})".format(post.get("user"))
)
click.echo(Style.RESET_ALL)
click.echo("-" * get_terminal_width())
except ValueError:
@@ -94,8 +106,8 @@ def posts(post_id, head, tail):
@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("--limit", default=10, help="Number of topics.")
@click.argument("forum_id", default=9)
def threads(limit, forum_id):
"""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)
for i, thread in enumerate(_threads, 1):
click.echo(" " + str(i) + "." +
get_vote_color(thread.get('score')) + Fore.RESET + thread.get('title'))
click.echo(Fore.BLUE + " {}".format(thread.get('url')))
click.echo(
" "
+ 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)
if __name__ == '__main__':
if __name__ == "__main__":
cli()

View File

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

View File

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