Merge branch 'master' into subfolders-8.3
[pyTivo.git] / config.py
blobdce3b22ac97bc2fd0f25b18fb620002bd91e114c
1 import ConfigParser, os
2 import re
3 from ConfigParser import NoOptionError
5 BLACKLIST_169 = ('540', '649')
7 config = ConfigParser.ConfigParser()
8 p = os.path.dirname(__file__)
9 config.read(os.path.join(p, 'pyTivo.conf'))
11 def getGUID():
12 if config.has_option('Server', 'GUID'):
13 guid = config.get('Server', 'GUID')
14 else:
15 guid = '123456'
16 return guid
18 def getBeaconAddreses():
19 if config.has_option('Server', 'beacon'):
20 beacon_ips = config.get('Server', 'beacon')
21 else:
22 beacon_ips = '255.255.255.255'
23 return beacon_ips
25 def getPort():
26 return config.get('Server', 'Port')
28 def get169Setting(tsn):
29 if not tsn:
30 return True
32 if config.has_section('_tivo_' + tsn):
33 if config.has_option('_tivo_' + tsn, 'aspect169'):
34 if config.get('_tivo_' + tsn, 'aspect169').lower() == 'true':
35 return True
36 else:
37 return False
39 if tsn[:3] in BLACKLIST_169:
40 return False
42 return True
44 def getShares():
45 shares = [ (section, dict(config.items(section))) for section in config.sections() if not(section.startswith('_tivo_') or section == 'Server') ]
47 for name, data in shares:
48 if not data.get('auto_subshares', 'False').lower() == 'true':
49 continue
51 base_path = data['path']
52 for item in os.listdir(base_path):
53 item_path = os.path.join(base_path, item)
54 if not os.path.isdir(item_path):
55 continue
57 new_name = name + '/' + item
58 new_data = dict(data)
59 new_data['path'] = item_path
61 shares.append( (new_name, new_data) )
63 return shares
66 def getDebug():
67 try:
68 debug = config.get('Server', 'debug')
69 if debug.lower() == 'true':
70 return True
71 else:
72 return False
73 except NoOptionError:
74 return False
76 def getHack83():
77 try:
78 debug = config.get('Server', 'hack83')
79 if debug.lower() == 'true':
80 return True
81 else:
82 return False
83 except NoOptionError:
84 return True
86 def getOptres():
87 try:
88 optres = config.get('Server', 'optres')
89 if optres.lower() == 'true':
90 return True
91 else:
92 return False
93 except NoOptionError:
94 return False
96 def get(section, key):
97 return config.get(section, key)
99 def getFFMPEGTemplate(tsn):
100 if tsn and config.has_section('_tivo_' + tsn):
101 try:
102 return config.get('_tivo_' + tsn, 'ffmpeg_prams', raw = True)
103 except NoOptionError:
104 pass
106 try:
107 return config.get('Server', 'ffmpeg_prams', raw = True)
108 except NoOptionError: #default
109 return '-vcodec mpeg2video -r 29.97 -b %(video_br)s -maxrate %(max_video_br)s -bufsize %(buff_size)s %(aspect_ratio)s -comment pyTivo.py -ac 2 -ab %(audio_br)s -ar 44100 -f vob -'
111 def getValidWidths():
112 return [1440, 720, 704, 544, 480, 352]
114 def getValidHeights():
115 return [720, 480] # Technically 240 is also supported
117 # Return the number in list that is nearest to x
118 # if two values are equidistant, return the larger
119 def nearest(x, list):
120 return reduce(lambda a, b: closest(x,a,b), list)
122 def closest(x,a, b):
123 if abs(x-a) < abs(x-b) or (abs(x-a) == abs(x-b)and a>b):
124 return a
125 else:
126 return b
128 def nearestTivoHeight(height):
129 return nearest(height, getValidHeights())
131 def nearestTivoWidth(width):
132 return nearest(width, getValidWidths())
134 def getTivoHeight(tsn):
135 if tsn and config.has_section('_tivo_' + tsn):
136 try:
137 height = int(config.get('_tivo_' + tsn, 'height'))
138 return nearest(height, getValidHeights())
139 except NoOptionError:
140 pass
142 try:
143 height = int(config.get('Server', 'height'))
144 return nearestTivoHeight(height)
145 except NoOptionError: #default
146 return 480
148 def getTivoWidth(tsn):
149 if tsn and config.has_section('_tivo_' + tsn):
150 try:
151 return config.get('_tivo_' + tsn, 'width')
152 except NoOptionError:
153 pass
155 try:
156 width = int(config.get('Server', 'width'))
157 return nearestTivoWidth(width)
158 except NoOptionError: #default
159 return 544
161 def getAudioBR(tsn = None):
162 if tsn and config.has_section('_tivo_' + tsn):
163 try:
164 return config.get('_tivo_' + tsn, 'audio_br')
165 except NoOptionError:
166 pass
168 try:
169 return config.get('Server', 'audio_br')
170 except NoOptionError: #default to 192
171 return '192K'
173 def getVideoBR(tsn = None):
174 if tsn and config.has_section('_tivo_' + tsn):
175 try:
176 return config.get('_tivo_' + tsn, 'video_br')
177 except NoOptionError:
178 pass
180 try:
181 return config.get('Server', 'video_br')
182 except NoOptionError: #default to 4096K
183 return '4096K'
185 def getMaxVideoBR():
186 try:
187 return config.get('Server', 'max_video_br')
188 except NoOptionError: #default to 17M
189 return '17408k'
191 def getBuffSize():
192 try:
193 return config.get('Server', 'bufsize')
194 except NoOptionError: #default 1024k
195 return '1024k'