#!/usr/bin/python

# lyrics_montage.py
#       --copyright--                   Copyright 2007 (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--
#       March 19, 2005          bar
#       November 18, 2007       bar     turn on doxygen
#       November 27, 2007       bar     insert boilerplate copyright
#       May 17, 2008            bar     email adr
#       May 27, 2012            bar     doxygen namespace
#       --eodstamps--
##      \file
#       \namespace              tzpython.lyrics_montage
#
#
#       Convert lyrics text to a Google image montage.
#
#

import  os.path

import  net_lyrics
import  TextToGoogleImages


__ALL__ = [
            'make_lyrics_montage_file',
          ]


def make_lyrics_montage_file(lyrics_file_name, safe = True, rnd_image = False) :
    """
        Make montage file from a lyrics file if the montage file doesn't exist.
        If all goes well, the file will be saved as a .jpg by the same name as the lyrics file.
          And, the image will be returned instead of None.
    """

    ifn = os.path.splitext(lyrics_file_name)[0] + ".jpg"

    if  not os.path.exists(ifn) :

        lg  = net_lyrics.a_lyrics_from_file(lyrics_file_name)

        if  lg :
            urls    = TextToGoogleImages.convert_text_to_google_image_urls(string.join( [ lg.title, lg.artist, lg.album, lg.lyrics ] ), safe, rnd_image)
            io      = TextToGoogleImages.make_montage_image_from_urls(urls)
            io.save(ifn)

            return(io)
        pass

    return(False)





if  __name__ == '__main__' :

    import  glob
    import  string
    import  sys

    import  TZCommandLineAtFile
    import  tzlib


    help_str = """
python lyrics_montage (options) ambiguous_file(s)...

Convert lyrics text to Google image montages.

--safe                  Use 'safe' search.
--random                Use random image if there is a choice.

"""


    del(sys.argv[0])

    TZCommandLineAtFile.expand_at_sign_command_line_files(sys.argv)

    if  tzlib.array_find(sys.argv, "--help") >= 0 :
        print help_str
        sys.exit(254)
    if  tzlib.array_find(sys.argv, "-h") >= 0 :
        print help_str
        sys.exit(254)
    if  tzlib.array_find(sys.argv, "-?") >= 0 :
        print help_str
        sys.exit(254)


    safe                = False
    do_rand             = False

    while True :
        oi  = tzlib.array_find(sys.argv, "--safe")
        if  oi < 0 :    break
        del sys.argv[oi]
        safe            = True

    while True :
        oi  = tzlib.array_find(sys.argv, "--random")
        if  oi < 0 :    break
        del sys.argv[oi]
        do_rand         = True

    while len(sys.argv) > 0 :

        files   = glob.glob(sys.argv.pop(0))

        for fn in files :
            if  make_lyrics_montage_file(fn, safe, do_rand) == None :
                print "No lyrics made for", fn
            pass
        pass

    pass


#
#
#
# eof
