-
Notifications
You must be signed in to change notification settings - Fork 59
/
config.py
370 lines (322 loc) · 10.9 KB
/
config.py
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import ConfigParser, os
import re
import random
import string
from ConfigParser import NoOptionError
BLACKLIST_169 = ('540', '649')
config = ConfigParser.ConfigParser()
p = os.path.dirname(__file__)
config_files = [
'/etc/pyTivo.conf',
os.path.join(p, 'pyTivo.conf'),
]
config.read(config_files)
def reset():
global config
del config
config = ConfigParser.ConfigParser()
config.read(config_file)
def getGUID():
if config.has_option('Server', 'GUID'):
return config.get('Server', 'GUID')
else:
return ''.join([random.choice(string.letters) for i in range(10)])
return guid
def getTivoUsername():
return config.get('Server', 'tivo_username')
def getTivoPassword():
return config.get('Server', 'tivo_password')
def getBeaconAddresses():
if config.has_option('Server', 'beacon'):
beacon_ips = config.get('Server', 'beacon')
else:
beacon_ips = '255.255.255.255'
return beacon_ips
def getPort():
return config.get('Server', 'Port')
def get169Setting(tsn):
if not tsn:
return True
if config.has_section('_tivo_' + tsn):
if config.has_option('_tivo_' + tsn, 'aspect169'):
try:
return config.getboolean('_tivo_' + tsn, 'aspect169')
except ValueError:
pass
if tsn[:3] in BLACKLIST_169:
return False
return True
def getShares(tsn=''):
shares = [(section, dict(config.items(section)))
for section in config.sections()
if not(section.startswith('_tivo_') or section == 'Server')]
if config.has_section('_tivo_' + tsn):
if config.has_option('_tivo_' + tsn, 'shares'):
# clean up leading and trailing spaces & make sure ref is valid
tsnshares = []
for x in config.get('_tivo_' + tsn, 'shares').split(','):
y = x.lstrip().rstrip()
if config.has_section(y):
tsnshares += [(y, dict(config.items(y)))]
if tsnshares:
shares = tsnshares
for name, data in shares:
if not data.get('auto_subshares', 'False').lower() == 'true':
continue
base_path = data['path']
try:
for item in os.listdir(base_path):
item_path = os.path.join(base_path, item)
if not os.path.isdir(item_path):
continue
new_name = name + '/' + item
new_data = dict(data)
new_data['path'] = item_path
shares.append((new_name, new_data))
except:
pass
return shares
def getDebug(ref):
if config.has_option('Server', 'debug'):
try:
return str2tuple(config.get('Server', 'debug')+',,')[ref]
except NoOptionError:
pass
return str2tuple('False,,')[ref]
def getHack83():
try:
debug = config.get('Server', 'hack83')
if debug.lower() == 'true':
return True
else:
return False
except NoOptionError:
return False
def getOptres(tsn = None):
if tsn and config.has_section('_tivo_' + tsn):
try:
return config.getboolean('_tivo_' + tsn, 'optres')
except NoOptionError, ValueError:
pass
try:
return config.getboolean('Server', 'optres')
except NoOptionError, ValueError:
return False
def getPixelAR(ref):
if config.has_option('Server', 'par'):
try:
return (True, config.getfloat('Server', 'par'))[ref]
except NoOptionError, ValueError:
pass
return (False, 1.0)[ref]
def get(section, key):
return config.get(section, key)
def getFFmpegTemplate(tsn):
if tsn and config.has_section('_tivo_' + tsn):
try:
return config.get('_tivo_' + tsn, 'ffmpeg_tmpl', raw=True)
except NoOptionError:
pass
try:
return config.get('Server', 'ffmpeg_tmpl', raw=True)
except NoOptionError: #default
return '%(video_codec)s %(video_fps)s %(video_br)s %(max_video_br)s \
%(buff_size)s %(aspect_ratio)s -comment pyTivo.py %(audio_br)s \
%(audio_fr)s %(audio_ch)s %(audio_codec)s %(ffmpeg_pram)s %(format)s'
def getFFmpegPrams(tsn):
if tsn and config.has_section('_tivo_' + tsn):
try:
return config.get('_tivo_' + tsn, 'ffmpeg_pram', raw=True)
except NoOptionError:
pass
try:
return config.get('Server', 'ffmpeg_pram', raw=True)
except NoOptionError:
return None
def isHDtivo(tsn): # tsn's of High Definition Tivo's
return tsn != '' and tsn[:3] in ['648', '652']
def getValidWidths():
return [1920, 1440, 1280, 720, 704, 544, 480, 352]
def getValidHeights():
return [1080, 720, 480] # Technically 240 is also supported
# Return the number in list that is nearest to x
# if two values are equidistant, return the larger
def nearest(x, list):
return reduce(lambda a, b: closest(x, a, b), list)
def closest(x, a, b):
if abs(x - a) < abs(x - b) or (abs(x - a) == abs(x - b) and a > b):
return a
else:
return b
def nearestTivoHeight(height):
return nearest(height, getValidHeights())
def nearestTivoWidth(width):
return nearest(width, getValidWidths())
def getTivoHeight(tsn):
if tsn and config.has_section('_tivo_' + tsn):
try:
height = config.getint('_tivo_' + tsn, 'height')
return nearestTivoHeight(height)
except NoOptionError:
pass
try:
height = config.getint('Server', 'height')
return nearestTivoHeight(height)
except NoOptionError: #defaults for S3/S2 TiVo
if isHDtivo(tsn):
return 720
else:
return 480
def getTivoWidth(tsn):
if tsn and config.has_section('_tivo_' + tsn):
try:
width = config.getint('_tivo_' + tsn, 'width')
return nearestTivoWidth(width)
except NoOptionError:
pass
try:
width = config.getint('Server', 'width')
return nearestTivoWidth(width)
except NoOptionError: #defaults for S3/S2 TiVo
if isHDtivo(tsn):
return 1280
else:
return 544
def getAudioBR(tsn = None):
#convert to non-zero multiple of 64 to ensure ffmpeg compatibility
#compare audio_br to max_audio_br and return lowest
if tsn and config.has_section('_tivo_' + tsn):
try:
audiobr = int(max(int(strtod(config.get('_tivo_' + tsn, 'audio_br'))/1000), 64)/64)*64
return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
except NoOptionError:
pass
try:
audiobr = int(max(int(strtod(config.get('Server', 'audio_br'))/1000), 64)/64)*64
return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
except NoOptionError:
return str(min(384, getMaxAudioBR(tsn))) + 'k'
def getVideoBR(tsn = None):
if tsn and config.has_section('_tivo_' + tsn):
try:
return config.get('_tivo_' + tsn, 'video_br')
except NoOptionError:
pass
try:
return config.get('Server', 'video_br')
except NoOptionError: #defaults for S3/S2 TiVo
if isHDtivo(tsn):
return '8192k'
else:
return '4096K'
def getMaxVideoBR():
try:
return str(int(strtod(config.get('Server', 'max_video_br'))/1000)) + 'k'
except NoOptionError: #default to 17Mi
return '17408k'
def getBuffSize():
try:
return str(int(strtod(config.get('Server', 'bufsize'))))
except NoOptionError: #default 1024k
return '1024k'
def getMaxAudioBR(tsn = None):
#convert to non-zero multiple of 64 for ffmpeg compatibility
if tsn and config.has_section('_tivo_' + tsn):
try:
return int(int(strtod(config.get('_tivo_' + tsn, 'max_audio_br'))/1000)/64)*64
except NoOptionError:
pass
try:
return int(int(strtod(config.get('Server', 'max_audio_br'))/1000)/64)*64
except NoOptionError:
return int(448) #default to 448
def getAudioCodec(tsn = None):
if tsn and config.has_section('_tivo_' + tsn):
try:
return config.get('_tivo_' + tsn, 'audio_codec')
except NoOptionError:
pass
try:
return config.get('Server', 'audio_codec')
except NoOptionError:
return None
def getAudioCH(tsn = None):
if tsn and config.has_section('_tivo_' + tsn):
try:
return config.get('_tivo_' + tsn, 'audio_ch')
except NoOptionError:
pass
try:
return config.get('Server', 'audio_ch')
except NoOptionError:
return None
def getAudioFR(tsn = None):
if tsn and config.has_section('_tivo_' + tsn):
try:
return config.get('_tivo_' + tsn, 'audio_fr')
except NoOptionError:
pass
try:
return config.get('Server', 'audio_fr')
except NoOptionError:
return None
def getVideoFPS(tsn = None):
if tsn and config.has_section('_tivo_' + tsn):
try:
return config.get('_tivo_' + tsn, 'video_fps')
except NoOptionError:
pass
try:
return config.get('Server', 'video_fps')
except NoOptionError:
return None
def getVideoCodec(tsn = None):
if tsn and config.has_section('_tivo_' + tsn):
try:
return config.get('_tivo_' + tsn, 'video_codec')
except NoOptionError:
pass
try:
return config.get('Server', 'video_codec')
except NoOptionError:
return None
def getFormat(tsn = None):
if tsn and config.has_section('_tivo_' + tsn):
try:
return config.get('_tivo_' + tsn, 'force_format')
except NoOptionError:
pass
try:
return config.get('Server', 'force_format')
except NoOptionError:
return None
def str2tuple(s):
items = s.split(',')
L = [x.strip() for x in items]
return tuple(L)
# Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
# For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
# Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
def strtod(value):
prefixes = {'y': -24, 'z': -21, 'a': -18, 'f': -15, 'p': -12,
'n': -9, 'u': -6, 'm': -3, 'c': -2, 'd': -1,
'h': 2, 'k': 3, 'K': 3, 'M': 6, 'G': 9,
'T': 12, 'P': 15, 'E': 18, 'Z': 21, 'Y': 24}
p = re.compile(r'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
m = p.match(value)
if m is None:
raise SyntaxError('Invalid bit value syntax')
(coef, prefix, power, byte) = m.groups()
if prefix is None:
value = float(coef)
else:
exponent = float(prefixes[prefix])
if power == 'i':
# Use powers of 2
value = float(coef) * pow(2.0, exponent / 0.3)
else:
# Use powers of 10
value = float(coef) * pow(10.0, exponent)
if byte == 'B': # B == Byte, b == bit
value *= 8;
return value