|
1 from lxml import etree |
|
2 from optparse import OptionParser #@UnresolvedImport |
|
3 |
|
4 def get_options(): |
|
5 |
|
6 parser = OptionParser() |
|
7 |
|
8 parser.add_option("-f", "--file", dest="outputfile", |
|
9 help="destination filename", metavar="FILE", default="twitter_export_conf.xml") |
|
10 parser.add_option("-i", "--input", dest="inputfile", |
|
11 help="inputfile", metavar="INPUT", default=None) |
|
12 |
|
13 return parser.parse_args() |
|
14 |
|
15 if __name__ == "__main__": |
|
16 (options, args) = get_options() |
|
17 |
|
18 dest_filename = options.outputfile |
|
19 |
|
20 path_list = [] |
|
21 if options.inputfile is None: |
|
22 path_list = args |
|
23 else: |
|
24 with open(options.inputfile, 'r') as fi: |
|
25 path_list = fi |
|
26 |
|
27 |
|
28 root = etree.Element("twitter_export") |
|
29 |
|
30 |
|
31 for path in path_list: |
|
32 |
|
33 iri_doc = etree.parse(path) |
|
34 media_nodes = iri_doc.xpath("/iri/body/medias/media[@id='video']/video") |
|
35 duration = int(media_nodes[0].get("dur"))/1000 |
|
36 |
|
37 file_elem = etree.SubElement(root, "file") |
|
38 etree.SubElement(file_elem, "path").text = path |
|
39 etree.SubElement(file_elem, "start_date") |
|
40 etree.SubElement(file_elem, "duration").text = unicode(duration) |
|
41 |
|
42 tree = etree.ElementTree(root) |
|
43 tree.write(dest_filename, encoding="utf-8", pretty_print=True, xml_declaration=True) |