import re from ..models.summarise_job import SummariseJob from ..models.translated_article import TranslatedArticle def first_heading(md: str) -> str | None: m = re.search(r'^#{1,2}\s+(.+)', md, re.MULTILINE) return m.group(1).strip() if m else None class ArticleService: def __init__(self, summarise_job_repository): self.summarise_job_repository = summarise_job_repository async def get_all_articles(self) -> list[TranslatedArticle]: summarise_jobs = await self.summarise_job_repository.list_all() return summarise_jobs.map(self.summarise_job_to_translated_article) def summarise_job_to_translated_article( self, summarise_job: SummariseJob, ) -> TranslatedArticle: return TranslatedArticle( id=summarise_job.id, source_lang=summarise_job.target_language, # The source language for the article is the target language of the job source_title=first_heading(summarise_job.translated_text) or "", source_text=summarise_job.translated_text, target_lang=summarise_job.source_language, # The target language for the article is the source language of the job target_title=first_heading(summarise_job.generated_text) or "", target_text=summarise_job.generated_text, )