Files
rfd/rfd/parsing.py
Dave Gallant be92e5773d fix bug when iterating over all existing posts in a thread (#23)
* fix get posts

* remove tail/head and fix posts command
2019-09-15 23:21:40 -04:00

35 lines
955 B
Python

from .constants import API_BASE_URL
from .scores import calculate_score
from .models import Thread
def build_web_path(slug):
return "{}{}".format(API_BASE_URL, slug)
def parse_threads(threads, limit):
"""parse topics list api response into digestible list.
Arguments:
threads {dict} -- topics response from rfd api
limit {int} -- limit number of threads returned
Returns:
list(dict) -- digestible list of threads
"""
parsed_threads = []
if threads is None:
return []
for count, topic in enumerate(threads.get("topics"), start=1):
if count > limit:
break
parsed_threads.append(
Thread(
title=topic.get("title"),
dealer_name=topic["offer"].get("dealer_name"),
score=calculate_score(topic),
url=build_web_path(topic.get("web_path")),
)
)
return parsed_threads