1 import ConfigParser
, os
, sys
6 from ConfigParser
import NoOptionError
8 guid
= ''.join([random
.choice(string
.letters
) for i
in range(10)])
10 config
= ConfigParser
.ConfigParser()
11 p
= os
.path
.dirname(__file__
)
15 os
.path
.join(p
, 'pyTivo.conf'),
18 for config_file
in config_files
:
19 if os
.path
.exists(config_file
):
22 print 'ERROR: pyTivo.conf does not exist.\n' + \
23 'You must create this file before running pyTivo.'
25 config
.read(config_files
)
30 config
= ConfigParser
.ConfigParser()
31 config
.read(config_files
)
34 if config
.has_option('Server', 'GUID'):
35 return config
.get('Server', 'GUID')
39 def getTivoUsername():
40 return config
.get('Server', 'tivo_username')
42 def getTivoPassword():
43 return config
.get('Server', 'tivo_password')
45 def getBeaconAddresses():
46 if config
.has_option('Server', 'beacon'):
47 beacon_ips
= config
.get('Server', 'beacon')
49 beacon_ips
= '255.255.255.255'
53 return config
.get('Server', 'Port')
55 def get169Blacklist(tsn
): # tivo does not pad 16:9 video
56 return tsn
and not isHDtivo(tsn
) and not get169Letterbox(tsn
)
58 def get169Letterbox(tsn
): # tivo pads 16:9 video for 4:3 display
59 return tsn
and tsn
[:3] in ('649')
61 def get169Setting(tsn
):
65 if config
.has_section('_tivo_' + tsn
):
66 if config
.has_option('_tivo_' + tsn
, 'aspect169'):
68 return config
.getboolean('_tivo_' + tsn
, 'aspect169')
72 if get169Blacklist(tsn
) or get169Letterbox(tsn
):
77 def getShares(tsn
=''):
78 shares
= [(section
, dict(config
.items(section
)))
79 for section
in config
.sections()
80 if not (section
.startswith(('_tivo_', 'logger_', 'handler_',
82 or section
in ('Server', 'loggers', 'handlers',
87 tsnsect
= '_tivo_' + tsn
88 if config
.has_section(tsnsect
) and config
.has_option(tsnsect
, 'shares'):
89 # clean up leading and trailing spaces & make sure ref is valid
91 for x
in config
.get(tsnsect
, 'shares').split(','):
93 if config
.has_section(y
):
94 tsnshares
.append((y
, dict(config
.items(y
))))
102 return config
.getboolean('Server', 'debug')
103 except NoOptionError
, ValueError:
106 def getOptres(tsn
= None):
107 if tsn
and config
.has_section('_tivo_' + tsn
):
109 return config
.getboolean('_tivo_' + tsn
, 'optres')
110 except NoOptionError
, ValueError:
113 return config
.getboolean('Server', 'optres')
114 except NoOptionError
, ValueError:
118 if config
.has_option('Server', 'par'):
120 return (True, config
.getfloat('Server', 'par'))[ref
]
121 except NoOptionError
, ValueError:
123 return (False, 1.0)[ref
]
125 def get(section
, key
):
126 return config
.get(section
, key
)
128 def getFFmpegTemplate(tsn
):
129 if tsn
and config
.has_section('_tivo_' + tsn
):
131 return config
.get('_tivo_' + tsn
, 'ffmpeg_tmpl', raw
=True)
132 except NoOptionError
:
135 return config
.get('Server', 'ffmpeg_tmpl', raw
=True)
136 except NoOptionError
: #default
137 return '%(video_codec)s %(video_fps)s %(video_br)s %(max_video_br)s \
138 %(buff_size)s %(aspect_ratio)s -comment pyTivo.py %(audio_br)s \
139 %(audio_fr)s %(audio_ch)s %(audio_codec)s %(audio_lang)s \
140 %(ffmpeg_pram)s %(format)s'
142 def getFFmpegPrams(tsn
):
143 if tsn
and config
.has_section('_tivo_' + tsn
):
145 return config
.get('_tivo_' + tsn
, 'ffmpeg_pram', raw
=True)
146 except NoOptionError
:
149 return config
.get('Server', 'ffmpeg_pram', raw
=True)
150 except NoOptionError
:
153 def isHDtivo(tsn
): # tsn's of High Definition Tivo's
154 return tsn
and tsn
[:3] in ['648', '652', '658']
156 def getValidWidths():
157 return [1920, 1440, 1280, 720, 704, 544, 480, 352]
159 def getValidHeights():
160 return [1080, 720, 480] # Technically 240 is also supported
162 # Return the number in list that is nearest to x
163 # if two values are equidistant, return the larger
164 def nearest(x
, list):
165 return reduce(lambda a
, b
: closest(x
, a
, b
), list)
167 def closest(x
, a
, b
):
168 if abs(x
- a
) < abs(x
- b
) or (abs(x
- a
) == abs(x
- b
) and a
> b
):
173 def nearestTivoHeight(height
):
174 return nearest(height
, getValidHeights())
176 def nearestTivoWidth(width
):
177 return nearest(width
, getValidWidths())
179 def getTivoHeight(tsn
):
180 if tsn
and config
.has_section('_tivo_' + tsn
):
182 height
= config
.getint('_tivo_' + tsn
, 'height')
183 return nearestTivoHeight(height
)
184 except NoOptionError
:
187 height
= config
.getint('Server', 'height')
188 return nearestTivoHeight(height
)
189 except NoOptionError
: #defaults for S3/S2 TiVo
195 def getTivoWidth(tsn
):
196 if tsn
and config
.has_section('_tivo_' + tsn
):
198 width
= config
.getint('_tivo_' + tsn
, 'width')
199 return nearestTivoWidth(width
)
200 except NoOptionError
:
203 width
= config
.getint('Server', 'width')
204 return nearestTivoWidth(width
)
205 except NoOptionError
: #defaults for S3/S2 TiVo
212 return max(int(strtod(i
)) / 64000, 1) * 64
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
):
219 audiobr
= _trunc64(config
.get('_tivo_' + tsn
, 'audio_br'))
220 return str(min(audiobr
, getMaxAudioBR(tsn
))) + 'k'
221 except NoOptionError
:
224 audiobr
= _trunc64(config
.get('Server', 'audio_br'))
225 return str(min(audiobr
, getMaxAudioBR(tsn
))) + 'k'
226 except NoOptionError
:
227 return str(min(384, getMaxAudioBR(tsn
))) + 'k'
230 return str(int(strtod(i
)) / 1000) + 'k'
232 def getVideoBR(tsn
= None):
233 if tsn
and config
.has_section('_tivo_' + tsn
):
235 return _k(config
.get('_tivo_' + tsn
, 'video_br'))
236 except NoOptionError
:
239 return _k(config
.get('Server', 'video_br'))
240 except NoOptionError
: #defaults for S3/S2 TiVo
248 return _k(config
.get('Server', 'max_video_br'))
249 except NoOptionError
: #default to 30000k
254 return config
.getfloat('Server', 'video_pct')
255 except NoOptionError
:
258 def getBuffSize(tsn
= None):
259 if tsn
and config
.has_section('_tivo_' + tsn
):
260 if config
.has_option('_tivo_' + tsn
, 'bufsize'):
262 return _k(config
.get('_tivo_' + tsn
, 'bufsize'))
263 except NoOptionError
:
265 if config
.has_option('Server', 'bufsize'):
267 return _k(config
.get('Server', 'bufsize'))
268 except NoOptionError
:
275 def getMaxAudioBR(tsn
= None):
276 #convert to non-zero multiple of 64 for ffmpeg compatibility
277 if tsn
and config
.has_section('_tivo_' + tsn
):
279 return _trunc64(config
.get('_tivo_' + tsn
, 'max_audio_br'))
280 except NoOptionError
:
283 return _trunc64(config
.get('Server', 'max_audio_br'))
284 except NoOptionError
:
285 return int(448) #default to 448
287 def get_tsn(name
, tsn
=None):
288 if tsn
and config
.has_section('_tivo_' + tsn
):
290 return config
.get('_tivo_' + tsn
, name
)
291 except NoOptionError
:
294 return config
.get('Server', name
)
295 except NoOptionError
:
298 def getAudioCodec(tsn
=None):
299 return get_tsn('audio_codec', tsn
)
301 def getAudioCH(tsn
=None):
302 return get_tsn('audio_ch', tsn
)
304 def getAudioFR(tsn
=None):
305 return get_tsn('audio_fr', tsn
)
307 def getAudioLang(tsn
=None):
308 return get_tsn('audio_lang', tsn
)
310 def getCopyTS(tsn
= None):
311 if tsn
and config
.has_section('_tivo_' + tsn
):
312 if config
.has_option('_tivo_' + tsn
, 'copy_ts'):
314 return config
.get('_tivo_' + tsn
, 'copy_ts')
315 except NoOptionError
, ValueError:
317 if config
.has_option('Server', 'copy_ts'):
319 return config
.get('Server', 'copy_ts')
320 except NoOptionError
, ValueError:
324 def getVideoFPS(tsn
=None):
325 return get_tsn('video_fps', tsn
)
327 def getVideoCodec(tsn
=None):
328 return get_tsn('video_codec', tsn
)
330 def getFormat(tsn
= None):
331 return get_tsn('format', tsn
)
333 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
334 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
335 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
337 prefixes
= {'y': -24, 'z': -21, 'a': -18, 'f': -15, 'p': -12,
338 'n': -9, 'u': -6, 'm': -3, 'c': -2, 'd': -1,
339 'h': 2, 'k': 3, 'K': 3, 'M': 6, 'G': 9,
340 'T': 12, 'P': 15, 'E': 18, 'Z': 21, 'Y': 24}
341 p
= re
.compile(r
'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
344 raise SyntaxError('Invalid bit value syntax')
345 (coef
, prefix
, power
, byte
) = m
.groups()
349 exponent
= float(prefixes
[prefix
])
352 value
= float(coef
) * pow(2.0, exponent
/ 0.3)
355 value
= float(coef
) * pow(10.0, exponent
)
356 if byte
== 'B': # B == Byte, b == bit
361 if (config
.has_section('loggers') and
362 config
.has_section('handlers') and
363 config
.has_section('formatters')):
365 logging
.config
.fileConfig(config_files
)
368 logging
.basicConfig(level
=logging
.DEBUG
)
370 logging
.basicConfig(level
=logging
.INFO
)