1 import ConfigParser
, os
, sys
5 from ConfigParser
import NoOptionError
7 guid
= ''.join([random
.choice(string
.letters
) for i
in range(10)])
9 config
= ConfigParser
.ConfigParser()
10 p
= os
.path
.dirname(__file__
)
14 os
.path
.join(p
, 'pyTivo.conf'),
17 for config_file
in config_files
:
18 if os
.path
.exists(config_file
):
21 print 'ERROR: pyTivo.conf does not exist.\n' + \
22 'You must create this file before running pyTivo.'
24 config
.read(config_files
)
29 config
= ConfigParser
.ConfigParser()
30 config
.read(config_files
)
33 if config
.has_option('Server', 'GUID'):
34 return config
.get('Server', 'GUID')
38 def getTivoUsername():
39 return config
.get('Server', 'tivo_username')
41 def getTivoPassword():
42 return config
.get('Server', 'tivo_password')
44 def getBeaconAddresses():
45 if config
.has_option('Server', 'beacon'):
46 beacon_ips
= config
.get('Server', 'beacon')
48 beacon_ips
= '255.255.255.255'
52 return config
.get('Server', 'Port')
54 def get169Blacklist(tsn
): # tivo does not pad 16:9 video
55 return tsn
!= '' and tsn
[:3] in ('130', '540')
57 def get169Letterbox(tsn
): # tivo pads 16:9 video for 4:3 display
58 return tsn
!= '' and tsn
[:3] in ('649')
60 def get169Setting(tsn
):
64 if config
.has_section('_tivo_' + tsn
):
65 if config
.has_option('_tivo_' + tsn
, 'aspect169'):
67 return config
.getboolean('_tivo_' + tsn
, 'aspect169')
71 if get169Blacklist(tsn
) or get169Letterbox(tsn
):
76 def getShares(tsn
=''):
77 shares
= [(section
, dict(config
.items(section
)))
78 for section
in config
.sections()
80 section
.startswith('_tivo_')
81 or section
in ('Server', 'loggers', 'handlers', 'formatters')
82 or section
.startswith('logger_')
83 or section
.startswith('handler_')
84 or section
.startswith('formatter_')
88 if config
.has_section('_tivo_' + tsn
):
89 if config
.has_option('_tivo_' + tsn
, 'shares'):
90 # clean up leading and trailing spaces & make sure ref is valid
92 for x
in config
.get('_tivo_' + tsn
, 'shares').split(','):
93 y
= x
.lstrip().rstrip()
94 if config
.has_section(y
):
95 tsnshares
+= [(y
, dict(config
.items(y
)))]
99 for name
, data
in shares
:
100 if not data
.get('auto_subshares', 'False').lower() == 'true':
103 base_path
= data
['path']
105 for item
in os
.listdir(base_path
):
106 item_path
= os
.path
.join(base_path
, item
)
107 if not os
.path
.isdir(item_path
) or item
.startswith('.'):
110 new_name
= name
+ '/' + item
111 new_data
= dict(data
)
112 new_data
['path'] = item_path
114 shares
.append((new_name
, new_data
))
122 return config
.getboolean('Server', 'debug')
123 except NoOptionError
, ValueError:
128 return config
.getboolean('Server', 'hack83')
129 except NoOptionError
, ValueError:
132 def getOptres(tsn
= None):
133 if tsn
and config
.has_section('_tivo_' + tsn
):
135 return config
.getboolean('_tivo_' + tsn
, 'optres')
136 except NoOptionError
, ValueError:
139 return config
.getboolean('Server', 'optres')
140 except NoOptionError
, ValueError:
144 if config
.has_option('Server', 'par'):
146 return (True, config
.getfloat('Server', 'par'))[ref
]
147 except NoOptionError
, ValueError:
149 return (False, 1.0)[ref
]
151 def get(section
, key
):
152 return config
.get(section
, key
)
154 def getFFmpegTemplate(tsn
):
155 if tsn
and config
.has_section('_tivo_' + tsn
):
157 return config
.get('_tivo_' + tsn
, 'ffmpeg_tmpl', raw
=True)
158 except NoOptionError
:
161 return config
.get('Server', 'ffmpeg_tmpl', raw
=True)
162 except NoOptionError
: #default
163 return '%(video_codec)s %(video_fps)s %(video_br)s %(max_video_br)s \
164 %(buff_size)s %(aspect_ratio)s -comment pyTivo.py %(audio_br)s \
165 %(audio_fr)s %(audio_ch)s %(audio_codec)s %(audio_lang)s \
166 %(ffmpeg_pram)s %(format)s'
168 def getFFmpegPrams(tsn
):
169 if tsn
and config
.has_section('_tivo_' + tsn
):
171 return config
.get('_tivo_' + tsn
, 'ffmpeg_pram', raw
=True)
172 except NoOptionError
:
175 return config
.get('Server', 'ffmpeg_pram', raw
=True)
176 except NoOptionError
:
179 def isHDtivo(tsn
): # tsn's of High Definition Tivo's
180 return tsn
!= '' and tsn
[:3] in ['648', '652', '658']
182 def getValidWidths():
183 return [1920, 1440, 1280, 720, 704, 544, 480, 352]
185 def getValidHeights():
186 return [1080, 720, 480] # Technically 240 is also supported
188 # Return the number in list that is nearest to x
189 # if two values are equidistant, return the larger
190 def nearest(x
, list):
191 return reduce(lambda a
, b
: closest(x
, a
, b
), list)
193 def closest(x
, a
, b
):
194 if abs(x
- a
) < abs(x
- b
) or (abs(x
- a
) == abs(x
- b
) and a
> b
):
199 def nearestTivoHeight(height
):
200 return nearest(height
, getValidHeights())
202 def nearestTivoWidth(width
):
203 return nearest(width
, getValidWidths())
205 def getTivoHeight(tsn
):
206 if tsn
and config
.has_section('_tivo_' + tsn
):
208 height
= config
.getint('_tivo_' + tsn
, 'height')
209 return nearestTivoHeight(height
)
210 except NoOptionError
:
213 height
= config
.getint('Server', 'height')
214 return nearestTivoHeight(height
)
215 except NoOptionError
: #defaults for S3/S2 TiVo
221 def getTivoWidth(tsn
):
222 if tsn
and config
.has_section('_tivo_' + tsn
):
224 width
= config
.getint('_tivo_' + tsn
, 'width')
225 return nearestTivoWidth(width
)
226 except NoOptionError
:
229 width
= config
.getint('Server', 'width')
230 return nearestTivoWidth(width
)
231 except NoOptionError
: #defaults for S3/S2 TiVo
238 return max(int(strtod(i
)) / 64000, 1) * 64
240 def getAudioBR(tsn
= None):
241 #convert to non-zero multiple of 64 to ensure ffmpeg compatibility
242 #compare audio_br to max_audio_br and return lowest
243 if tsn
and config
.has_section('_tivo_' + tsn
):
245 audiobr
= _trunc64(config
.get('_tivo_' + tsn
, 'audio_br'))
246 return str(min(audiobr
, getMaxAudioBR(tsn
))) + 'k'
247 except NoOptionError
:
250 audiobr
= _trunc64(config
.get('Server', 'audio_br'))
251 return str(min(audiobr
, getMaxAudioBR(tsn
))) + 'k'
252 except NoOptionError
:
253 return str(min(384, getMaxAudioBR(tsn
))) + 'k'
256 return str(int(strtod(i
)) / 1000) + 'k'
258 def getVideoBR(tsn
= None):
259 if tsn
and config
.has_section('_tivo_' + tsn
):
261 return _k(config
.get('_tivo_' + tsn
, 'video_br'))
262 except NoOptionError
:
265 return _k(config
.get('Server', 'video_br'))
266 except NoOptionError
: #defaults for S3/S2 TiVo
274 return _k(config
.get('Server', 'max_video_br'))
275 except NoOptionError
: #default to 30000k
280 return config
.getfloat('Server', 'video_pct')
281 except NoOptionError
:
286 return _k(config
.get('Server', 'bufsize'))
287 except NoOptionError
: #default 1024k
290 def getMaxAudioBR(tsn
= None):
291 #convert to non-zero multiple of 64 for ffmpeg compatibility
292 if tsn
and config
.has_section('_tivo_' + tsn
):
294 return _trunc64(config
.get('_tivo_' + tsn
, 'max_audio_br'))
295 except NoOptionError
:
298 return _trunc64(config
.get('Server', 'max_audio_br'))
299 except NoOptionError
:
300 return int(448) #default to 448
302 def get_tsn(name
, tsn
=None):
303 if tsn
and config
.has_section('_tivo_' + tsn
):
305 return config
.get('_tivo_' + tsn
, name
)
306 except NoOptionError
:
309 return config
.get('Server', name
)
310 except NoOptionError
:
313 def getAudioCodec(tsn
=None):
314 return get_tsn('audio_codec', tsn
)
316 def getAudioCH(tsn
=None):
317 return get_tsn('audio_ch', tsn
)
319 def getAudioFR(tsn
=None):
320 return get_tsn('audio_fr', tsn
)
322 def getAudioLang(tsn
=None):
323 return get_tsn('audio_lang', tsn
)
325 def getCopyTS(tsn
= None):
326 if tsn
and config
.has_section('_tivo_' + tsn
):
327 if config
.has_option('_tivo_' + tsn
, 'copy_ts'):
329 return config
.get('_tivo_' + tsn
, 'copy_ts')
330 except NoOptionError
, ValueError:
332 if config
.has_option('Server', 'copy_ts'):
334 return config
.get('Server', 'copy_ts')
335 except NoOptionError
, ValueError:
339 def getVideoFPS(tsn
=None):
340 return get_tsn('video_fps', tsn
)
342 def getVideoCodec(tsn
=None):
343 return get_tsn('video_codec', tsn
)
345 def getFormat(tsn
= None):
346 return get_tsn('format', tsn
)
348 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
349 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
350 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
352 prefixes
= {'y': -24, 'z': -21, 'a': -18, 'f': -15, 'p': -12,
353 'n': -9, 'u': -6, 'm': -3, 'c': -2, 'd': -1,
354 'h': 2, 'k': 3, 'K': 3, 'M': 6, 'G': 9,
355 'T': 12, 'P': 15, 'E': 18, 'Z': 21, 'Y': 24}
356 p
= re
.compile(r
'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
359 raise SyntaxError('Invalid bit value syntax')
360 (coef
, prefix
, power
, byte
) = m
.groups()
364 exponent
= float(prefixes
[prefix
])
367 value
= float(coef
) * pow(2.0, exponent
/ 0.3)
370 value
= float(coef
) * pow(10.0, exponent
)
371 if byte
== 'B': # B == Byte, b == bit