#!/usr/bin/env python
#
# $Source: /home/blais/repos/cvsroot/arubomu/bin/arubomu-convert-titles,v $
# $Id: arubomu-convert-titles,v 1.18 2004/02/16 01:02:57 blais Exp $
#

"""arubomu-titles-to-xml [<options>]

Converts a simple list of number-dot-name (one per line) to XML song names.

This little script can process cut-n-paste output from Amazon or Freedb web
pages.

"""

__version__ = "$Revision: 1.18 $"
__author__ = "Martin Blais <blais@furius.ca>"


import sys, re

from arubomu import common
from arubomu.album import Album, Disc, Song

from elementtree.ElementTree import ElementTree
from elementtree_pretty import write_pretty


def main():
    import optparse
    parser = optparse.OptionParser(__doc__.strip(), version=__version__)
    parser.add_option('-I', '--input-encoding', action='store',
                      default=common.xml_encoding,
                      help="Specifies the file encoding of the input file.")
    parser.add_option('-O', '--output-encoding', action='store',
                      default=common.xml_encoding,
                      help="Specifies the file encoding of the output file.")
    parser.add_option('-a', '--artist-first', action='store_true',
                      help="Place artist first.")
    opts, args = parser.parse_args()

    m1re = re.compile('\s*(\d+)\.\s*(\d+:\d\d)\s*(.*)')
    m2re = re.compile('\s*(\d+)\.(.*)')
    wsre = re.compile('^\s*$')

    a = Album()
    a.title = '(Unknown Album)'
    a.artist = '(Unknown Artist)'
    d = Disc(1)
    a.discs[1] = d

    iif = sys.stdin

    while 1:
        l = iif.readline()
        if not l:
            break
        if wsre.match(l):
            continue
        l = unicode(l.strip(), opts.input_encoding)

        no, duration, title, artist = '', '', '', ''

        mo = m1re.match(l)
        if mo:
            no, duration, title = mo.groups()

        else:
            # cut-n-paste from amazon webpage
            mo = m2re.match(l)
            if mo:
                no, title = mo.groups()
                title = title.replace('ListenMusic', '')
                title = title.replace('Listen', '')
                title = title.replace('Extrait sonore', '')
            else:
                print >> sys.stderr, 'Error parsing line: "%s"' % l
                sys.exit(1)

        if '/' in title:
            sep = '/'
        else:
            sep = ' - '

        stit = title.split(sep)
        if len(stit) > 1:
            if opts.artist_first:
                artist = stit[0]
                title = ' - '.join(stit[1:])
            else:
                title = ' - '.join(stit[0:-1])
                artist = stit[-1]

        s = Song()
        s.no = int(no)
        d.songs[no] = s
        s.title = title.strip()
        if artist:
            s.artist = artist.strip()
        if duration:
            s.duration = duration.strip()

    xel = a.toxml()
    tree = ElementTree(xel)
    write_pretty(tree, sys.stdout, encoding=opts.output_encoding)
    print

if __name__ == '__main__':
    main()
