2019-04-17 07:49:18 +00:00
|
|
|
__package__ = 'archivebox.core'
|
|
|
|
|
|
|
|
import uuid
|
|
|
|
|
2020-10-12 18:47:03 +00:00
|
|
|
from django.db import models, transaction
|
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
|
2020-09-21 16:50:26 +00:00
|
|
|
|
2019-04-27 21:26:24 +00:00
|
|
|
from ..util import parse_date
|
|
|
|
from ..index.schema import Link
|
2020-11-04 12:28:02 +00:00
|
|
|
from ..extractors import get_default_archive_methods
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
name = models.CharField(verbose_name="name", unique=True, blank=False, max_length=100)
|
|
|
|
slug = models.SlugField(verbose_name="slug", unique=True, max_length=100)
|
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)
|
|
|
|
|
|
|
|
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
|
|
|
|
else:
|
|
|
|
return super().save(*args, **kwargs)
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
url = models.URLField(unique=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
|
|
|
|
2020-07-28 03:26:45 +00:00
|
|
|
title = models.CharField(max_length=128, 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)
|
2020-07-28 03:26:45 +00:00
|
|
|
updated = models.DateTimeField(null=True, blank=True, db_index=True)
|
2020-10-12 18:47:03 +00:00
|
|
|
tags = models.ManyToManyField(Tag)
|
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-09-21 18:57: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-09-21 18:57:38 +00:00
|
|
|
|
2020-10-31 11:55:27 +00:00
|
|
|
def tags_str(self) -> str:
|
|
|
|
return ','.join(self.tags.order_by('name').values_list('name', flat=True))
|
2020-08-28 16:08:03 +00:00
|
|
|
|
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)
|
|
|
|
|
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):
|
|
|
|
return self.as_link().num_outputs
|
|
|
|
|
2020-06-30 10:41:48 +00:00
|
|
|
@cached_property
|
2019-04-22 17:20:19 +00:00
|
|
|
def url_hash(self):
|
|
|
|
return self.as_link().url_hash
|
|
|
|
|
2020-06-30 10:41:48 +00:00
|
|
|
@cached_property
|
2019-04-22 17:20:19 +00:00
|
|
|
def base_url(self):
|
|
|
|
return self.as_link().base_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):
|
|
|
|
return self.as_link().link_dir
|
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):
|
|
|
|
return self.as_link().archive_path
|
|
|
|
|
2020-06-30 10:41:48 +00:00
|
|
|
@cached_property
|
2020-06-26 03:32:01 +00:00
|
|
|
def archive_size(self):
|
|
|
|
return self.as_link().archive_size
|
2020-06-30 10:41:48 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def history(self):
|
|
|
|
from ..index import load_link_details
|
|
|
|
return load_link_details(self.as_link()).history
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def latest_title(self):
|
|
|
|
if ('title' in self.history
|
|
|
|
and self.history['title']
|
|
|
|
and (self.history['title'][-1].status == 'succeeded')
|
|
|
|
and self.history['title'][-1].output.strip()):
|
|
|
|
return self.history['title'][-1].output.strip()
|
|
|
|
return None
|
2020-10-12 18:47:03 +00:00
|
|
|
|
|
|
|
def save_tags(self, tags=[]):
|
|
|
|
tags_id = []
|
|
|
|
for tag in tags:
|
|
|
|
tags_id.append(Tag.objects.get_or_create(name=tag)[0].id)
|
|
|
|
self.tags.clear()
|
|
|
|
self.tags.add(*tags_id)
|
2020-11-03 14:54:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ArchiveResult(models.Model):
|
|
|
|
snapshot = models.ForeignKey(Snapshot, on_delete=models.CASCADE)
|
2020-11-23 23:28:43 +00:00
|
|
|
cmd = models.CharField(max_length=500)
|
|
|
|
pwd = models.CharField(max_length=200)
|
|
|
|
cmd_version = models.CharField(max_length=32)
|
|
|
|
output = models.CharField(max_length=500)
|
2020-11-03 14:54:02 +00:00
|
|
|
start_ts = models.DateTimeField()
|
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)
|
|
|
|
extractor = models.CharField(choices=EXTRACTORS, blank=False, max_length=32)
|
2020-11-04 15:31:20 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.extractor
|