mirror of
https://github.com/davegallant/rfd.git
synced 2025-08-07 09:02:32 +00:00
69
rfd/api.py
69
rfd/api.py
@@ -67,7 +67,16 @@ def get_threads(forum_id, limit):
|
||||
return threads[:limit]
|
||||
|
||||
|
||||
def get_posts(post, limit=5):
|
||||
def get_posts(post, count=5, tail=False, per_page=40):
|
||||
"""Retrieve posts from a thread.
|
||||
|
||||
Args:
|
||||
post (str): either post id or full url
|
||||
count (int, optional): Description
|
||||
|
||||
Yields:
|
||||
list(dict): body, score, and user
|
||||
"""
|
||||
if is_valid_url(post):
|
||||
post_id = extract_post_id(post)
|
||||
elif is_int(post):
|
||||
@@ -75,28 +84,62 @@ def get_posts(post, limit=5):
|
||||
else:
|
||||
raise ValueError()
|
||||
|
||||
if limit == 0:
|
||||
response = requests.get(
|
||||
"https://forums.redflagdeals.com/api/topics/{}/posts?per_page=40&page=1".format(post_id))
|
||||
pages = response.json().get('pager').get('total_pages')
|
||||
elif limit > 40:
|
||||
pages = ceil(limit / 40)
|
||||
response = requests.get(
|
||||
"https://forums.redflagdeals.com/api/topics/{}/posts?per_page=40&page=1".format(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
|
||||
if count > per_page:
|
||||
if count > total_posts:
|
||||
count = total_posts
|
||||
pages = ceil(count / per_page)
|
||||
else:
|
||||
pages = 1
|
||||
if tail:
|
||||
pages = total_pages
|
||||
else:
|
||||
pages = 1
|
||||
|
||||
if tail:
|
||||
start_page = ceil((total_posts + 1 - count) / per_page)
|
||||
start_post = (total_posts + 1 - count) % per_page
|
||||
if start_post == 0:
|
||||
start_post = per_page
|
||||
else:
|
||||
start_page, start_post = 0, 0
|
||||
|
||||
# Go through as many pages as necessary
|
||||
for page in range(pages):
|
||||
page = page + 1 # page 0 causes issues
|
||||
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(
|
||||
limit),
|
||||
per_page),
|
||||
page))
|
||||
|
||||
users = users_to_dict(response.json().get('users'))
|
||||
for _post in 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:]
|
||||
else:
|
||||
_posts = _posts[:start_post]
|
||||
|
||||
for _post in _posts:
|
||||
count -= 1
|
||||
if count < 0:
|
||||
return
|
||||
# Sometimes votes is null
|
||||
if _post.get('votes') is not None:
|
||||
calculated_score = get_vote_score(_post.get('votes').get(
|
||||
'total_up'), _post.get('votes').get('total_down'))
|
||||
else:
|
||||
calculated_score = 0
|
||||
yield{
|
||||
'body': strip_html(_post.get('body')),
|
||||
'score': get_vote_score(_post.get('votes').get('total_up'), _post.get('votes').get('total_down')),
|
||||
'score': calculated_score,
|
||||
'user': users[_post.get('author_id')],
|
||||
}
|
||||
|
@@ -28,9 +28,10 @@ def cli():
|
||||
|
||||
|
||||
@cli.command(short_help="Displays posts in a specific thread.")
|
||||
@click.option('--count', default=5, help='Number of topics. 0 for all topics')
|
||||
@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(count, post_id):
|
||||
def posts(post_id, head, tail):
|
||||
"""Displays posts in a specific thread.
|
||||
|
||||
post_id can be a full url or post id only
|
||||
@@ -41,13 +42,23 @@ def posts(count, post_id):
|
||||
url: https://forums.redflagdeals.com/koodo-targeted-public-mobile-12-120-koodo-5gb-40-no-referrals-2173603
|
||||
post_id: 2173603
|
||||
"""
|
||||
if count < 0:
|
||||
click.echo("Invalid count.")
|
||||
if head < 0:
|
||||
click.echo("Invalid head.")
|
||||
sys.exit(1)
|
||||
|
||||
if tail < 0:
|
||||
click.echo("Invalid tail.")
|
||||
sys.exit(1)
|
||||
|
||||
# Tail overrides head
|
||||
if tail > 0:
|
||||
count = tail
|
||||
else:
|
||||
count = head
|
||||
|
||||
try:
|
||||
click.echo("-" * get_terminal_width())
|
||||
for post in get_posts(post_id, count):
|
||||
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(Style.RESET_ALL)
|
||||
|
Reference in New Issue
Block a user