forked from hadleyrich/xmltv-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xmltv-join
executable file
·69 lines (51 loc) · 1.64 KB
/
xmltv-join
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/python
"""
xmltv-join by Hadley Rich <hads@nice.net.nz>
Licensed under the BSD License.
Joins two or more XMLTV files. A lot faster and less dependancies than
tv_cat but doesn't validate etc. during joining.
Call like so;
cat freeview.xml sky.xml | xmltv-join > joined-file.xml
or;
xmltv-join freeview.xml sky.xml > joined-file.xml
"""
import sys
from xml.etree import cElementTree as ElementTree
from optparse import OptionParser
VERSION = '0.1.0'
def indent(elem, level=0):
i = "\n" + level * "\t"
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + "\t"
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
indent(elem, level+1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
if __name__ == '__main__':
parser = OptionParser(version='%prog ' + str(VERSION))
(options, args) = parser.parse_args()
if sys.stdin.isatty():
data = ''
for arg in args:
data += open(arg).read()
else:
data = sys.stdin.read()
files = ['%s</tv>' % x.strip() for x in data.split('</tv>') if x.strip()]
trees = []
out = ElementTree.Element('tv')
for f in files:
trees.append(ElementTree.XML(f))
for tree in trees:
for channel in tree.findall('.//channel'):
out.append(channel)
for tree in trees:
for programme in tree.findall('.//programme'):
out.append(programme)
indent(out)
ElementTree.dump(out)