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