#!/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.
#       --eodstamps--
##      \file
#
#
#       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 <a href='url/big/htm_or_image'> to the img info under 'a' and change the .js to put a <a> tag around the image
#
#

import  glob
import  json
import  os
import  re
import  sys

import  tzlib



icon_re = re.compile(r"""<SMALL><A\s+HREF='([^']*?)all_icons.htm'>All\s+Icons</A></SMALL>""",                                                                               re.DOTALL + re.IGNORECASE)
img_re  = re.compile(r"""<IMG\s+SRC="([^"]+)"\s+ALT="([^"]+)"\s+WIDTH="(\d+)"\s+HEIGHT="(\d+)"(?:\s+style="border-style:\s+none"\s+BORDER="0")?\s*></A>(?:</TD>|<BR>)""",   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 >>sys.stderr, "Tell at least 1 .htm file and one output .js file."
        print >>sys.stderr, sys.argv
        sys.exit(101)

    json_file_name  = sys.argv.pop()
    if  json_file_name.startswith('-') or sys.argv[-1].startswith('-') :
        print >>sys.stderr, "There's a problem with the last command line arguments - dashs start them!"
        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             = 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

