Convert SMIL file to JSON

0

In this program we manage a .smil file and we want to convert it to Json during its execution. The program works as it should in all methods but it shows the same output as when I was printing the .smil file on the terminal, so I think that my to_json method should not be doing its function well.

import sys
import json
from xml.sax import make_parser
from smallsmilhandler import SmallSMILHandler
from urllib.request import urlretrieve


class KaraokeLocal():

    def __init__(self, fich):
        parser = make_parser()
        cHandler = SmallSMILHandler()
        parser.setContentHandler(cHandler)
        parser.parse(open(file_smil))
        self.my_file = cHandler.get_tags()

    def __str__(self):
        archivo_smil = []
        for linea in self.my_file:
            lista_final = []
            etiqueta = linea[0]
            lista_final.append(etiqueta)
            for atributo, valor in linea[1].items():
                lista_final.append (atributo + "=" + '"' + valor + '"')   
            lista_final = '\t'.join(lista_final)
            archivo_smil.append(lista_final + '\n')
        print(archivo_smil)

    def to_json(self,fjson):
        with open(file_json, "w"):
            json.dumps(self.my_file)

    def do_local(self):
        for linea in self.my_file:
                for atributo, valor in linea[1].items():
                    if valor[:7] == "http://":
                        name_local = valor.split('/')[-1]
                        urlretrieve(valor, name_local)
                        print("Descargando %s..." % valor)    

if __name__ == "__main__":

    try:
        file_smil = sys.argv[1]
        file_json = sys.argv[1].replace(".smil", ".json")
        file_karaoke = KaraokeLocal(file_smil)
        file_karaoke.__str__()
        file_karaoke.to_json(file_json)
        file_karaoke.do_local()
        file_karaoke.to_json('.json')                
        file_karaoke.__str__()



    except IndexError:

        sys.exit("Usage:python3 karaoke.py file.smil")

I add the output I get in the terminal:

['root-layout\twidth="248"\theight="300"\tbackground-color="blue"\n',     'region\tid="a"\ttop="20"\tbottom=""\tleft="64"\tright=""\n', 'region\tid="b"\ttop="120"\tbottom=""\tleft="20"\tright=""\n', 'region\tid="text_area"\ttop="100"\tbottom=""\tleft="20"\tright=""\n', 'img\tsrc="http://www.content-networking.com/smil/hello.jpg"\tregion="a"\tbegin="2s"\tdur="36s"\n', 'img\tsrc="http://www.content-networking.com/smil/earthrise.jpg"\tregion="b"\tbegin="12s"\tdur=""\n', 'audio\tsrc="http://www.content-networking.com/smil/hello.wav"\tbegin="1s"\tdur=""\n', 'textstream\tsrc="http://gsyc.es/~grex/letra.rt"\tregion="text_area"\n', 'audio\tsrc="cancion.ogg"\tbegin="4s"\tdur=""\n']


Descargando http://www.content-networking.com/smil/hello.jpg...
Descargando http://www.content-networking.com/smil/earthrise.jpg...
Descargando http://www.content-networking.com/smil/hello.wav...
Descargando http://gsyc.es/~grex/letra.rt...


['root-layout\twidth="248"\theight="300"\tbackground-color="blue"\n', 'region\tid="a"\ttop="20"\tbottom=""\tleft="64"\tright=""\n', 'region\tid="b"\ttop="120"\tbottom=""\tleft="20"\tright=""\n', 'region\tid="text_area"\ttop="100"\tbottom=""\tleft="20"\tright=""\n', 'img\tsrc="http://www.content-networking.com/smil/hello.jpg"\tregion="a"\tbegin="2s"\tdur="36s"\n', 'img\tsrc="http://www.content-networking.com/smil/earthrise.jpg"\tregion="b"\tbegin="12s"\tdur=""\n', 'audio\tsrc="http://www.content-networking.com/smil/hello.wav"\tbegin="1s"\tdur=""\n', 'textstream\tsrc="http://gsyc.es/~grex/letra.rt"\tregion="text_area"\n', 'audio\tsrc="cancion.ogg"\tbegin="4s"\tdur=""\n']
    
asked by Victor Correa Ruiz 16.10.2018 в 13:51
source

0 answers