models.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. __package__ = 'archivebox.core'
  2. import uuid
  3. from django.db import models
  4. from django.utils.functional import cached_property
  5. from ..util import parse_date
  6. from ..index.schema import Link
  7. class Snapshot(models.Model):
  8. id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
  9. url = models.URLField(unique=True)
  10. timestamp = models.CharField(max_length=32, null=True, default=None, db_index=True)
  11. title = models.CharField(max_length=128, null=True, default=None, db_index=True)
  12. tags = models.CharField(max_length=256, null=True, default=None, db_index=True)
  13. added = models.DateTimeField(auto_now_add=True, db_index=True)
  14. updated = models.DateTimeField(null=True, default=None, db_index=True)
  15. # bookmarked = models.DateTimeField()
  16. keys = ('url', 'timestamp', 'title', 'tags', 'updated')
  17. def __repr__(self) -> str:
  18. title = self.title or '-'
  19. return f'[{self.timestamp}] {self.url[:64]} ({title[:64]})'
  20. def __str__(self) -> str:
  21. title = self.title or '-'
  22. return f'[{self.timestamp}] {self.url[:64]} ({title[:64]})'
  23. @classmethod
  24. def from_json(cls, info: dict):
  25. info = {k: v for k, v in info.items() if k in cls.keys}
  26. return cls(**info)
  27. def as_json(self, *args) -> dict:
  28. args = args or self.keys
  29. return {
  30. key: getattr(self, key)
  31. for key in args
  32. }
  33. def as_link(self) -> Link:
  34. return Link.from_json(self.as_json())
  35. @cached_property
  36. def bookmarked(self):
  37. return parse_date(self.timestamp)
  38. @cached_property
  39. def is_archived(self):
  40. return self.as_link().is_archived
  41. @cached_property
  42. def num_outputs(self):
  43. return self.as_link().num_outputs
  44. @cached_property
  45. def url_hash(self):
  46. return self.as_link().url_hash
  47. @cached_property
  48. def base_url(self):
  49. return self.as_link().base_url
  50. @cached_property
  51. def link_dir(self):
  52. return self.as_link().link_dir
  53. @cached_property
  54. def archive_path(self):
  55. return self.as_link().archive_path
  56. @cached_property
  57. def archive_size(self):
  58. return self.as_link().archive_size
  59. @cached_property
  60. def history(self):
  61. from ..index import load_link_details
  62. return load_link_details(self.as_link()).history
  63. @cached_property
  64. def latest_title(self):
  65. if ('title' in self.history
  66. and self.history['title']
  67. and (self.history['title'][-1].status == 'succeeded')
  68. and self.history['title'][-1].output.strip()):
  69. return self.history['title'][-1].output.strip()
  70. return None