Commit 6dbb530d authored by njean42's avatar njean42
Browse files

Kumiko can now parse a list of image URLs.

--output now also tells --html to write the webpage, and to which file.
parent 28e87377
#!/usr/bin/env python
import sys, os, json, argparse, subprocess
import sys, os, json, argparse, subprocess, hashlib
from kumikolib import Kumiko
from lib.html import HTML
......@@ -13,13 +13,14 @@ parser.add_argument('--debug-dir', nargs=1, help='Write debug files in this dire
parser.add_argument('--progress', action='store_true', help='Prints progress information')
# Input/Output
parser.add_argument('-i', '--input', nargs=1, required=True, help='A file or folder name to parse')
parser.add_argument('-o', '--output', nargs=1, help='A file name to save json output to')
parser.add_argument('-i', '--input', nargs='+', required=True, help='A file or folder name to parse')
parser.add_argument('-o', '--output', nargs=1, help='A file name to save json/html output to')
# HTML reader page options
parser.add_argument('-b', '--browser', nargs=1, help='Opens given browser to view the result when ready (implies --html)', choices=['firefox','konqueror','chromium'])
parser.add_argument('--html', action='store_true', help='Generates an HTML file showing the result')
parser.add_argument('--html-debug', action='store_true', help='Shows debug information on resulting HTML page')
parser.add_argument('-b', '--browser', nargs=1, help='Opens given browser to read your comic page(s) panel-by-panel! (implies --html)', choices=['firefox','konqueror','chromium'])
parser.add_argument('--html', action='store_true', help='Generates an HTML file for reading')
parser.add_argument('--html-debug', action='store_true', help='Shows debug information on resulting HTML page (implies --html)')
parser.add_argument('--html-static-dir', nargs=1, help='When generating HTML, this will be the relative directory for javascript files: <script src="$static-dir/..."> (implies --html)')
# Configuration tweaks
parser.add_argument('--min-panel-size-ratio', nargs=1, type=float, help='Panels will be considered too small and exluded if they have a width < img.width * ratio or height < img/height * ratio (default is 1/15th)')
......@@ -34,38 +35,65 @@ k = Kumiko({
})
file_or_folder = args.input[0]
if file_or_folder[-1] == '/':
file_or_folder = file_or_folder[0:-1]
folder = file_or_folder
if os.path.isdir(file_or_folder):
info = k.parse_dir(file_or_folder)
elif os.path.isfile(file_or_folder):
info = k.parse_images([file_or_folder])
folder = os.path.dirname(file_or_folder)
folder = None
html_file = None
# Folder
if len(args.input) == 1 and os.path.isdir(args.input[0]):
folder = args.input[0]
if folder[-1] == '/':
folder = folder[0:-1]
info = k.parse_dir(folder)
html_file = os.path.join('tests/results',os.path.basename(folder)+'.html')
# File
elif len(args.input) == 1 and os.path.isfile(args.input[0]):
f = args.input[0]
folder = os.path.dirname(f)
info = k.parse_images([f])
html_file = os.path.join('tests/results',os.path.basename(f)+'.html')
# URL list
else:
print("--input (-i) is not a file or directory: '"+file_or_folder+"'")
info = k.parse_url_list(args.input)
filehash = hashlib.sha1(';'.join(sorted(args.input)).encode()).hexdigest()
html_file = os.path.join('tests/results',filehash+'.html')
if len(info) == 0:
print("--input (-i) is not a file, or directory, or URL list: '"+str(args.input)+"'")
sys.exit(1)
info = json.dumps(info)
if args.output:
f = open(args.output[0], 'w')
f.write(info)
f.close()
# Generate HTML
if args.html or args.browser or args.html_debug or args.html_static_dir:
images_dir = os.path.relpath(folder,'tests/results')+'/' if folder else 'urls'
html = ''
html += HTML.header(reldir = args.html_static_dir[0] if args.html_static_dir else '../../')
html += HTML.reader(info,images_dir,args.html_debug)
html += HTML.footer
if args.output:
html_file = args.output[0]
if args.browser or args.html_debug or args.output:
fh = open(html_file, 'w')
fh.write(html)
fh.close()
print('Saved HTML file:',html_file)
else:
print(html)
# Or JSON info
else:
print(info)
# generate html file for viewing the result
if args.html or args.browser:
html_file = os.path.join('tests/results',os.path.basename(file_or_folder)+'.html')
fh = open(html_file, 'w')
fh.write(HTML.header(reldir='../../'))
fh.write(HTML.reader(info,os.path.relpath(folder,'tests/results')+'/',args.html_debug))
fh.write(HTML.footer)
fh.close()
print('Generated HTML file:',html_file)
if args.output:
f = open(args.output[0], 'w')
f.write(info)
f.close()
else:
print(info)
if args.browser:
......
#!/usr/bin/env python
import os, json, sys
import os, json, sys, tempfile, requests
import cv2 as cv
import numpy as np
from urllib.parse import urlparse
from lib.panel import Panel
......@@ -24,28 +25,45 @@ class Kumiko:
self.options['min_panel_size_ratio'] = options['min_panel_size_ratio']
def parse_dir(self,directory):
def parse_url_list(self,urls):
tempdir = tempfile.TemporaryDirectory()
i = 0
for url in urls:
i += 1
parts = urlparse(url)
if not parts.netloc or not parts.path:
continue
r = requests.get(url)
with open(os.path.join(tempdir.name,'img'+str(i)), 'wb') as f:
f.write(r.content)
return self.parse_dir(tempdir.name,urls=urls)
def parse_dir(self,directory,urls=None):
filenames = []
for root, dirs, files in os.walk(directory):
for filename in files:
filenames.append(os.path.join(root,filename))
filenames.sort()
return self.parse_images(filenames)
for filename in os.scandir(directory):
filenames.append(filename.path)
return self.parse_images(filenames,urls)
def parse_images(self,filenames=[]):
def parse_images(self,filenames=[],urls=None):
infos = []
if self.options['progress']:
print(len(filenames),'files')
i = -1
for filename in filenames:
i += 1
if self.options['progress']:
print("\t",filename)
print("\t",urls[i] if urls else filename)
try:
infos.append(self.parse_image(filename))
infos.append(self.parse_image(filename,url=urls[i] if urls else None))
except Exception:
print("Not an image, will be ignored: {}".format(filename), file=sys.stderr)
pass # this file is not an image, will not be part of the results
return infos
......@@ -181,7 +199,7 @@ class Kumiko:
return sum(size) / 2 / 20
def parse_image(self,filename):
def parse_image(self,filename,url=None):
img = cv.imread(filename)
if not isinstance(img,np.ndarray) or img.size == 0:
raise Exception('File {} is not an image'.format(filename))
......@@ -190,7 +208,7 @@ class Kumiko:
size.reverse() # get a [width,height] list
infos = {
'filename': os.path.basename(filename),
'filename': url if url else os.path.basename(filename),
'size': size,
'panels': []
}
......
......@@ -17,7 +17,7 @@ class Reader {
console.error('no images_dir given in options');
return;
}
this.images_dir = options.images_dir
this.images_dir = options.images_dir;
if (!options.comicsJson || typeof options.comicsJson != 'object') {
console.error('no comicsJson given in options, or not a javascript object');
......@@ -78,7 +78,7 @@ class Reader {
$('.pagenb',this.gui).html('page '+(page+1)+' <small>/'+this.comic.length+'</small>')
var imginfo = this.comic[page];
var imgurl = this.images_dir + imginfo.filename.split('/').reverse()[0];
var imgurl = this.images_dir == 'urls' ? imginfo.filename : this.images_dir + imginfo.filename.split('/').reverse()[0];
var img = $('<img class="pageimg" src="'+imgurl+'"/>');
img.css({
......@@ -327,6 +327,9 @@ $(document).delegate( '.kumiko-reader', 'dblclick', function (e) {
/**** KEYBOARD NAVIGATION ****/
$(document).keydown(function(e) {
if (e.altKey) // alt+left is shortcut for previous page, don't prevent it
return;
switch(e.which) {
case 37: // left
reader.prev();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment