<commit_before>#!/usr/bin/python3
# -*- coding: utf-8 -*-

from xml.sax import make_parser
from xml.sax.handler import ContentHandler
import smallsmilhandler
import sys
import json
import urllib


def listado_ordenado(tags):
    listado = ""
    for diccs in tags:
        atrib_line = ""
        for key in diccs.keys():
            if key != "name" and diccs[key] != "":
                atrib_line = atrib_line + key + "=" + diccs[key] + "\t"
        listado = listado + diccs["name"] + "\t" + atrib_line + "\n"
    return listado

def to_json(fichero):
    json_listado = json.dumps(fichero)
    #print(json_listado)
    with open('/home/garijos/Escritorio/PTAVI/ptavi-p3/karaoke.json', 'w') as outfile:
        json.dump(json_listado, outfile)

def url_local(tags):
    list_url = []
    for diccs in tags:
        for key in diccs.keys():
            if key == "src":
                url = diccs[key]
                list_url = url.split("/")
                remoto = list_url[0]
                if remoto == "http:":
                    arch_web = urllib.request.urlopen(url)
                    filename = list_url[-1]
                    print(filename)
                    f = open(filename, "wb")
                    f.write(arch_web.read())
                else:
                    filename = diccs[key]
                    print(filename)

if __name__ == "__main__":

    fich = sys.argv[1]
    try:
        fichero = open(fich)
    except IndexError:
        sys.exit("Usage: python3 karaoke.py file.smil.")

    parser = make_parser()
    ssHandler = smallsmilhandler.SmallSMILHandler()
    parser.setContentHandler(ssHandler)
    parser.parse(fichero)

    tags = ssHandler.get_tags()
    result = listado_ordenado(tags)
    print(result)

    #to_json(fichero)
    url_local(tags)<commit_msg>Mejorado para un Diccionario<commit_after>#!/usr/bin/python3
# -*- coding: utf-8 -*-

from xml.sax import make_parser
from xml.sax.handler import ContentHandler
import smallsmilhandler
import sys
import json
import urllib

def init_parse(fichero):
    parser = make_parser()
    ssHandler = smallsmilhandler.SmallSMILHandler()
    parser.setContentHandler(ssHandler)
    parser.parse(fichero)
    tags = ssHandler.get_tags()
    return tags

def listado_ordenado(tags):
    listado = ""
    for etiqueta in tags:
        listado = listado + etiqueta[0]
        atribs = etiqueta[1].items()
        for atrib, contenido in atribs:
            if contenido != "":
                listado = listado + "\t" + atrib + "=" + '"' + contenido + '"'
        listado = listado + "\n"
    return listado

def to_json(tags):
    with open('karaoke.json', 'w') as outfile_json:
        json.dump(tags, outfile_json, sort_keys=True, indent=3, separators=(' ', ': '))

def url_local(tags):
    list_url = []
    for etiqueta in tags:
        atribs = etiqueta[1].items()
        for atrib, contenido in atribs:
            if atrib == "src":
                url = contenido
                list_url = url.split("/")
                remoto = list_url[0]
                if remoto == "http:":
                    arch_web = urllib.request.urlopen(url)
                    filename = list_url[-1]
                    print("Descargando... " + filename)
                    f = open(filename, "wb")
                    f.write(arch_web.read())
                else:
                    filename = contenido
                    print("Este contenido ya está en local... " + filename)

if __name__ == "__main__":

    fich = sys.argv[1]
    try:
        fichero = open(fich)
    except IndexError:
        sys.exit("Usage: python3 karaoke.py file.smil.")

    tags = init_parse(fichero)
    result = listado_ordenado(tags)
    print(result)
    to_json(tags)
    url_local(tags)

{'fraction_non_alphanumeric': 0.11839660207061321, 'fraction_numerical': 0.004512874966817096, 'mean_word_length': 1.9857369255150554, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3037504', 'n_tokens_mistral': 1238, 'n_tokens_neox': 1208, 'n_words': 265}