Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

40 rindas
1.3 KiB

  1. import logging
  2. from collections import OrderedDict
  3. from services.dates import date_part
  4. from structs.headline import Headline
  5. logger = logging.getLogger(__name__)
  6. class Story:
  7. """A cluster of headlines, drawn from one or more sources, that all report
  8. the same underlying news story."""
  9. def __init__(self, headlines: list[Headline]):
  10. self.headlines = list(headlines)
  11. logger.debug("Created Story with %d headline(s)", len(self.headlines))
  12. @property
  13. def sources(self) -> list[str]:
  14. """Distinct outlet domains for this story, in first-seen order."""
  15. seen = OrderedDict()
  16. for h in self.headlines:
  17. if h.domain:
  18. seen.setdefault(h.domain, None)
  19. return list(seen.keys())
  20. @property
  21. def source_count(self) -> int:
  22. return len(self.sources)
  23. @property
  24. def representative(self) -> str:
  25. """The most descriptive headline text for this story (longest, first on tie)."""
  26. return max(self.headlines, key=lambda h: len(h.display_text)).display_text
  27. @property
  28. def latest_date(self):
  29. """The most recent publication date among this story's headlines, or None."""
  30. dates = [date_part(h.published_at) for h in self.headlines if h.published_at is not None]
  31. return max(dates) if dates else None