2020-07-28 03:26:45 +00:00
|
|
|
__package__ = 'archivebox.core'
|
|
|
|
|
|
|
|
from io import StringIO
|
|
|
|
from contextlib import redirect_stdout
|
2020-07-28 09:59:54 +00:00
|
|
|
from pathlib import Path
|
2020-07-28 03:26:45 +00:00
|
|
|
|
2019-04-02 20:36:41 +00:00
|
|
|
from django.contrib import admin
|
2020-07-28 03:26:45 +00:00
|
|
|
from django.urls import path
|
2020-06-30 09:56:17 +00:00
|
|
|
from django.utils.html import format_html
|
2020-07-28 09:59:54 +00:00
|
|
|
from django.utils.safestring import mark_safe
|
2020-07-28 03:56:35 +00:00
|
|
|
from django.shortcuts import render, redirect
|
2020-07-28 03:26:45 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
2019-04-02 20:36:41 +00:00
|
|
|
|
2019-05-01 03:44:51 +00:00
|
|
|
from core.models import Snapshot
|
2020-07-28 03:26:45 +00:00
|
|
|
from core.forms import AddLinkForm
|
|
|
|
|
2020-07-28 10:50:03 +00:00
|
|
|
from util import htmldecode, urldecode, ansi_to_html
|
|
|
|
from logging_util import printable_filesize
|
|
|
|
from main import add, remove
|
|
|
|
from config import OUTPUT_DIR
|
|
|
|
from extractors import archive_links
|
2020-06-30 09:56:17 +00:00
|
|
|
|
|
|
|
# TODO: https://stackoverflow.com/questions/40760880/add-custom-button-to-django-admin-panel
|
2019-04-27 21:26:24 +00:00
|
|
|
|
2020-07-28 09:59:54 +00:00
|
|
|
def update_snapshots(modeladmin, request, queryset):
|
|
|
|
archive_links([
|
|
|
|
snapshot.as_link()
|
|
|
|
for snapshot in queryset
|
|
|
|
], out_dir=OUTPUT_DIR)
|
|
|
|
update_snapshots.short_description = "Archive"
|
|
|
|
|
|
|
|
def update_titles(modeladmin, request, queryset):
|
|
|
|
archive_links([
|
|
|
|
snapshot.as_link()
|
|
|
|
for snapshot in queryset
|
|
|
|
], overwrite=True, methods=('title',), out_dir=OUTPUT_DIR)
|
|
|
|
update_titles.short_description = "Pull title"
|
|
|
|
|
|
|
|
def overwrite_snapshots(modeladmin, request, queryset):
|
|
|
|
archive_links([
|
|
|
|
snapshot.as_link()
|
|
|
|
for snapshot in queryset
|
|
|
|
], overwrite=True, out_dir=OUTPUT_DIR)
|
|
|
|
overwrite_snapshots.short_description = "Re-archive (overwrite)"
|
|
|
|
|
|
|
|
def verify_snapshots(modeladmin, request, queryset):
|
|
|
|
for snapshot in queryset:
|
|
|
|
print(snapshot.timestamp, snapshot.url, snapshot.is_archived, snapshot.archive_size, len(snapshot.history))
|
|
|
|
|
|
|
|
verify_snapshots.short_description = "Check"
|
|
|
|
|
|
|
|
def delete_snapshots(modeladmin, request, queryset):
|
|
|
|
remove(links=[snapshot.as_link() for snapshot in queryset], yes=True, delete=True, out_dir=OUTPUT_DIR)
|
|
|
|
|
|
|
|
delete_snapshots.short_description = "Delete"
|
|
|
|
|
2019-04-22 17:20:19 +00:00
|
|
|
|
2019-05-01 03:44:51 +00:00
|
|
|
class SnapshotAdmin(admin.ModelAdmin):
|
2020-07-30 03:54:26 +00:00
|
|
|
list_display = ('added', 'title_str', 'url_str', 'files', 'size')
|
|
|
|
sort_fields = ('title_str', 'url_str', 'added')
|
2020-07-28 03:26:45 +00:00
|
|
|
readonly_fields = ('id', 'url', 'timestamp', 'num_outputs', 'is_archived', 'url_hash', 'added', 'updated')
|
2020-06-30 09:56:17 +00:00
|
|
|
search_fields = ('url', 'timestamp', 'title', 'tags')
|
2020-07-28 03:26:45 +00:00
|
|
|
fields = ('title', 'tags', *readonly_fields)
|
2020-06-30 09:56:17 +00:00
|
|
|
list_filter = ('added', 'updated', 'tags')
|
|
|
|
ordering = ['-added']
|
2020-07-28 09:59:54 +00:00
|
|
|
actions = [delete_snapshots, overwrite_snapshots, update_snapshots, update_titles, verify_snapshots]
|
|
|
|
actions_template = 'admin/actions_as_select.html'
|
2020-06-30 09:56:17 +00:00
|
|
|
|
|
|
|
def id_str(self, obj):
|
|
|
|
return format_html(
|
|
|
|
'<code style="font-size: 10px">{}</code>',
|
|
|
|
obj.url_hash[:8],
|
|
|
|
)
|
|
|
|
|
|
|
|
def title_str(self, obj):
|
|
|
|
canon = obj.as_link().canonical_outputs()
|
2020-07-28 09:59:54 +00:00
|
|
|
tags = ''.join(
|
|
|
|
format_html('<span>{}</span>', tag.strip())
|
|
|
|
for tag in obj.tags.split(',')
|
|
|
|
) if obj.tags else ''
|
2020-06-30 09:56:17 +00:00
|
|
|
return format_html(
|
|
|
|
'<a href="/{}">'
|
2020-07-28 03:26:45 +00:00
|
|
|
'<img src="/{}/{}" class="favicon" onerror="this.remove()">'
|
2020-06-30 09:56:17 +00:00
|
|
|
'</a>'
|
|
|
|
'<a href="/{}/{}">'
|
2020-07-28 03:26:45 +00:00
|
|
|
'<b class="status-{}">{}</b>'
|
|
|
|
'</a>',
|
2020-06-30 09:56:17 +00:00
|
|
|
obj.archive_path,
|
2020-07-27 23:29:57 +00:00
|
|
|
obj.archive_path, canon['favicon_path'],
|
2020-06-30 09:56:17 +00:00
|
|
|
obj.archive_path, canon['wget_path'] or '',
|
2020-07-28 03:26:45 +00:00
|
|
|
'fetched' if obj.latest_title or obj.title else 'pending',
|
2020-07-28 09:59:54 +00:00
|
|
|
urldecode(htmldecode(obj.latest_title or obj.title or ''))[:128] or 'Pending...'
|
|
|
|
) + mark_safe(f'<span class="tags">{tags}</span>')
|
2020-06-30 09:56:17 +00:00
|
|
|
|
|
|
|
def files(self, obj):
|
2020-07-28 09:59:54 +00:00
|
|
|
link = obj.as_link()
|
|
|
|
canon = link.canonical_outputs()
|
|
|
|
out_dir = Path(link.link_dir)
|
|
|
|
|
|
|
|
link_tuple = lambda link, method: (link.archive_path, canon[method], canon[method] and (out_dir / canon[method]).exists())
|
|
|
|
|
2020-06-30 09:56:17 +00:00
|
|
|
return format_html(
|
2020-07-28 09:59:54 +00:00
|
|
|
'<span class="files-icons" style="font-size: 1.2em; opacity: 0.8">'
|
|
|
|
'<a href="/{}/{}/" class="exists-{}" title="Wget clone">🌐 </a> '
|
|
|
|
'<a href="/{}/{}" class="exists-{}" title="PDF">📄</a> '
|
|
|
|
'<a href="/{}/{}" class="exists-{}" title="Screenshot">🖥 </a> '
|
|
|
|
'<a href="/{}/{}" class="exists-{}" title="HTML dump">🅷 </a> '
|
|
|
|
'<a href="/{}/{}/" class="exists-{}" title="WARC">🆆 </a> '
|
|
|
|
'<a href="/{}/{}/" class="exists-{}" title="Media files">📼 </a> '
|
|
|
|
'<a href="/{}/{}/" class="exists-{}" title="Git repos">📦 </a> '
|
|
|
|
'<a href="{}" class="exists-{}" title="Archive.org snapshot">🏛 </a> '
|
2020-07-27 23:29:57 +00:00
|
|
|
'</span>',
|
2020-07-28 09:59:54 +00:00
|
|
|
*link_tuple(link, 'wget_path'),
|
|
|
|
*link_tuple(link, 'pdf_path'),
|
|
|
|
*link_tuple(link, 'screenshot_path'),
|
|
|
|
*link_tuple(link, 'dom_path'),
|
|
|
|
*link_tuple(link, 'warc_path')[:2], any((out_dir / canon['warc_path']).glob('*.warc.gz')),
|
|
|
|
*link_tuple(link, 'media_path')[:2], any((out_dir / canon['media_path']).glob('*')),
|
|
|
|
*link_tuple(link, 'git_path')[:2], any((out_dir / canon['git_path']).glob('*')),
|
|
|
|
canon['archive_org_path'], (out_dir / 'archive.org.txt').exists(),
|
2020-07-27 23:29:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def size(self, obj):
|
|
|
|
return format_html(
|
|
|
|
'<a href="/{}" title="View all files">{}</a>',
|
2020-06-30 09:56:17 +00:00
|
|
|
obj.archive_path,
|
2020-06-30 10:41:48 +00:00
|
|
|
printable_filesize(obj.archive_size) if obj.archive_size else 'pending',
|
2020-06-30 09:56:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def url_str(self, obj):
|
|
|
|
return format_html(
|
2020-07-28 03:26:45 +00:00
|
|
|
'<a href="{}">{}</a>',
|
2020-06-30 09:56:17 +00:00
|
|
|
obj.url,
|
2020-06-30 10:41:48 +00:00
|
|
|
obj.url.split('://www.', 1)[-1].split('://', 1)[-1][:64],
|
2020-06-30 09:56:17 +00:00
|
|
|
)
|
2019-04-22 17:20:19 +00:00
|
|
|
|
2020-06-30 09:56:17 +00:00
|
|
|
id_str.short_description = 'ID'
|
|
|
|
title_str.short_description = 'Title'
|
2020-07-28 03:26:45 +00:00
|
|
|
url_str.short_description = 'Original URL'
|
2019-04-22 17:20:19 +00:00
|
|
|
|
2020-06-30 09:56:17 +00:00
|
|
|
id_str.admin_order_field = 'id'
|
|
|
|
title_str.admin_order_field = 'title'
|
|
|
|
url_str.admin_order_field = 'url'
|
2019-04-23 01:40:42 +00:00
|
|
|
|
2020-07-28 03:26:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ArchiveBoxAdmin(admin.AdminSite):
|
|
|
|
site_header = 'ArchiveBox'
|
|
|
|
index_title = 'Links'
|
|
|
|
site_title = 'Index'
|
|
|
|
|
|
|
|
def get_urls(self):
|
|
|
|
return [
|
2020-07-28 03:56:35 +00:00
|
|
|
path('core/snapshot/add/', self.add_view, name='Add'),
|
2020-07-28 03:26:45 +00:00
|
|
|
] + super().get_urls()
|
|
|
|
|
|
|
|
def add_view(self, request):
|
2020-07-28 03:56:35 +00:00
|
|
|
if not request.user.is_authenticated:
|
|
|
|
return redirect(f'/admin/login/?next={request.path}')
|
|
|
|
|
2020-07-28 03:26:45 +00:00
|
|
|
request.current_app = self.name
|
|
|
|
context = {
|
|
|
|
**self.each_context(request),
|
|
|
|
'title': 'Add URLs',
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.method == 'GET':
|
|
|
|
context['form'] = AddLinkForm()
|
|
|
|
|
|
|
|
elif request.method == 'POST':
|
|
|
|
form = AddLinkForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
url = form.cleaned_data["url"]
|
|
|
|
print(f'[+] Adding URL: {url}')
|
|
|
|
depth = 0 if form.cleaned_data["depth"] == "0" else 1
|
|
|
|
input_kwargs = {
|
|
|
|
"urls": url,
|
|
|
|
"depth": depth,
|
|
|
|
"update_all": False,
|
|
|
|
"out_dir": OUTPUT_DIR,
|
|
|
|
}
|
|
|
|
add_stdout = StringIO()
|
|
|
|
with redirect_stdout(add_stdout):
|
|
|
|
add(**input_kwargs)
|
|
|
|
print(add_stdout.getvalue())
|
|
|
|
|
|
|
|
context.update({
|
|
|
|
"stdout": ansi_to_html(add_stdout.getvalue().strip()),
|
|
|
|
"form": AddLinkForm()
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
context["form"] = form
|
|
|
|
|
|
|
|
return render(template_name='add_links.html', request=request, context=context)
|
|
|
|
|
|
|
|
|
|
|
|
admin.site = ArchiveBoxAdmin()
|
|
|
|
admin.site.register(get_user_model())
|
2019-05-01 03:44:51 +00:00
|
|
|
admin.site.register(Snapshot, SnapshotAdmin)
|
2020-07-28 09:51:02 +00:00
|
|
|
admin.site.disable_action('delete_selected')
|