feat: Add stdout from process to the template

This commit is contained in:
Cristian 2020-07-01 12:23:59 -05:00
parent bc1f925542
commit c971e00c9c
4 changed files with 276 additions and 208 deletions

View file

@ -133,6 +133,16 @@ DEFAULT_CLI_COLORS = {
} }
ANSI = {k: '' for k in DEFAULT_CLI_COLORS.keys()} ANSI = {k: '' for k in DEFAULT_CLI_COLORS.keys()}
COLOR_DICT = {
'00': [(0, 0, 0), (0, 0, 0)],
'31': [(255, 0, 0), (128, 0, 0)],
'32': [(0, 200, 0), (0, 128, 0)],
'33': [(255, 255, 0), (128, 128, 0)],
'34': [(0, 0, 255), (0, 0, 128)],
'35': [(255, 0, 255), (128, 0, 128)],
'36': [(0, 255, 255), (0, 128, 128)],
}
STATICFILE_EXTENSIONS = { STATICFILE_EXTENSIONS = {
# 99.999% of the time, URLs ending in these extensions are static files # 99.999% of the time, URLs ending in these extensions are static files
# that can be downloaded as-is, not html pages that need to be rendered # that can be downloaded as-is, not html pages that need to be rendered

View file

@ -8,6 +8,9 @@ from django.conf import settings
from core.models import Snapshot from core.models import Snapshot
from contextlib import redirect_stdout
from io import StringIO
from ..index import load_main_index, load_main_index_meta from ..index import load_main_index, load_main_index_meta
from ..config import ( from ..config import (
OUTPUT_DIR, OUTPUT_DIR,
@ -16,7 +19,7 @@ from ..config import (
PUBLIC_INDEX, PUBLIC_INDEX,
PUBLIC_SNAPSHOTS, PUBLIC_SNAPSHOTS,
) )
from ..util import base_url from ..util import base_url, ansi_to_html
from .. main import add from .. main import add
@ -55,12 +58,20 @@ class AddLinks(View):
def post(self, request): def post(self, request):
url = request.POST['url'] url = request.POST['url']
print(f'[+] Adding URL: {url}') print(f'[+] Adding URL: {url}')
add( add_stdout = StringIO()
import_str=url, with redirect_stdout(add_stdout):
update_all=False, extracted_links = add(
out_dir=OUTPUT_DIR, import_str=url,
) update_all=False,
return redirect('/') out_dir=OUTPUT_DIR,
)
print(add_stdout.getvalue())
context = {
"stdout": ansi_to_html(add_stdout.getvalue())
}
return render(template_name=self.template, request=request, context=context)
class LinkDetails(View): class LinkDetails(View):

View file

@ -2,208 +2,231 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<title>Archived Sites</title> <title>Archived Sites</title>
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1"> <meta
<style> charset="utf-8"
html, body { name="viewport"
width: 100%; content="width=device-width, initial-scale=1"
height: 100%; />
font-size: 18px; <style>
font-weight: 200; html,
text-align: center; body {
margin: 0px; width: 100%;
padding: 0px; height: 100%;
font-family: "Gill Sans", Helvetica, sans-serif; font-size: 18px;
} font-weight: 200;
.header-top small { text-align: center;
font-weight: 200; margin: 0px;
color: #efefef; padding: 0px;
} font-family: "Gill Sans", Helvetica, sans-serif;
}
.header-top { .header-top small {
width: 100%; font-weight: 200;
height: auto; color: #efefef;
min-height: 40px; }
margin: 0px;
text-align: center;
color: white;
font-size: calc(11px + 0.84vw);
font-weight: 200;
padding: 4px 4px;
border-bottom: 3px solid #aa1e55;
background-color: #aa1e55;
}
input[type=search] {
width: 22vw;
border-radius: 4px;
border: 1px solid #aeaeae;
padding: 3px 5px;
}
.nav > div {
min-height: 30px;
}
.header-top a {
text-decoration: none;
color: rgba(0,0,0,0.6);
}
.header-top a:hover {
text-decoration: none;
color: rgba(0,0,0,0.9);
}
.header-top .col-lg-4 {
text-align: center;
padding-top: 4px;
padding-bottom: 4px;
}
.header-archivebox img {
display: inline-block;
margin-right: 3px;
height: 30px;
margin-left: 12px;
margin-top: -4px;
margin-bottom: 2px;
}
.header-archivebox img:hover {
opacity: 0.5;
}
#table-bookmarks_length, #table-bookmarks_filter { .header-top {
padding-top: 12px; width: 100%;
opacity: 0.8; height: auto;
padding-left: 24px; min-height: 40px;
padding-right: 22px; margin: 0px;
margin-bottom: -16px; text-align: center;
} color: white;
table { font-size: calc(11px + 0.84vw);
padding: 6px; font-weight: 200;
width: 100%; padding: 4px 4px;
} border-bottom: 3px solid #aa1e55;
table thead th { background-color: #aa1e55;
font-weight: 400; }
} input[type="search"] {
table tr { width: 22vw;
height: 35px; border-radius: 4px;
} border: 1px solid #aeaeae;
tbody tr:nth-child(odd) { padding: 3px 5px;
background-color: #ffebeb !important; }
} .nav > div {
table tr td { min-height: 30px;
white-space: nowrap; }
overflow: hidden; .header-top a {
/*padding-bottom: 0.4em;*/ text-decoration: none;
/*padding-top: 0.4em;*/ color: rgba(0, 0, 0, 0.6);
padding-left: 2px; }
text-align: center; .header-top a:hover {
} text-decoration: none;
table tr td a { color: rgba(0, 0, 0, 0.9);
text-decoration: none; }
} .header-top .col-lg-4 {
table tr td img, table tr td object { text-align: center;
display: inline-block; padding-top: 4px;
margin: auto; padding-bottom: 4px;
height: 24px; }
width: 24px; .header-archivebox img {
padding: 0px; display: inline-block;
padding-right: 5px; margin-right: 3px;
vertical-align: middle; height: 30px;
margin-left: 4px; margin-left: 12px;
} margin-top: -4px;
#table-bookmarks { margin-bottom: 2px;
width: 100%; }
overflow-y: scroll; .header-archivebox img:hover {
table-layout: fixed; opacity: 0.5;
} }
.dataTables_wrapper {
background-color: #fafafa; #table-bookmarks_length,
} #table-bookmarks_filter {
table tr a span[data-archived~=False] { padding-top: 12px;
opacity: 0.4; opacity: 0.8;
} padding-left: 24px;
.files-spinner { padding-right: 22px;
height: 15px; margin-bottom: -16px;
width: auto; }
opacity: 0.5; table {
vertical-align: -2px; padding: 6px;
} width: 100%;
.in-progress { }
display: none; table thead th {
} font-weight: 400;
body[data-status~=finished] .files-spinner { }
display: none; table tr {
} height: 35px;
/*body[data-status~=running] .in-progress { }
tbody tr:nth-child(odd) {
background-color: #ffebeb !important;
}
table tr td {
white-space: nowrap;
overflow: hidden;
/*padding-bottom: 0.4em;*/
/*padding-top: 0.4em;*/
padding-left: 2px;
text-align: center;
}
table tr td a {
text-decoration: none;
}
table tr td img,
table tr td object {
display: inline-block;
margin: auto;
height: 24px;
width: 24px;
padding: 0px;
padding-right: 5px;
vertical-align: middle;
margin-left: 4px;
}
#table-bookmarks {
width: 100%;
overflow-y: scroll;
table-layout: fixed;
}
.dataTables_wrapper {
background-color: #fafafa;
}
table tr a span[data-archived~="False"] {
opacity: 0.4;
}
.files-spinner {
height: 15px;
width: auto;
opacity: 0.5;
vertical-align: -2px;
}
.in-progress {
display: none;
}
body[data-status~="finished"] .files-spinner {
display: none;
}
/*body[data-status~=running] .in-progress {
display: inline-block; display: inline-block;
}*/ }*/
tr td a.favicon img { tr td a.favicon img {
padding-left: 6px; padding-left: 6px;
padding-right: 12px; padding-right: 12px;
vertical-align: -4px; vertical-align: -4px;
} }
tr td a.title { tr td a.title {
font-size: 1.4em; font-size: 1.4em;
text-decoration:none; text-decoration: none;
color:black; color: black;
} }
tr td a.title small { tr td a.title small {
background-color: #efefef; background-color: #efefef;
border-radius: 4px; border-radius: 4px;
float:right float: right;
} }
input[type=search]::-webkit-search-cancel-button { input[type="search"]::-webkit-search-cancel-button {
-webkit-appearance: searchfield-cancel-button; -webkit-appearance: searchfield-cancel-button;
} }
.title-col { .title-col {
text-align: left; text-align: left;
} }
.title-col a { .title-col a {
color: black; color: black;
} }
</style> </style>
<link rel="stylesheet" href="{% static 'bootstrap.min.css' %}"> <link rel="stylesheet" href="{% static 'bootstrap.min.css' %}" />
<link rel="stylesheet" href="{% static 'jquery.dataTables.min.css' %}"/> <link rel="stylesheet" href="{% static 'jquery.dataTables.min.css' %}" />
<script src="{% static 'jquery.min.js' %}"></script> <script src="{% static 'jquery.min.js' %}"></script>
<script src="{% static 'jquery.dataTables.min.js' %}"></script> <script src="{% static 'jquery.dataTables.min.js' %}"></script>
<script> <script>
document.addEventListener('error', function(e) { document.addEventListener(
e.target.style.opacity = 0; "error",
}, true) function (e) {
jQuery(document).ready(function() { e.target.style.opacity = 0;
jQuery('#table-bookmarks').DataTable({ },
stateSave: true, // save state (filtered input, number of entries shown, etc) in localStorage true
dom: '<lf<t>ip>', // how to show the table and its helpers (filter, etc) in the DOM );
order: [[0, 'desc']], jQuery(document).ready(function () {
iDisplayLength: 100, jQuery("#table-bookmarks").DataTable({
}); stateSave: true, // save state (filtered input, number of entries shown, etc) in localStorage
}); dom: "<lf<t>ip>", // how to show the table and its helpers (filter, etc) in the DOM
</script> order: [[0, "desc"]],
</head> iDisplayLength: 100,
<body data-status="finished"> });
<header> });
<div class="header-top container-fluid"> </script>
<div class="row nav"> </head>
<div class="col-sm-2"> <body data-status="finished">
<a href="/" class="header-archivebox" title="Last updated: {{updated}}"> <header>
<img src="{% static 'archive.png' %}" alt="Logo"/> <div class="header-top container-fluid">
ArchiveBox: Add <div class="row nav">
</a> <div class="col-sm-2">
</div> <a
<div class="col-sm-10" style="text-align: right"> href="/"
<a href="/">Main Index</a> &nbsp; | &nbsp; class="header-archivebox"
<a href="/admin/">Admin</a> &nbsp; | &nbsp; title="Last updated: {{updated}}"
<a href="https://github.com/pirate/ArchiveBox/wiki">Docs</a> >
</div> <img src="{% static 'archive.png' %}" alt="Logo" />
</div> ArchiveBox: Add
</div> </a>
</header> </div>
<center> <div class="col-sm-10" style="text-align: right;">
<br/><br/> <a href="/">Main Index</a> &nbsp; | &nbsp;
<form action="?" method="POST">{% csrf_token %} <a href="/admin/">Admin</a> &nbsp; | &nbsp;
Add new links...<br/> <a href="https://github.com/pirate/ArchiveBox/wiki">Docs</a>
<input type="text" name="url" placeholder="URL of page or feed..."/><br/> </div>
<button role="submit">Add</button> </div>
</form> </div>
</center> </header>
<center>
</body> {{ stdout | safe }}
<br /><br />
<form action="?" method="POST">
{% csrf_token %} Add new links...<br />
<input
type="text"
name="url"
placeholder="URL of page or feed..."
/><br />
<button role="submit">Add</button>
</form>
<a href="{% url 'admin:core_snapshot_changelist' %}"
>Go back to Snapshot list</a
>
</center>
</body>
</html> </html>

View file

@ -20,6 +20,7 @@ from .config import (
CHECK_SSL_VALIDITY, CHECK_SSL_VALIDITY,
WGET_USER_AGENT, WGET_USER_AGENT,
CHROME_OPTIONS, CHROME_OPTIONS,
COLOR_DICT
) )
try: try:
@ -69,6 +70,8 @@ URL_REGEX = re.compile(
re.IGNORECASE, re.IGNORECASE,
) )
COLOR_REGEX = re.compile(r'\[(?P<arg_1>\d+)(;(?P<arg_2>\d+)(;(?P<arg_3>\d+))?)?m')
def enforce_types(func): def enforce_types(func):
""" """
@ -195,6 +198,27 @@ def chrome_args(**options) -> List[str]:
return cmd_args return cmd_args
def ansi_to_html(text):
"""
Based on: https://stackoverflow.com/questions/19212665/python-converting-ansi-color-codes-to-html
"""
TEMPLATE = '<span style="color: rgb{}"><br>'
text = text.replace('[m', '</span>')
def single_sub(match):
argsdict = match.groupdict()
if argsdict['arg_3'] is None:
if argsdict['arg_2'] is None:
bold, color = 0, argsdict['arg_1']
else:
bold, color = argsdict['arg_1'], argsdict['arg_2']
else:
bold, color = argsdict['arg_3'], argsdict['arg_2']
return TEMPLATE.format(COLOR_DICT[color][0])
return COLOR_REGEX.sub(single_sub, text)
class ExtendedEncoder(pyjson.JSONEncoder): class ExtendedEncoder(pyjson.JSONEncoder):
""" """