#!/usr/bin/python
# make_slide_show_json.py
# --copyright-- Copyright 2012 (C) Tranzoa, Co. All rights reserved. Warranty: You're free and on your own here. This code is not necessarily up-to-date or of public quality.
# --url-- http://www.tranzoa.net/tzpython/
# --email-- pycode is the name to send to. tranzoa.com is the place to send to.
# --bodstamps--
# January 30, 2012 bar
# February 3, 2012 bar get the old dirs going with --url, etc.
# May 27, 2012 bar doxygen namespace
# March 5, 2023 bar future print
# April 13, 2023 bar listize keys values items
# --eodstamps--
## \file
# \namespace tzpython.make_slide_show_json
#
#
# Create a .js json file that contains URLs to and information about the images from a thumbnail_htm.py output file.
#
# TODO:
# --big add /big/ to the url and look up the hite/width in the .htm files if they are there - otherwise use PIL.
# --a big add to the img info under 'a' and change the .js to put a tag around the image
#
#
from __future__ import print_function
import glob
import json
import os
import re
import sys
import tzlib
icon_re = re.compile(r"""All\s+Icons""", re.DOTALL + re.IGNORECASE)
img_re = re.compile(r"""(?:|
)""", re.DOTALL + re.IGNORECASE)
def make_image_dict(fn, base_url, width, hite, alt) :
me = {}
me['fn'] = fn
me['url'] = base_url.rstrip("/") + "/" + os.path.basename(fn).replace("\\", "/")
me['width'] = int(width)
me['hite'] = int(hite)
me['alt'] = alt
me['when'] = os.path.getmtime(fn)
me['fsize'] = os.path.getsize(fn)
return(me)
def get_images_from_htm(htm_file_name, url = None) :
fd = tzlib.read_whole_text_file(htm_file_name)
if not url :
g = icon_re.search(fd)
if not g :
raise ValueError("No URL in htm file: %s!" % htm_file_name)
url = g.group(1)
imgs = img_re.findall(fd)
if not len(imgs) :
raise ValueError("No images in htm file: %s!" % htm_file_name)
a = []
for img in imgs :
a.append(make_image_dict(os.path.join(os.path.dirname(htm_file_name), img[0]), url, img[2], img[3], img[1]))
return(a)
if __name__ == '__main__' :
import TZCommandLineAtFile
program_name = sys.argv.pop(0)
json_name = "image_information"
help_str = program_name + """ (options) htm_file_and_options... output_json_file
Finds all the images in the htm_file(s) and creates a json file containing
URLs and information about each of the images.
The information includes the height, width, date/time, file size of each image.
Options:
--json_name name Name the json array (Default: %s).
--url url Set the base URL for subsequent htm_files and directories.
(Overrides URL in htm_file.)
""" % ( json_name, )
TZCommandLineAtFile.expand_at_sign_command_line_files(sys.argv)
if tzlib.array_find(sys.argv, [ "--help", "-h", "-H", "-?", "/h", "/H", "/?" ] ) >= 0 :
print(help_str)
sys.exit(254)
while True :
oi = tzlib.array_find(sys.argv, [ "--json_name", "--jsonname", "--json-name", "-j" ] )
if oi < 0 : break
del sys.argv[oi]
json_name = sys.argv.pop(oi)
if not len(sys.argv) != 1 :
print("Tell at least 1 .htm file and one output .js file.", file = sys.stderr)
print(sys.argv, file = sys.stderr)
sys.exit(101)
json_file_name = sys.argv.pop()
if json_file_name.startswith('-') or sys.argv[-1].startswith('-') :
print("There's a problem with the last command line arguments - dashs start them!", file = sys.stderr)
sys.exit(102)
imgs = []
args = list(sys.argv)
url = None
while len(args) :
fn = args.pop(0)
if fn in [ '--base_url', '--base-url', '--baseurl', '--url', '-u' ] :
url = args.pop(0)
else :
if os.path.isdir(fn) :
args = glob.glob(os.path.join(fn, "*.htm")) + args
else :
a = get_images_from_htm(fn, url = url)
imgs += a
pass
pass
imgd = {}
for img in imgs :
imgd[img['url']] = img # eliminate duplicated image URLs
kys = list(imgd.keys())
kys.sort(lambda a, b : cmp(a.lower(), b.lower()))
imgs = [ imgd[k] for k in kys ] # sort 'em by URL
ejson = ";\n"
if json_name :
bjson = "%s = " % json_name
else :
bjson = ""
ejson = ""
tzlib.write_whole_text_file(json_file_name, bjson + json.dumps(imgs, sort_keys = True, indent = 4) + ejson)
# print("@@@@", len(imgs))
#
#
#
# eof