"""This file sort many url from :
    - arguments
    - and/or a file (-i file) (in the file, it must have one url per line).
"""

# Copyright (c) 2001 LOGILAB S.A. (Paris, FRANCE).
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


if __name__ == '__main__':
    import sys, getopt
    import source_vector
    import sort_vectors_mc
    import myhtmllib

    args = sys.argv[1:]
    
    
    optlist, args = getopt.getopt(args, 'i:')
    input = None
    for attr, value in optlist:
        if attr == '-i':
            input = value
    
    if input:
        try:
            f = open(input, 'r')
        except IOError, msg:
            print input, ":", msg
            sys.exit(1)

        tmp = f.readlines()
        for i in range(len(tmp)):
            if tmp[i][-1] == '\n':
                args.append(tmp[i][:-1])
            else:
                args.append(tmp[i])
        
        if f is not sys.stdin:
            f.close()
    
    if len(args) < 3:
        print 'Command: [-i bookmark-file] [url1 url2 url3 ...]'
        print 'The bookmark-file must contain one URL per line.'
        sys.exit(1)
    
    all_vector = []
    i=0
    for url in args:
        # Transform source to vector
        temp_source_vect = []
        print '--'*35
        print url

        temp_source_vect.append(url)

        # image filter
        import re
        ext_list = re.split('[.*]', url[-5:])
        end = len(ext_list)-1
        if ext_list[end] == 'jpg' or \
           ext_list[end] == 'png' or \
           ext_list[end] == 'gif':
            print 'image'
            continue

        
        try:
            data = myhtmllib.parse_html_url(url, debug=0)
        except:
            print 'Error, Pass this url.'
            continue
        
        tsv = source_vector.tranf_source_vector()
        
        try:
            temp_source_vect.append(tsv.do(data, debug=0, only_dico=0))
        except ReadError:
            continue
        
        all_vector.append(temp_source_vect)
        print i
        print tsv.list_repr_vect
        i = i+1
    
    # Sort vectors
    svt = sort_vectors_mc.sort_vectors_mc(all_vector, 5, debug=0)
    the_tree = svt.do()
    
    print '\n'
    print the_tree
    
    for i in range(len(all_vector)):
        print i, ': ', all_vector[i][0]

