mirror of
https://github.com/davegallant/rfd.git
synced 2025-08-07 09:02:32 +00:00
More pytests (#7)
* add constants.py * more tests and robustness. * try/catch JSONDecodedError.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
__version__ = '0.1.1'
|
||||
__version__ = '0.1.2'
|
||||
|
58
rfd/api.py
58
rfd/api.py
@@ -1,8 +1,14 @@
|
||||
"""RFD API."""
|
||||
|
||||
try:
|
||||
from json.decoder import JSONDecodeError
|
||||
except ImportError:
|
||||
JSONDecodeError = ValueError
|
||||
import logging
|
||||
from math import ceil
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
from rfd.constants import API_BASE_URL
|
||||
|
||||
try:
|
||||
from urllib.parse import urlparse # python 3
|
||||
@@ -11,7 +17,7 @@ except ImportError:
|
||||
|
||||
|
||||
def build_web_path(slug):
|
||||
return "https://forums.redflagdeals.com{}".format(slug)
|
||||
return "{}{}".format(API_BASE_URL, slug)
|
||||
|
||||
|
||||
def extract_post_id(url):
|
||||
@@ -70,10 +76,42 @@ def is_valid_url(url):
|
||||
|
||||
|
||||
def get_threads(forum_id, limit):
|
||||
"""Get threads from rfd api
|
||||
|
||||
Arguments:
|
||||
forum_id {int} -- forum id
|
||||
limit {[type]} -- limit number of threads returned
|
||||
|
||||
Returns:
|
||||
dict -- api response
|
||||
"""
|
||||
try:
|
||||
response = requests.get(
|
||||
"{}/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)
|
||||
except JSONDecodeError as err:
|
||||
logging.error("Unable to retrieve threads. %s", err)
|
||||
return None
|
||||
|
||||
|
||||
def parse_threads(api_response, limit):
|
||||
"""parse topics list api response into digestible list.
|
||||
|
||||
Arguments:
|
||||
api_response {dict} -- topics response from rfd api
|
||||
limit {int} -- limit number of threads returned
|
||||
|
||||
Returns:
|
||||
list(dict) -- digestible list of threads
|
||||
"""
|
||||
threads = []
|
||||
response = requests.get(
|
||||
"https://forums.redflagdeals.com/api/topics?forum_id={}&per_page={}".format(forum_id, get_safe_per_page(limit)))
|
||||
for topic in response.json().get('topics'):
|
||||
if api_response is None:
|
||||
return threads
|
||||
for topic in api_response.get('topics'):
|
||||
threads.append({
|
||||
'title': topic.get('title'),
|
||||
'score': calculate_score(topic),
|
||||
@@ -100,7 +138,8 @@ def get_posts(post, count=5, tail=False, per_page=40):
|
||||
raise ValueError()
|
||||
|
||||
response = requests.get(
|
||||
"https://forums.redflagdeals.com/api/topics/{}/posts?per_page=40&page=1".format(post_id))
|
||||
"{}/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')
|
||||
|
||||
@@ -127,10 +166,11 @@ 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(
|
||||
"https://forums.redflagdeals.com/api/topics/{}/posts?per_page={}&page={}".format(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'))
|
||||
|
||||
|
2
rfd/constants.py
Normal file
2
rfd/constants.py
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
API_BASE_URL = 'https://forums.redflagdeals.com'
|
@@ -1,16 +1,21 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
|
||||
import sys
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import click
|
||||
from colorama import init, Fore, Style
|
||||
from rfd.api import get_threads, get_posts
|
||||
from rfd.api import parse_threads, get_threads, get_posts
|
||||
from rfd.__version__ import __version__
|
||||
|
||||
init()
|
||||
print()
|
||||
|
||||
logging.getLogger()
|
||||
logging.getLogger().setLevel(logging.INFO)
|
||||
logging.getLogger().addHandler(logging.StreamHandler())
|
||||
|
||||
|
||||
def get_version():
|
||||
return 'rfd ' + __version__
|
||||
@@ -108,7 +113,7 @@ def threads(count, forum_id):
|
||||
74 \t shopping discussion
|
||||
88 \t cell phones
|
||||
"""
|
||||
_threads = get_threads(forum_id, count)
|
||||
_threads = parse_threads(get_threads(forum_id, count), count)
|
||||
for i, thread in enumerate(_threads, 1):
|
||||
click.echo(" " + str(i) + "." +
|
||||
get_vote_color(thread.get('score')) + Fore.RESET + thread.get('title'))
|
||||
|
Reference in New Issue
Block a user