forked from magicbadger/python-hybridspi
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxml_to_binary
More file actions
executable file
·203 lines (172 loc) · 7.72 KB
/
xml_to_binary
File metadata and controls
executable file
·203 lines (172 loc) · 7.72 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python
"""
Feed in an SI file URL and an ensemble and it will generate a directory named with the ensemble ID
including a manifest, an encoded SI file covering only services on that Ensemble, and PI files for
each service for the next 5 days
"""
import argparse
import os, sys
from datetime import datetime, timedelta
import urllib2
import logging
from json import dumps, JSONEncoder
import random
import string
import re
parser = argparse.ArgumentParser(description='Encode an SI url into binary encoded representation')
parser.add_argument('f', nargs=1, help='SI document to encode from')
parser.add_argument('-e', dest='ensemble', required=True, nargs=1, help='ensemble to encode')
parser.add_argument('-o', dest='output', nargs=1, default='data', help='output directory')
parser.add_argument('-X', dest='debug', action='store_true', help='turn debug on')
parser.add_argument('-d', dest='days', nargs=1, type=int, default=0, help='number of days ahead to encode schedule files')
args = parser.parse_args()
if args.debug:
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger('xml_to_binary')
logging.getLogger('spi.binary').setLevel(logging.INFO)
# make sure we have an output directory
d = args.output
if not os.path.exists(d):
logger.debug('making output directory: %s', d)
os.makedirs(d)
# read the SI
if 'http' in args.f:
f = urllib2.urlopen(args.f[0])
else:
f = open(args.f[0])
from spi import DabBearer, IpBearer, Time
from spi.xml import unmarshall
from spi.binary import marshall, Ensemble
unmarshalled = unmarshall(f.read())
# parse ensemble
p = re.compile('(.{2})\.(.{4})')
m = p.match(args.ensemble[0])
if not m:
logger.error('ensemble cannot be parsed: %s', args.ensemble[0])
sys.exit(1)
ecc, eid = map(lambda n: int(n ,16), [m.group(1), m.group(2)])
logger.debug('encoding xml for ensemble: %02x.%04x', ecc, eid)
def ensemble_filter(service):
for bearer in service.bearers:
if isinstance(bearer, DabBearer) and bearer.ecc == ecc and bearer.eid == eid:
return True
return False
def bearer_filter(service):
service.bearers = [x for x in service.bearers if isinstance(x, (DabBearer, IpBearer))]
return service
# filter on selected services from the desired ensemble and DAB or IP bearers only
unmarshalled.services = map(bearer_filter, unmarshalled.services)
unmarshalled.services = filter(ensemble_filter, unmarshalled.services)
# list of tuples: (filename, params, function, args, kwargs)
jobs = []
def get_filename():
return ''.join(random.choice(string.ascii_lowercase) for i in range(6))
def encode_image(filename, media):
logger.debug('encoding image from %s to local file %s', media.url, filename)
response = urllib2.urlopen(media.url)
data = response.read()
logger.debug('read %d bytes of image data', len(data))
return data
# encode service logos
for service in unmarshalled.services:
from spi import Multimedia
for media in filter(lambda m:
m.type in (Multimedia.LOGO_COLOUR_SQUARE, Multimedia.LOGO_COLOUR_RECTANGLE) or
(m.content in ('image/png', 'image/jpg', 'image/jpeg') and
(m.width, m.height) in ((32, 32), (112, 32), (128, 128))),
service.media):
filename = get_filename()
jobs.append((filename,
{"contentname" : media.url,
"type" : "2/3" if media.content == 'image/png' or media.type in (Multimedia.LOGO_COLOUR_SQUARE, Multimedia.LOGO_COLOUR_RECTANGLE) else "2/1"},
encode_image, [filename, media], {}))
# encode the SI file
jobs.append((get_filename(),
{"contentname" : "si.xml",
"scopeid" : "%02x.%04x" % (ecc, eid),
"type" : "7/0"},
marshall, [unmarshalled],
{"ensemble" : Ensemble(ecc=ecc, eid=eid)}))
def calculate_scope(schedule): # this should probably be in the main codebase
start, end = schedule.scope
if start is not None and end is not None: return (start, end)
if start is None:
for programme in schedule.programmes:
for location in programme.locations:
for time in location.times:
if not isinstance(time, Time): continue
if start is None or time.actual_time < start: start = time.actual_time
if end is None or (time.actual_time + time.actual_duration) > end: end = (time.actual_time + time.actual_duration)
return (start, end)
if args.days > 0:
for service in unmarshalled.services:
from urlparse import urlparse
url = urlparse(service.lookup)
fqdn = url.hostname
serviceIdentifier = url.path[1:]
try:
import srvlookup
logger.debug('looking for radioepg SRV record on domain: %s', fqdn)
srvs = srvlookup.lookup('radioepg', domain=fqdn)
srv = srvs[0]
logger.debug('found SRV record for domain %s : %s', fqdn, srv)
except Exception, e:
print >> sys.stderr, 'failure to find SRV record for %s: %s' % (fqdn, e)
continue
# get the service bearer on this ensemble
bearer = filter(lambda b: isinstance(b, DabBearer) and b.ecc == ecc and b.eid == eid, service.bearers)[0]
now = datetime.today()
# for each service, encode 5 days worth of PI
for i in range(0, 5):
day = now + timedelta(days=i)
url = 'http://%s/radiodns/spi/3.1/id/%s/%s/%s' % (srv.host, fqdn, serviceIdentifier, day.strftime("%Y%m%d_PI.xml"))
filename = '%s_PI_%04x' % (day.strftime("%Y%m%d"), bearer.sid)
logger.debug('making request for PI file to: %s', url)
f = urllib2.urlopen(url)
data = f.read()
logger.debug('read %d bytes', len(data))
programmeinfo = unmarshall(data)
# get the right scope - not entirely clear from the specification how multiple schedules should be handled
# in terms of scope signalling
scope_start = None
scope_end = None
for schedule in programmeinfo.schedules:
start, end = schedule.scope
if scope_start == None or start < scope_start: scope_start = start
if scope_end == None or end > scope_end: scope_start = start
start, end = calculate_scope(schedule)
if scope_start == None or start < scope_start: scope_start = start
if scope_end == None or end > scope_end: scope_end = end
jobs.append((filename, {"contentname" : filename,
"scopeid" : "%02x.%04x.%04x.%x" % (bearer.ecc, bearer.eid, bearer.sid, bearer.scids),
"scopestart" : scope_start,
"scopeend" : scope_end
}, marshall, [programmeinfo], {}))
# now perform the jobs
manifest = {"entries": []}
for job in jobs:
try:
filename, params, func, args, kwargs = job
logger.debug('opening output file at: %s', os.path.join(d, filename))
f = open(os.path.join(d, filename), 'wb')
logger.debug('creating data from function: %s, args=%s, kwargs=%s', func, args, kwargs)
r = func(*args, **kwargs)
f.write(r)
f.close()
entry = {"path": filename}
entry.update(params)
manifest['entries'].append(entry)
except:
logger.error('error running job: %s', job)
# write the manifest
class Encoder(JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime):
return obj.isoformat()
return str(obj)
f = open(os.path.join(d, "manifest.json"), "w")
f.write(dumps(manifest, cls=Encoder, indent=4))
f.close()
#from spi.binary import unmarshall
#print unmarshall(binary)
#print binary