2019-04-17 07:49:18 +00:00
|
|
|
__package__ = 'archivebox.core'
|
|
|
|
|
|
|
|
import uuid
|
|
|
|
|
2021-03-27 05:50:26 +00:00
|
|
|
from django.db import models
|
2020-06-30 10:41:48 +00:00
|
|
|
from django.utils.functional import cached_property
|
2020-10-12 18:47:03 +00:00
|
|
|
from django.utils.text import slugify
|
2021-02-16 20:49:29 +00:00
|
|
|
from django.core.cache import cache
|
2020-11-23 18:04:38 +00:00
|
|
|
from django.db.models import Case, When, Value, IntegerField
|
2021-04-06 01:14:22 +00:00
|
|
|
from django.contrib.auth.models import User # noqa
|
2020-09-21 16:50:26 +00:00
|
|
|
|
2021-02-16 20:49:29 +00:00
|
|
|
from ..config import ARCHIVE_DIR, ARCHIVE_DIR_NAME
|
|
|
|
from ..system import get_dir_size
|
2021-02-16 01:44:08 +00:00
|
|
|
from ..util import parse_date, base_url, hashurl
|
2019-04-27 21:26:24 +00:00
|
|
|
from ..index.schema import Link
|
2021-04-01 06:22:15 +00:00
|
|
|
from ..index.html import snapshot_icons
|
2020-11-23 18:04:38 +00:00
|
|
|
from ..extractors import get_default_archive_methods, ARCHIVE_METHODS_INDEXING_PRECEDENCE
|
2020-11-04 12:28:02 +00:00
|
|
|
|
|
|
|
EXTRACTORS = [(extractor[0], extractor[0]) for extractor in get_default_archive_methods()]
|
2020-11-23 23:28:43 +00:00
|
|
|
STATUS_CHOICES = [
|
|
|
|
("succeeded", "succeeded"),
|
|
|
|
("failed", "failed"),
|
|
|
|
("skipped", "skipped")
|
|
|
|
]
|
2019-04-22 17:20:19 +00:00
|
|
|
|
2020-12-11 18:45:44 +00:00
|
|
|
try:
|
|
|
|
JSONField = models.JSONField
|
|
|
|
except AttributeError:
|
|
|
|
import jsonfield
|
|
|
|
JSONField = jsonfield.JSONField
|
|
|
|
|
2019-04-17 07:49:18 +00:00
|
|
|
|
2020-10-12 18:47:03 +00:00
|
|
|
class Tag(models.Model):
|
|
|
|
"""
|
|
|
|
Based on django-taggit model
|
|
|
|
"""
|
2021-03-01 06:39:06 +00:00
|
|
|
name = models.CharField(unique=True, blank=False, max_length=100)
|
2021-03-27 07:57:05 +00:00
|
|
|
|
|
|
|
# slug is autoset on save from name, never set it manually
|
2021-03-01 06:39:06 +00:00
|
|
|
slug = models.SlugField(unique=True, blank=True, max_length=100)
|
2020-09-21 16:50:26 +00:00
|
|
|
|
2021-03-27 07:57:05 +00:00
|
|
|
|
2020-09-21 16:50:26 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = "Tag"
|
|
|
|
verbose_name_plural = "Tags"
|
|
|
|
|
2020-10-12 18:47:03 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
def slugify(self, tag, i=None):
|
|
|
|
slug = slugify(tag)
|
|
|
|
if i is not None:
|
|
|
|
slug += "_%d" % i
|
|
|
|
return slug
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if self._state.adding and not self.slug:
|
|
|
|
self.slug = self.slugify(self.name)
|
|
|
|
|
2021-03-01 03:54:04 +00:00
|
|
|
# if name is different but slug conficts with another tags slug, append a counter
|
|
|
|
# with transaction.atomic():
|
|
|
|
slugs = set(
|
|
|
|
type(self)
|
|
|
|
._default_manager.filter(slug__startswith=self.slug)
|
|
|
|
.values_list("slug", flat=True)
|
|
|
|
)
|
|
|
|
|
|
|
|
i = None
|
|
|
|
while True:
|
|
|
|
slug = self.slugify(self.name, i)
|
|
|
|
if slug not in slugs:
|
|
|
|
self.slug = slug
|
|
|
|
return super().save(*args, **kwargs)
|
|
|
|
i = 1 if i is None else i+1
|
2020-10-12 18:47:03 +00:00
|
|
|
else:
|
|
|
|
return super().save(*args, **kwargs)
|
|
|
|
|
2020-12-06 17:23:02 +00:00
|
|
|
|
2019-05-01 03:44:51 +00:00
|
|
|
class Snapshot(models.Model):
|
2019-04-17 07:49:18 +00:00
|
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
|
|
|
|
2021-04-01 07:31:20 +00:00
|
|
|
url = models.URLField(unique=True, db_index=True)
|
2020-07-13 16:21:52 +00:00
|
|
|
timestamp = models.CharField(max_length=32, unique=True, db_index=True)
|
2019-04-17 07:49:18 +00:00
|
|
|
|
2021-02-18 07:33:08 +00:00
|
|
|
title = models.CharField(max_length=512, null=True, blank=True, db_index=True)
|
2019-04-17 07:49:18 +00:00
|
|
|
|
2020-06-26 03:32:01 +00:00
|
|
|
added = models.DateTimeField(auto_now_add=True, db_index=True)
|
2021-02-16 20:54:27 +00:00
|
|
|
updated = models.DateTimeField(auto_now=True, blank=True, null=True, db_index=True)
|
2021-02-18 13:04:14 +00:00
|
|
|
tags = models.ManyToManyField(Tag, blank=True)
|
2019-04-17 07:49:18 +00:00
|
|
|
|
2019-04-17 07:50:41 +00:00
|
|
|
keys = ('url', 'timestamp', 'title', 'tags', 'updated')
|
2019-04-17 07:49:18 +00:00
|
|
|
|
2019-04-22 17:20:19 +00:00
|
|
|
def __repr__(self) -> str:
|
2020-06-30 09:55:34 +00:00
|
|
|
title = self.title or '-'
|
|
|
|
return f'[{self.timestamp}] {self.url[:64]} ({title[:64]})'
|
2019-04-22 17:20:19 +00:00
|
|
|
|
|
|
|
def __str__(self) -> str:
|
2020-06-30 09:55:34 +00:00
|
|
|
title = self.title or '-'
|
|
|
|
return f'[{self.timestamp}] {self.url[:64]} ({title[:64]})'
|
2019-04-22 17:20:19 +00:00
|
|
|
|
2019-04-17 07:49:18 +00:00
|
|
|
@classmethod
|
|
|
|
def from_json(cls, info: dict):
|
2019-04-17 07:50:41 +00:00
|
|
|
info = {k: v for k, v in info.items() if k in cls.keys}
|
2019-04-17 07:49:18 +00:00
|
|
|
return cls(**info)
|
|
|
|
|
|
|
|
def as_json(self, *args) -> dict:
|
2019-04-17 07:50:41 +00:00
|
|
|
args = args or self.keys
|
2019-04-17 07:49:18 +00:00
|
|
|
return {
|
|
|
|
key: getattr(self, key)
|
2020-10-31 11:55:27 +00:00
|
|
|
if key != 'tags' else self.tags_str()
|
2020-11-23 18:04:38 +00:00
|
|
|
for key in args
|
2019-04-17 07:49:18 +00:00
|
|
|
}
|
2019-04-22 17:20:19 +00:00
|
|
|
|
|
|
|
def as_link(self) -> Link:
|
|
|
|
return Link.from_json(self.as_json())
|
|
|
|
|
2020-08-28 16:08:03 +00:00
|
|
|
def as_link_with_details(self) -> Link:
|
|
|
|
from ..index import load_link_details
|
|
|
|
return load_link_details(self.as_link())
|
2020-11-23 18:04:38 +00:00
|
|
|
|
2021-03-27 08:30:15 +00:00
|
|
|
def tags_str(self, nocache=True) -> str:
|
2021-02-16 20:49:29 +00:00
|
|
|
cache_key = f'{self.id}-{(self.updated or self.added).timestamp()}-tags'
|
|
|
|
calc_tags_str = lambda: ','.join(self.tags.order_by('name').values_list('name', flat=True))
|
2021-03-27 08:30:15 +00:00
|
|
|
if nocache:
|
|
|
|
tags_str = calc_tags_str()
|
|
|
|
cache.set(cache_key, tags_str)
|
|
|
|
return tags_str
|
2021-02-16 20:49:29 +00:00
|
|
|
return cache.get_or_set(cache_key, calc_tags_str)
|
2020-08-28 16:08:03 +00:00
|
|
|
|
2021-04-01 06:22:15 +00:00
|
|
|
def icons(self) -> str:
|
|
|
|
return snapshot_icons(self)
|
|
|
|
|
2020-06-30 10:41:48 +00:00
|
|
|
@cached_property
|
2019-04-23 01:40:42 +00:00
|
|
|
def bookmarked(self):
|
|
|
|
return parse_date(self.timestamp)
|
|
|
|
|
2021-02-16 01:44:08 +00:00
|
|
|
@cached_property
|
|
|
|
def bookmarked_date(self):
|
|
|
|
# TODO: remove this
|
|
|
|
return self.bookmarked
|
|
|
|
|
2020-06-30 10:41:48 +00:00
|
|
|
@cached_property
|
2019-04-22 17:20:19 +00:00
|
|
|
def is_archived(self):
|
|
|
|
return self.as_link().is_archived
|
|
|
|
|
2020-06-30 10:41:48 +00:00
|
|
|
@cached_property
|
2019-04-22 17:20:19 +00:00
|
|
|
def num_outputs(self):
|
2020-11-28 06:05:53 +00:00
|
|
|
return self.archiveresult_set.filter(status='succeeded').count()
|
2019-04-22 17:20:19 +00:00
|
|
|
|
2020-06-30 10:41:48 +00:00
|
|
|
@cached_property
|
2019-04-22 17:20:19 +00:00
|
|
|
def url_hash(self):
|
2021-02-16 01:44:08 +00:00
|
|
|
return hashurl(self.url)
|
2019-04-22 17:20:19 +00:00
|
|
|
|
2020-06-30 10:41:48 +00:00
|
|
|
@cached_property
|
2019-04-22 17:20:19 +00:00
|
|
|
def base_url(self):
|
2021-02-16 01:44:08 +00:00
|
|
|
return base_url(self.url)
|
2019-05-01 03:13:04 +00:00
|
|
|
|
2020-06-30 10:41:48 +00:00
|
|
|
@cached_property
|
2019-05-01 03:13:04 +00:00
|
|
|
def link_dir(self):
|
2021-02-16 01:44:08 +00:00
|
|
|
return str(ARCHIVE_DIR / self.timestamp)
|
2020-06-26 03:32:01 +00:00
|
|
|
|
2020-06-30 10:41:48 +00:00
|
|
|
@cached_property
|
2020-06-26 03:32:01 +00:00
|
|
|
def archive_path(self):
|
2021-02-16 11:16:02 +00:00
|
|
|
return '{}/{}'.format(ARCHIVE_DIR_NAME, self.timestamp)
|
2020-06-26 03:32:01 +00:00
|
|
|
|
2020-06-30 10:41:48 +00:00
|
|
|
@cached_property
|
2020-06-26 03:32:01 +00:00
|
|
|
def archive_size(self):
|
2021-02-16 20:49:29 +00:00
|
|
|
cache_key = f'{str(self.id)[:12]}-{(self.updated or self.added).timestamp()}-size'
|
|
|
|
|
|
|
|
def calc_dir_size():
|
|
|
|
try:
|
|
|
|
return get_dir_size(self.link_dir)[0]
|
|
|
|
except Exception:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
return cache.get_or_set(cache_key, calc_dir_size)
|
2020-06-30 10:41:48 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def history(self):
|
2020-11-28 06:05:53 +00:00
|
|
|
# TODO: use ArchiveResult for this instead of json
|
2020-11-28 06:14:45 +00:00
|
|
|
return self.as_link_with_details().history
|
2020-06-30 10:41:48 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def latest_title(self):
|
2021-02-16 20:49:53 +00:00
|
|
|
if self.title:
|
2021-02-18 07:33:28 +00:00
|
|
|
return self.title # whoopdedoo that was easy
|
2021-02-16 20:49:53 +00:00
|
|
|
|
|
|
|
try:
|
2021-02-18 07:33:28 +00:00
|
|
|
# take longest successful title from ArchiveResult db history
|
|
|
|
return sorted(
|
|
|
|
self.archiveresult_set\
|
|
|
|
.filter(extractor='title', status='succeeded', output__isnull=False)\
|
|
|
|
.values_list('output', flat=True),
|
|
|
|
key=lambda r: len(r),
|
|
|
|
)[-1]
|
|
|
|
except IndexError:
|
2021-02-16 20:49:53 +00:00
|
|
|
pass
|
|
|
|
|
2021-02-18 07:33:28 +00:00
|
|
|
try:
|
|
|
|
# take longest successful title from Link json index file history
|
|
|
|
return sorted(
|
|
|
|
(
|
|
|
|
result.output.strip()
|
|
|
|
for result in self.history['title']
|
|
|
|
if result.status == 'succeeded' and result.output.strip()
|
|
|
|
),
|
|
|
|
key=lambda r: len(r),
|
|
|
|
)[-1]
|
|
|
|
except (KeyError, IndexError):
|
|
|
|
pass
|
2021-02-16 20:49:53 +00:00
|
|
|
|
2020-06-30 10:41:48 +00:00
|
|
|
return None
|
2020-10-12 18:47:03 +00:00
|
|
|
|
2020-11-28 06:06:11 +00:00
|
|
|
def save_tags(self, tags=()):
|
2020-10-12 18:47:03 +00:00
|
|
|
tags_id = []
|
|
|
|
for tag in tags:
|
2021-02-18 07:33:42 +00:00
|
|
|
if tag.strip():
|
|
|
|
tags_id.append(Tag.objects.get_or_create(name=tag)[0].id)
|
2020-10-12 18:47:03 +00:00
|
|
|
self.tags.clear()
|
|
|
|
self.tags.add(*tags_id)
|
2020-11-03 14:54:02 +00:00
|
|
|
|
2020-12-06 17:23:02 +00:00
|
|
|
|
2020-11-23 18:04:38 +00:00
|
|
|
class ArchiveResultManager(models.Manager):
|
|
|
|
def indexable(self, sorted: bool = True):
|
|
|
|
INDEXABLE_METHODS = [ r[0] for r in ARCHIVE_METHODS_INDEXING_PRECEDENCE ]
|
|
|
|
qs = self.get_queryset().filter(extractor__in=INDEXABLE_METHODS,status='succeeded')
|
2020-11-03 14:54:02 +00:00
|
|
|
|
2020-11-23 18:04:38 +00:00
|
|
|
if sorted:
|
|
|
|
precedence = [ When(extractor=method, then=Value(precedence)) for method, precedence in ARCHIVE_METHODS_INDEXING_PRECEDENCE ]
|
|
|
|
qs = qs.annotate(indexing_precedence=Case(*precedence, default=Value(1000),output_field=IntegerField())).order_by('indexing_precedence')
|
|
|
|
return qs
|
2020-12-06 17:23:02 +00:00
|
|
|
|
|
|
|
|
2020-11-03 14:54:02 +00:00
|
|
|
class ArchiveResult(models.Model):
|
2021-02-16 20:54:27 +00:00
|
|
|
id = models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')
|
|
|
|
uuid = models.UUIDField(default=uuid.uuid4, editable=False)
|
|
|
|
|
2020-11-03 14:54:02 +00:00
|
|
|
snapshot = models.ForeignKey(Snapshot, on_delete=models.CASCADE)
|
2021-02-17 23:25:01 +00:00
|
|
|
extractor = models.CharField(choices=EXTRACTORS, max_length=32)
|
2020-12-11 18:45:44 +00:00
|
|
|
cmd = JSONField()
|
2020-11-27 05:01:34 +00:00
|
|
|
pwd = models.CharField(max_length=256)
|
2021-02-16 20:54:27 +00:00
|
|
|
cmd_version = models.CharField(max_length=128, default=None, null=True, blank=True)
|
|
|
|
output = models.CharField(max_length=1024)
|
|
|
|
start_ts = models.DateTimeField(db_index=True)
|
2020-11-04 12:28:02 +00:00
|
|
|
end_ts = models.DateTimeField()
|
2020-11-23 23:28:43 +00:00
|
|
|
status = models.CharField(max_length=16, choices=STATUS_CHOICES)
|
2020-11-04 15:31:20 +00:00
|
|
|
|
2020-11-23 18:04:38 +00:00
|
|
|
objects = ArchiveResultManager()
|
|
|
|
|
2020-11-04 15:31:20 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.extractor
|