Don't show CopyProtected shows in NPL
[pyTivo/krkeegan.git] / config.py
blobacf72e038f16f203b8182998034bc0f396bfd20e
1 import ConfigParser, os, sys
2 import re
3 from ConfigParser import NoOptionError
5 config = ConfigParser.ConfigParser()
6 p = os.path.dirname(__file__)
7 config_file = os.path.join(p, 'pyTivo.conf')
8 if not os.path.exists(config_file):
9 print 'ERROR: pyTivo.conf does not exist.\n' + \
10 'You must create this file before running pyTivo.'
11 sys.exit(1)
12 config.read(config_file)
14 def reset():
15 global config
16 del config
17 config = ConfigParser.ConfigParser()
18 config.read(config_file)
20 def getGUID():
21 if config.has_option('Server', 'GUID'):
22 guid = config.get('Server', 'GUID')
23 else:
24 guid = '123456'
25 return guid
27 def getBeaconAddresses():
28 if config.has_option('Server', 'beacon'):
29 beacon_ips = config.get('Server', 'beacon')
30 else:
31 beacon_ips = '255.255.255.255'
32 return beacon_ips
34 def getPort():
35 return config.get('Server', 'Port')
37 def get169Blacklist(tsn): # tivo does not pad 16:9 video
38 return tsn != '' and tsn[:3] in ('540')
40 def get169Letterbox(tsn): # tivo pads 16:9 video for 4:3 display
41 return tsn != '' and tsn[:3] in ('649')
43 def get169Setting(tsn):
44 if not tsn:
45 return True
47 if config.has_section('_tivo_' + tsn):
48 if config.has_option('_tivo_' + tsn, 'aspect169'):
49 try:
50 return config.getboolean('_tivo_' + tsn, 'aspect169')
51 except ValueError:
52 pass
54 if get169Blacklist(tsn) or get169Letterbox(tsn):
55 return False
57 return True
59 def getShares(tsn=''):
60 shares = [(section, dict(config.items(section)))
61 for section in config.sections()
62 if not(section.startswith('_tivo_') or section == 'Server')]
64 if config.has_section('_tivo_' + tsn):
65 if config.has_option('_tivo_' + tsn, 'shares'):
66 # clean up leading and trailing spaces & make sure ref is valid
67 tsnshares = []
68 for x in config.get('_tivo_' + tsn, 'shares').split(','):
69 y = x.lstrip().rstrip()
70 if config.has_section(y):
71 tsnshares += [(y, dict(config.items(y)))]
72 if tsnshares:
73 shares = tsnshares
75 for name, data in shares:
76 if not data.get('auto_subshares', 'False').lower() == 'true':
77 continue
79 base_path = data['path']
80 try:
81 for item in os.listdir(base_path):
82 item_path = os.path.join(base_path, item)
83 if not os.path.isdir(item_path) or item.startswith('.'):
84 continue
86 new_name = name + '/' + item
87 new_data = dict(data)
88 new_data['path'] = item_path
90 shares.append((new_name, new_data))
91 except:
92 pass
94 return shares
96 def getDebug(ref):
97 if config.has_option('Server', 'debug'):
98 try:
99 return str2tuple(config.get('Server', 'debug')+',,')[ref]
100 except NoOptionError:
101 pass
102 return str2tuple('False,,')[ref]
104 def getHack83():
105 try:
106 debug = config.get('Server', 'hack83')
107 if debug.lower() == 'true':
108 return True
109 else:
110 return False
111 except NoOptionError:
112 return False
114 def getOptres(tsn = None):
115 if tsn and config.has_section('_tivo_' + tsn):
116 try:
117 return config.getboolean('_tivo_' + tsn, 'optres')
118 except NoOptionError, ValueError:
119 pass
120 try:
121 return config.getboolean('Server', 'optres')
122 except NoOptionError, ValueError:
123 return False
125 def getPixelAR(ref):
126 if config.has_option('Server', 'par'):
127 try:
128 return (True, config.getfloat('Server', 'par'))[ref]
129 except NoOptionError, ValueError:
130 pass
131 return (False, 1.0)[ref]
133 def get(section, key):
134 return config.get(section, key)
136 def getFFmpegTemplate(tsn):
137 if tsn and config.has_section('_tivo_' + tsn):
138 try:
139 return config.get('_tivo_' + tsn, 'ffmpeg_tmpl', raw=True)
140 except NoOptionError:
141 pass
142 try:
143 return config.get('Server', 'ffmpeg_tmpl', raw=True)
144 except NoOptionError: #default
145 return '%(video_codec)s %(video_fps)s %(video_br)s %(max_video_br)s \
146 %(buff_size)s %(aspect_ratio)s -comment pyTivo.py %(audio_br)s \
147 %(audio_fr)s %(audio_ch)s %(audio_codec)s %(ffmpeg_pram)s %(format)s'
149 def getFFmpegPrams(tsn):
150 if tsn and config.has_section('_tivo_' + tsn):
151 try:
152 return config.get('_tivo_' + tsn, 'ffmpeg_pram', raw=True)
153 except NoOptionError:
154 pass
155 try:
156 return config.get('Server', 'ffmpeg_pram', raw=True)
157 except NoOptionError:
158 return None
160 def isHDtivo(tsn): # tsn's of High Definition Tivo's
161 return tsn != '' and tsn[:3] in ['648', '652']
163 def getValidWidths():
164 return [1920, 1440, 1280, 720, 704, 544, 480, 352]
166 def getValidHeights():
167 return [1080, 720, 480] # Technically 240 is also supported
169 # Return the number in list that is nearest to x
170 # if two values are equidistant, return the larger
171 def nearest(x, list):
172 return reduce(lambda a, b: closest(x, a, b), list)
174 def closest(x, a, b):
175 if abs(x - a) < abs(x - b) or (abs(x - a) == abs(x - b) and a > b):
176 return a
177 else:
178 return b
180 def nearestTivoHeight(height):
181 return nearest(height, getValidHeights())
183 def nearestTivoWidth(width):
184 return nearest(width, getValidWidths())
186 def getTivoHeight(tsn):
187 if tsn and config.has_section('_tivo_' + tsn):
188 try:
189 height = config.getint('_tivo_' + tsn, 'height')
190 return nearestTivoHeight(height)
191 except NoOptionError:
192 pass
193 try:
194 height = config.getint('Server', 'height')
195 return nearestTivoHeight(height)
196 except NoOptionError: #defaults for S3/S2 TiVo
197 if isHDtivo(tsn):
198 return 720
199 else:
200 return 480
202 def getTivoWidth(tsn):
203 if tsn and config.has_section('_tivo_' + tsn):
204 try:
205 width = config.getint('_tivo_' + tsn, 'width')
206 return nearestTivoWidth(width)
207 except NoOptionError:
208 pass
209 try:
210 width = config.getint('Server', 'width')
211 return nearestTivoWidth(width)
212 except NoOptionError: #defaults for S3/S2 TiVo
213 if isHDtivo(tsn):
214 return 1280
215 else:
216 return 544
218 def getAudioBR(tsn = None):
219 #convert to non-zero multiple of 64 to ensure ffmpeg compatibility
220 #compare audio_br to max_audio_br and return lowest
221 if tsn and config.has_section('_tivo_' + tsn):
222 try:
223 audiobr = int(max(int(strtod(config.get('_tivo_' + tsn, 'audio_br'))/1000), 64)/64)*64
224 return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
225 except NoOptionError:
226 pass
227 try:
228 audiobr = int(max(int(strtod(config.get('Server', 'audio_br'))/1000), 64)/64)*64
229 return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
230 except NoOptionError:
231 return str(min(384, getMaxAudioBR(tsn))) + 'k'
233 def getVideoBR(tsn = None):
234 if tsn and config.has_section('_tivo_' + tsn):
235 try:
236 return config.get('_tivo_' + tsn, 'video_br')
237 except NoOptionError:
238 pass
239 try:
240 return config.get('Server', 'video_br')
241 except NoOptionError: #defaults for S3/S2 TiVo
242 if isHDtivo(tsn):
243 return '8192k'
244 else:
245 return '4096K'
247 def getMaxVideoBR():
248 try:
249 return str(int(strtod(config.get('Server', 'max_video_br'))/1000)) + 'k'
250 except NoOptionError: #default to 30000k
251 return '30000k'
253 def getVideoPCT():
254 try:
255 return config.getfloat('Server', 'video_pct')
256 except NoOptionError:
257 return 70
259 def getBuffSize():
260 try:
261 return str(int(strtod(config.get('Server', 'bufsize'))))
262 except NoOptionError: #default 1024k
263 return '1024k'
265 def getMaxAudioBR(tsn = None):
266 #convert to non-zero multiple of 64 for ffmpeg compatibility
267 if tsn and config.has_section('_tivo_' + tsn):
268 try:
269 return int(int(strtod(config.get('_tivo_' + tsn, 'max_audio_br'))/1000)/64)*64
270 except NoOptionError:
271 pass
272 try:
273 return int(int(strtod(config.get('Server', 'max_audio_br'))/1000)/64)*64
274 except NoOptionError:
275 return int(448) #default to 448
277 def getAudioCodec(tsn = None):
278 if tsn and config.has_section('_tivo_' + tsn):
279 try:
280 return config.get('_tivo_' + tsn, 'audio_codec')
281 except NoOptionError:
282 pass
283 try:
284 return config.get('Server', 'audio_codec')
285 except NoOptionError:
286 return None
288 def getAudioCH(tsn = None):
289 if tsn and config.has_section('_tivo_' + tsn):
290 try:
291 return config.get('_tivo_' + tsn, 'audio_ch')
292 except NoOptionError:
293 pass
294 try:
295 return config.get('Server', 'audio_ch')
296 except NoOptionError:
297 return None
299 def getAudioFR(tsn = None):
300 if tsn and config.has_section('_tivo_' + tsn):
301 try:
302 return config.get('_tivo_' + tsn, 'audio_fr')
303 except NoOptionError:
304 pass
305 try:
306 return config.get('Server', 'audio_fr')
307 except NoOptionError:
308 return None
310 def getCopyTS(tsn = None):
311 if tsn and config.has_section('_tivo_' + tsn):
312 if config.has_option('_tivo_' + tsn, 'copy_ts'):
313 try:
314 return config.get('_tivo_' + tsn, 'copy_ts')
315 except NoOptionError, ValueError:
316 pass
317 if config.has_option('Server', 'copy_ts'):
318 try:
319 return config.get('Server', 'copy_ts')
320 except NoOptionError, ValueError:
321 pass
322 return 'none'
324 def getVideoFPS(tsn = None):
325 if tsn and config.has_section('_tivo_' + tsn):
326 try:
327 return config.get('_tivo_' + tsn, 'video_fps')
328 except NoOptionError:
329 pass
330 try:
331 return config.get('Server', 'video_fps')
332 except NoOptionError:
333 return None
335 def getVideoCodec(tsn = None):
336 if tsn and config.has_section('_tivo_' + tsn):
337 try:
338 return config.get('_tivo_' + tsn, 'video_codec')
339 except NoOptionError:
340 pass
341 try:
342 return config.get('Server', 'video_codec')
343 except NoOptionError:
344 return None
346 def getFormat(tsn = None):
347 if tsn and config.has_section('_tivo_' + tsn):
348 try:
349 return config.get('_tivo_' + tsn, 'force_format')
350 except NoOptionError:
351 pass
352 try:
353 return config.get('Server', 'force_format')
354 except NoOptionError:
355 return None
357 def str2tuple(s):
358 items = s.split(',')
359 L = [x.strip() for x in items]
360 return tuple(L)
362 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
363 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
364 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
365 def strtod(value):
366 prefixes = {'y': -24, 'z': -21, 'a': -18, 'f': -15, 'p': -12,
367 'n': -9, 'u': -6, 'm': -3, 'c': -2, 'd': -1,
368 'h': 2, 'k': 3, 'K': 3, 'M': 6, 'G': 9,
369 'T': 12, 'P': 15, 'E': 18, 'Z': 21, 'Y': 24}
370 p = re.compile(r'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
371 m = p.match(value)
372 if m is None:
373 raise SyntaxError('Invalid bit value syntax')
374 (coef, prefix, power, byte) = m.groups()
375 if prefix is None:
376 value = float(coef)
377 else:
378 exponent = float(prefixes[prefix])
379 if power == 'i':
380 # Use powers of 2
381 value = float(coef) * pow(2.0, exponent / 0.3)
382 else:
383 # Use powers of 10
384 value = float(coef) * pow(10.0, exponent)
385 if byte == 'B': # B == Byte, b == bit
386 value *= 8;
387 return value