feat: Refactor outlets with multilingual support and dynamic queries
- Replace static articles array with dynamic source_keyword queries
- Use MongoDB _id as unique identifier for outlets
- Add multilingual translations (9 languages: ko, en, zh_cn, zh_tw, ja, fr, de, es, it)
- Add OutletService for database operations
- Add outlet migration script with Korean source_keyword matching
- Remove JSON file-based outlet loading
- Add /outlets/{outlet_id}/articles endpoint for dynamic article retrieval
This resolves the design issues with:
1. Static articles array requiring constant updates
2. Lack of multilingual support for outlet names/descriptions
3. Broken image URLs
4. Korean entity matching for article queries
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@ -2,25 +2,14 @@ from fastapi import APIRouter, HTTPException, Query
|
||||
from typing import Optional
|
||||
from app.services.article_service import ArticleService
|
||||
from app.services.comment_service import CommentService
|
||||
from app.services.outlet_service import OutletService
|
||||
from app.models.article import ArticleList, Article, ArticleSummary
|
||||
from app.models.comment import Comment, CommentCreate, CommentList
|
||||
from app.models.outlet import Outlet
|
||||
from typing import List
|
||||
import json
|
||||
import os
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
# Load outlets data
|
||||
OUTLETS_FILE = os.path.join(os.path.dirname(__file__), '../../outlets-extracted.json')
|
||||
outlets_data = None
|
||||
|
||||
def load_outlets():
|
||||
global outlets_data
|
||||
if outlets_data is None:
|
||||
with open(OUTLETS_FILE, 'r', encoding='utf-8') as f:
|
||||
outlets_data = json.load(f)
|
||||
return outlets_data
|
||||
|
||||
@router.get("/{language}/articles", response_model=ArticleList)
|
||||
async def get_articles(
|
||||
language: str,
|
||||
@ -84,28 +73,48 @@ async def get_categories(language: str):
|
||||
@router.get("/outlets")
|
||||
async def get_outlets(category: Optional[str] = Query(None, description="Filter by category: people, topics, companies")):
|
||||
"""Get outlets list - people, topics, companies"""
|
||||
data = load_outlets()
|
||||
|
||||
if category:
|
||||
if category in ['people', 'topics', 'companies']:
|
||||
return {category: data[category]}
|
||||
else:
|
||||
raise HTTPException(status_code=400, detail=f"Invalid category: {category}. Must be one of: people, topics, companies")
|
||||
# Get outlets for specific category
|
||||
outlets = await OutletService.get_all_outlets(category=category)
|
||||
return {category: outlets}
|
||||
|
||||
return data
|
||||
# Get all outlets grouped by category
|
||||
result = {}
|
||||
for cat in ['people', 'topics', 'companies']:
|
||||
outlets = await OutletService.get_all_outlets(category=cat)
|
||||
result[cat] = outlets
|
||||
|
||||
return result
|
||||
|
||||
@router.get("/outlets/{outlet_id}")
|
||||
async def get_outlet_by_id(outlet_id: str):
|
||||
"""Get specific outlet by ID"""
|
||||
data = load_outlets()
|
||||
"""Get specific outlet by ID (_id)"""
|
||||
outlet = await OutletService.get_outlet_by_id(outlet_id)
|
||||
return outlet
|
||||
|
||||
# Search in all categories
|
||||
for category in ['people', 'topics', 'companies']:
|
||||
for outlet in data[category]:
|
||||
if outlet['id'] == outlet_id:
|
||||
return outlet
|
||||
@router.get("/{language}/outlets/{outlet_id}/articles")
|
||||
async def get_outlet_articles(
|
||||
language: str,
|
||||
outlet_id: str,
|
||||
page: int = Query(1, ge=1, description="Page number"),
|
||||
page_size: int = Query(20, ge=1, le=100, description="Items per page")
|
||||
):
|
||||
"""Get articles for a specific outlet using source_keyword"""
|
||||
if not ArticleService.validate_language(language):
|
||||
raise HTTPException(status_code=400, detail=f"Unsupported language: {language}")
|
||||
|
||||
raise HTTPException(status_code=404, detail=f"Outlet not found: {outlet_id}")
|
||||
# Get outlet to retrieve source_keyword
|
||||
outlet = await OutletService.get_outlet_by_id(outlet_id)
|
||||
|
||||
# Query articles by source_keyword dynamically
|
||||
articles_result = await ArticleService.get_articles_by_source_keyword(
|
||||
language,
|
||||
outlet['source_keyword'],
|
||||
page,
|
||||
page_size
|
||||
)
|
||||
|
||||
return articles_result
|
||||
|
||||
# Comment endpoints
|
||||
@router.get("/comments", response_model=CommentList)
|
||||
|
||||
Reference in New Issue
Block a user