1 import ConfigParser
, os
, sys
5 from ConfigParser
import NoOptionError
7 BLACKLIST_169
= ('540', '649')
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 tsn
[:3] in ('540')
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()
81 section
.startswith('_tivo_')
82 or section
in ('Server', 'loggers', 'handlers', 'formatters')
83 or section
.startswith('logger_')
84 or section
.startswith('handler_')
85 or section
.startswith('formatter_')
89 if config
.has_section('_tivo_' + tsn
):
90 if config
.has_option('_tivo_' + tsn
, 'shares'):
91 # clean up leading and trailing spaces & make sure ref is valid
93 for x
in config
.get('_tivo_' + tsn
, 'shares').split(','):
94 y
= x
.lstrip().rstrip()
95 if config
.has_section(y
):
96 tsnshares
+= [(y
, dict(config
.items(y
)))]
100 for name
, data
in shares
:
101 if not data
.get('auto_subshares', 'False').lower() == 'true':
104 base_path
= data
['path']
106 for item
in os
.listdir(base_path
):
107 item_path
= os
.path
.join(base_path
, item
)
108 if not os
.path
.isdir(item_path
) or item
.startswith('.'):
111 new_name
= name
+ '/' + item
112 new_data
= dict(data
)
113 new_data
['path'] = item_path
115 shares
.append((new_name
, new_data
))
123 return config
.getboolean('Server', 'debug')
124 except NoOptionError
, ValueError:
129 debug
= config
.get('Server', 'hack83')
130 if debug
.lower() == 'true':
134 except NoOptionError
:
137 def getOptres(tsn
= None):
138 if tsn
and config
.has_section('_tivo_' + tsn
):
140 return config
.getboolean('_tivo_' + tsn
, 'optres')
141 except NoOptionError
, ValueError:
144 return config
.getboolean('Server', 'optres')
145 except NoOptionError
, ValueError:
149 if config
.has_option('Server', 'par'):
151 return (True, config
.getfloat('Server', 'par'))[ref
]
152 except NoOptionError
, ValueError:
154 return (False, 1.0)[ref
]
156 def get(section
, key
):
157 return config
.get(section
, key
)
159 def getFFmpegTemplate(tsn
):
160 if tsn
and config
.has_section('_tivo_' + tsn
):
162 return config
.get('_tivo_' + tsn
, 'ffmpeg_tmpl', raw
=True)
163 except NoOptionError
:
166 return config
.get('Server', 'ffmpeg_tmpl', raw
=True)
167 except NoOptionError
: #default
168 return '%(video_codec)s %(video_fps)s %(video_br)s %(max_video_br)s \
169 %(buff_size)s %(aspect_ratio)s -comment pyTivo.py %(audio_br)s \
170 %(audio_fr)s %(audio_ch)s %(audio_codec)s %(audio_lang)s \
171 %(ffmpeg_pram)s %(format)s'
173 def getFFmpegPrams(tsn
):
174 if tsn
and config
.has_section('_tivo_' + tsn
):
176 return config
.get('_tivo_' + tsn
, 'ffmpeg_pram', raw
=True)
177 except NoOptionError
:
180 return config
.get('Server', 'ffmpeg_pram', raw
=True)
181 except NoOptionError
:
184 def isHDtivo(tsn
): # tsn's of High Definition Tivo's
185 return tsn
!= '' and tsn
[:3] in ['648', '652']
187 def getValidWidths():
188 return [1920, 1440, 1280, 720, 704, 544, 480, 352]
190 def getValidHeights():
191 return [1080, 720, 480] # Technically 240 is also supported
193 # Return the number in list that is nearest to x
194 # if two values are equidistant, return the larger
195 def nearest(x
, list):
196 return reduce(lambda a
, b
: closest(x
, a
, b
), list)
198 def closest(x
, a
, b
):
199 if abs(x
- a
) < abs(x
- b
) or (abs(x
- a
) == abs(x
- b
) and a
> b
):
204 def nearestTivoHeight(height
):
205 return nearest(height
, getValidHeights())
207 def nearestTivoWidth(width
):
208 return nearest(width
, getValidWidths())
210 def getTivoHeight(tsn
):
211 if tsn
and config
.has_section('_tivo_' + tsn
):
213 height
= config
.getint('_tivo_' + tsn
, 'height')
214 return nearestTivoHeight(height
)
215 except NoOptionError
:
218 height
= config
.getint('Server', 'height')
219 return nearestTivoHeight(height
)
220 except NoOptionError
: #defaults for S3/S2 TiVo
226 def getTivoWidth(tsn
):
227 if tsn
and config
.has_section('_tivo_' + tsn
):
229 width
= config
.getint('_tivo_' + tsn
, 'width')
230 return nearestTivoWidth(width
)
231 except NoOptionError
:
234 width
= config
.getint('Server', 'width')
235 return nearestTivoWidth(width
)
236 except NoOptionError
: #defaults for S3/S2 TiVo
242 def getAudioBR(tsn
= None):
243 #convert to non-zero multiple of 64 to ensure ffmpeg compatibility
244 #compare audio_br to max_audio_br and return lowest
245 if tsn
and config
.has_section('_tivo_' + tsn
):
247 audiobr
= int(max(int(strtod(config
.get('_tivo_' + tsn
, 'audio_br'))/1000), 64)/64)*64
248 return str(min(audiobr
, getMaxAudioBR(tsn
))) + 'k'
249 except NoOptionError
:
252 audiobr
= int(max(int(strtod(config
.get('Server', 'audio_br'))/1000), 64)/64)*64
253 return str(min(audiobr
, getMaxAudioBR(tsn
))) + 'k'
254 except NoOptionError
:
255 return str(min(384, getMaxAudioBR(tsn
))) + 'k'
257 def getVideoBR(tsn
= None):
258 if tsn
and config
.has_section('_tivo_' + tsn
):
260 return str(int(strtod(config
.get('_tivo_' + tsn
, 'video_br'))/1000)) + 'k'
261 except NoOptionError
:
264 return str(int(strtod(config
.get('Server', 'video_br'))/1000)) + 'k'
265 except NoOptionError
: #defaults for S3/S2 TiVo
273 return str(int(strtod(config
.get('Server', 'max_video_br'))/1000)) + 'k'
274 except NoOptionError
: #default to 30000k
279 return config
.getfloat('Server', 'video_pct')
280 except NoOptionError
:
285 return str(int(strtod(config
.get('Server', 'bufsize'))/1000)) + 'k'
286 except NoOptionError
: #default 1024k
289 def getMaxAudioBR(tsn
= None):
290 #convert to non-zero multiple of 64 for ffmpeg compatibility
291 if tsn
and config
.has_section('_tivo_' + tsn
):
293 return int(int(strtod(config
.get('_tivo_' + tsn
, 'max_audio_br'))/1000)/64)*64
294 except NoOptionError
:
297 return int(int(strtod(config
.get('Server', 'max_audio_br'))/1000)/64)*64
298 except NoOptionError
:
299 return int(448) #default to 448
301 def get_tsn(name
, tsn
=None):
302 if tsn
and config
.has_section('_tivo_' + tsn
):
304 return config
.get('_tivo_' + tsn
, name
)
305 except NoOptionError
:
308 return config
.get('Server', name
)
309 except NoOptionError
:
312 def getAudioCodec(tsn
=None):
313 return get_tsn('audio_codec', tsn
)
315 def getAudioCH(tsn
=None):
316 return get_tsn('audio_ch', tsn
)
318 def getAudioFR(tsn
=None):
319 return get_tsn('audio_fr', tsn
)
321 def getAudioLang(tsn
=None):
322 return get_tsn('audio_lang', tsn
)
324 def getCopyTS(tsn
= None):
325 if tsn
and config
.has_section('_tivo_' + tsn
):
326 if config
.has_option('_tivo_' + tsn
, 'copy_ts'):
328 return config
.get('_tivo_' + tsn
, 'copy_ts')
329 except NoOptionError
, ValueError:
331 if config
.has_option('Server', 'copy_ts'):
333 return config
.get('Server', 'copy_ts')
334 except NoOptionError
, ValueError:
338 def getVideoFPS(tsn
=None):
339 return get_tsn('video_fps', tsn
)
341 def getVideoCodec(tsn
=None):
342 return get_tsn('video_codec', tsn
)
344 def getFormat(tsn
= None):
345 return get_tsn('force_format', tsn
)
347 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
348 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
349 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
351 prefixes
= {'y': -24, 'z': -21, 'a': -18, 'f': -15, 'p': -12,
352 'n': -9, 'u': -6, 'm': -3, 'c': -2, 'd': -1,
353 'h': 2, 'k': 3, 'K': 3, 'M': 6, 'G': 9,
354 'T': 12, 'P': 15, 'E': 18, 'Z': 21, 'Y': 24}
355 p
= re
.compile(r
'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
358 raise SyntaxError('Invalid bit value syntax')
359 (coef
, prefix
, power
, byte
) = m
.groups()
363 exponent
= float(prefixes
[prefix
])
366 value
= float(coef
) * pow(2.0, exponent
/ 0.3)
369 value
= float(coef
) * pow(10.0, exponent
)
370 if byte
== 'B': # B == Byte, b == bit