1 import ConfigParser
, os
3 from ConfigParser
import NoOptionError
5 BLACKLIST_169
= ('540', '649')
7 config
= ConfigParser
.ConfigParser()
8 p
= os
.path
.dirname(__file__
)
9 config_file
= os
.path
.join(p
, 'pyTivo.conf')
10 config
.read(config_file
)
15 config
= ConfigParser
.ConfigParser()
16 config
.read(config_file
)
19 if config
.has_option('Server', 'GUID'):
20 guid
= config
.get('Server', 'GUID')
25 def getTivoUsername():
26 return config
.get('Server', 'tivo_username')
28 def getTivoPassword():
29 return config
.get('Server', 'tivo_password')
31 def getBeaconAddresses():
32 if config
.has_option('Server', 'beacon'):
33 beacon_ips
= config
.get('Server', 'beacon')
35 beacon_ips
= '255.255.255.255'
39 return config
.get('Server', 'Port')
41 def get169Setting(tsn
):
45 if config
.has_section('_tivo_' + tsn
):
46 if config
.has_option('_tivo_' + tsn
, 'aspect169'):
48 return config
.getboolean('_tivo_' + tsn
, 'aspect169')
52 if tsn
[:3] in BLACKLIST_169
:
57 def getShares(tsn
=''):
58 shares
= [(section
, dict(config
.items(section
)))
59 for section
in config
.sections()
60 if not(section
.startswith('_tivo_') or section
== 'Server')]
62 if config
.has_section('_tivo_' + tsn
):
63 if config
.has_option('_tivo_' + tsn
, 'shares'):
64 # clean up leading and trailing spaces & make sure ref is valid
66 for x
in config
.get('_tivo_' + tsn
, 'shares').split(','):
67 y
= x
.lstrip().rstrip()
68 if config
.has_section(y
):
69 tsnshares
+= [(y
, dict(config
.items(y
)))]
73 for name
, data
in shares
:
74 if not data
.get('auto_subshares', 'False').lower() == 'true':
77 base_path
= data
['path']
79 for item
in os
.listdir(base_path
):
80 item_path
= os
.path
.join(base_path
, item
)
81 if not os
.path
.isdir(item_path
):
84 new_name
= name
+ '/' + item
86 new_data
['path'] = item_path
88 shares
.append((new_name
, new_data
))
95 if config
.has_option('Server', 'debug'):
97 return str2tuple(config
.get('Server', 'debug')+',,')[ref
]
100 return str2tuple('False,,')[ref
]
104 debug
= config
.get('Server', 'hack83')
105 if debug
.lower() == 'true':
109 except NoOptionError
:
112 def getOptres(tsn
= None):
113 if tsn
and config
.has_section('_tivo_' + tsn
):
115 return config
.getboolean('_tivo_' + tsn
, 'optres')
116 except NoOptionError
, ValueError:
119 return config
.getboolean('Server', 'optres')
120 except NoOptionError
, ValueError:
124 if config
.has_option('Server', 'par'):
126 return (True, config
.getfloat('Server', 'par'))[ref
]
127 except NoOptionError
, ValueError:
129 return (False, 1.0)[ref
]
131 def get(section
, key
):
132 return config
.get(section
, key
)
134 def getFFmpegTemplate(tsn
):
135 if tsn
and config
.has_section('_tivo_' + tsn
):
137 return config
.get('_tivo_' + tsn
, 'ffmpeg_tmpl', raw
=True)
138 except NoOptionError
:
141 return config
.get('Server', 'ffmpeg_tmpl', raw
=True)
142 except NoOptionError
: #default
143 return '%(video_codec)s %(video_fps)s %(video_br)s %(max_video_br)s \
144 %(buff_size)s %(aspect_ratio)s -comment pyTivo.py %(audio_br)s \
145 %(audio_fr)s %(audio_ch)s %(audio_codec)s %(ffmpeg_pram)s %(format)s'
147 def getFFmpegPrams(tsn
):
148 if tsn
and config
.has_section('_tivo_' + tsn
):
150 return config
.get('_tivo_' + tsn
, 'ffmpeg_pram', raw
=True)
151 except NoOptionError
:
154 return config
.get('Server', 'ffmpeg_pram', raw
=True)
155 except NoOptionError
:
158 def isHDtivo(tsn
): # tsn's of High Definition Tivo's
159 return tsn
!= '' and tsn
[:3] in ['648', '652']
161 def getValidWidths():
162 return [1920, 1440, 1280, 720, 704, 544, 480, 352]
164 def getValidHeights():
165 return [1080, 720, 480] # Technically 240 is also supported
167 # Return the number in list that is nearest to x
168 # if two values are equidistant, return the larger
169 def nearest(x
, list):
170 return reduce(lambda a
, b
: closest(x
, a
, b
), list)
172 def closest(x
, a
, b
):
173 if abs(x
- a
) < abs(x
- b
) or (abs(x
- a
) == abs(x
- b
) and a
> b
):
178 def nearestTivoHeight(height
):
179 return nearest(height
, getValidHeights())
181 def nearestTivoWidth(width
):
182 return nearest(width
, getValidWidths())
184 def getTivoHeight(tsn
):
185 if tsn
and config
.has_section('_tivo_' + tsn
):
187 height
= config
.getint('_tivo_' + tsn
, 'height')
188 return nearestTivoHeight(height
)
189 except NoOptionError
:
192 height
= config
.getint('Server', 'height')
193 return nearestTivoHeight(height
)
194 except NoOptionError
: #defaults for S3/S2 TiVo
200 def getTivoWidth(tsn
):
201 if tsn
and config
.has_section('_tivo_' + tsn
):
203 width
= config
.getint('_tivo_' + tsn
, 'width')
204 return nearestTivoWidth(width
)
205 except NoOptionError
:
208 width
= config
.getint('Server', 'width')
209 return nearestTivoWidth(width
)
210 except NoOptionError
: #defaults for S3/S2 TiVo
216 def getAudioBR(tsn
= None):
217 #convert to non-zero multiple of 64 to ensure ffmpeg compatibility
218 #compare audio_br to max_audio_br and return lowest
219 if tsn
and config
.has_section('_tivo_' + tsn
):
221 audiobr
= int(max(int(strtod(config
.get('_tivo_' + tsn
, 'audio_br'))/1000), 64)/64)*64
222 return str(min(audiobr
, getMaxAudioBR(tsn
))) + 'k'
223 except NoOptionError
:
226 audiobr
= int(max(int(strtod(config
.get('Server', 'audio_br'))/1000), 64)/64)*64
227 return str(min(audiobr
, getMaxAudioBR(tsn
))) + 'k'
228 except NoOptionError
:
229 return str(min(384, getMaxAudioBR(tsn
))) + 'k'
231 def getVideoBR(tsn
= None):
232 if tsn
and config
.has_section('_tivo_' + tsn
):
234 return config
.get('_tivo_' + tsn
, 'video_br')
235 except NoOptionError
:
238 return config
.get('Server', 'video_br')
239 except NoOptionError
: #defaults for S3/S2 TiVo
247 return str(int(strtod(config
.get('Server', 'max_video_br'))/1000)) + 'k'
248 except NoOptionError
: #default to 17Mi
253 return str(int(strtod(config
.get('Server', 'bufsize'))))
254 except NoOptionError
: #default 1024k
257 def getMaxAudioBR(tsn
= None):
258 #convert to non-zero multiple of 64 for ffmpeg compatibility
259 if tsn
and config
.has_section('_tivo_' + tsn
):
261 return int(int(strtod(config
.get('_tivo_' + tsn
, 'max_audio_br'))/1000)/64)*64
262 except NoOptionError
:
265 return int(int(strtod(config
.get('Server', 'max_audio_br'))/1000)/64)*64
266 except NoOptionError
:
267 return int(448) #default to 448
269 def getAudioCodec(tsn
= None):
270 if tsn
and config
.has_section('_tivo_' + tsn
):
272 return config
.get('_tivo_' + tsn
, 'audio_codec')
273 except NoOptionError
:
276 return config
.get('Server', 'audio_codec')
277 except NoOptionError
:
280 def getAudioCH(tsn
= None):
281 if tsn
and config
.has_section('_tivo_' + tsn
):
283 return config
.get('_tivo_' + tsn
, 'audio_ch')
284 except NoOptionError
:
287 return config
.get('Server', 'audio_ch')
288 except NoOptionError
:
291 def getAudioFR(tsn
= None):
292 if tsn
and config
.has_section('_tivo_' + tsn
):
294 return config
.get('_tivo_' + tsn
, 'audio_fr')
295 except NoOptionError
:
298 return config
.get('Server', 'audio_fr')
299 except NoOptionError
:
302 def getVideoFPS(tsn
= None):
303 if tsn
and config
.has_section('_tivo_' + tsn
):
305 return config
.get('_tivo_' + tsn
, 'video_fps')
306 except NoOptionError
:
309 return config
.get('Server', 'video_fps')
310 except NoOptionError
:
313 def getVideoCodec(tsn
= None):
314 if tsn
and config
.has_section('_tivo_' + tsn
):
316 return config
.get('_tivo_' + tsn
, 'video_codec')
317 except NoOptionError
:
320 return config
.get('Server', 'video_codec')
321 except NoOptionError
:
324 def getFormat(tsn
= None):
325 if tsn
and config
.has_section('_tivo_' + tsn
):
327 return config
.get('_tivo_' + tsn
, 'force_format')
328 except NoOptionError
:
331 return config
.get('Server', 'force_format')
332 except NoOptionError
:
337 L
= [x
.strip() for x
in items
]
340 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
341 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
342 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
344 prefixes
= {'y': -24, 'z': -21, 'a': -18, 'f': -15, 'p': -12,
345 'n': -9, 'u': -6, 'm': -3, 'c': -2, 'd': -1,
346 'h': 2, 'k': 3, 'K': 3, 'M': 6, 'G': 9,
347 'T': 12, 'P': 15, 'E': 18, 'Z': 21, 'Y': 24}
348 p
= re
.compile(r
'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
351 raise SyntaxError('Invalid bit value syntax')
352 (coef
, prefix
, power
, byte
) = m
.groups()
356 exponent
= float(prefixes
[prefix
])
359 value
= float(coef
) * pow(2.0, exponent
/ 0.3)
362 value
= float(coef
) * pow(10.0, exponent
)
363 if byte
== 'B': # B == Byte, b == bit