Adds topic IDs to the topics and primary_topics entries in the topic timeline response.
#111
@@ -1,6 +1,9 @@
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class TopicCount(BaseModel):
|
||||
id: Optional[str] = Field(default=None, description="The topic id.")
|
||||
name: str = Field(description="The topic display name.")
|
||||
count: int = Field(default=0, description="The number of occurrences.")
|
||||
|
||||
@@ -117,45 +117,57 @@ async def get_topics_by_time_by_author(
|
||||
for topic in work.topics or []:
|
||||
if topic.display_name:
|
||||
topic_name = topic.display_name
|
||||
topic_id = topic.id
|
||||
|
||||
if topic_name not in years[year]["topics"]:
|
||||
years[year]["topics"][topic_name] = 0
|
||||
years[year]["topics"][topic_name] = {
|
||||
"id": topic_id,
|
||||
"name": topic_name,
|
||||
"count": 0,
|
||||
}
|
||||
|
||||
years[year]["topics"][topic_name] += 1
|
||||
years[year]["topics"][topic_name]["count"] += 1
|
||||
|
||||
primary_topic = work.primary_topic
|
||||
|
||||
if primary_topic and primary_topic.display_name:
|
||||
primary_topic_name = primary_topic.display_name
|
||||
primary_topic_id = primary_topic.id
|
||||
|
||||
if primary_topic_name not in years[year]["primary_topics"]:
|
||||
years[year]["primary_topics"][primary_topic_name] = 0
|
||||
|
||||
years[year]["primary_topics"][primary_topic_name] += 1
|
||||
years[year]["primary_topics"][primary_topic_name] = {
|
||||
"id": primary_topic_id,
|
||||
"name": primary_topic_name,
|
||||
"count": 0,
|
||||
}
|
||||
|
||||
years[year]["primary_topics"][primary_topic_name]["count"] += 1
|
||||
|
||||
elements = []
|
||||
|
||||
for year in sorted(years.keys()):
|
||||
topic_items = list(years[year]["topics"].items())
|
||||
topic_items.sort(key=lambda item: item[1], reverse=True)
|
||||
topic_items = list(years[year]["topics"].values())
|
||||
topic_items.sort(key=lambda item: item["count"], reverse=True)
|
||||
|
||||
topics = []
|
||||
|
||||
for topic_name, count in topic_items[:max_topics_per_year]:
|
||||
for topic in topic_items[:max_topics_per_year]:
|
||||
topics.append(TopicCount(
|
||||
name=topic_name,
|
||||
count=count,
|
||||
id=topic["id"],
|
||||
name=topic["name"],
|
||||
count=topic["count"],
|
||||
))
|
||||
|
||||
primary_topic_items = list(years[year]["primary_topics"].items())
|
||||
primary_topic_items.sort(key=lambda item: item[1], reverse=True)
|
||||
primary_topic_items = list(years[year]["primary_topics"].values())
|
||||
primary_topic_items.sort(key=lambda item: item["count"], reverse=True)
|
||||
|
||||
primary_topics = []
|
||||
|
||||
for topic_name, count in primary_topic_items[:max_topics_per_year]:
|
||||
for topic in primary_topic_items[:max_topics_per_year]:
|
||||
primary_topics.append(TopicCount(
|
||||
name=topic_name,
|
||||
count=count,
|
||||
id=topic["id"],
|
||||
name=topic["name"],
|
||||
count=topic["count"],
|
||||
))
|
||||
|
||||
elements.append(TopicsByYear(
|
||||
|
||||
Reference in New Issue
Block a user