2 # -*- coding: utf-8 -*-
17 import cStringIO as StringIO
22 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20100101 Firefox/10.0',
23 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
24 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
25 'Accept-Encoding': 'gzip, deflate',
26 'Accept-Language': 'en-us,en;q=0.5',
30 compat_str = unicode # Python 2
34 def preferredencoding():
35 """Get preferred encoding.
37 Returns the best encoding scheme for the system, based on
38 locale.getpreferredencoding() and some further tweaks.
40 def yield_preferredencoding():
42 pref = locale.getpreferredencoding()
48 return yield_preferredencoding().next()
51 def htmlentity_transform(matchobj):
52 """Transforms an HTML entity to a Unicode character.
54 This function receives a match object and is intended to be used with
55 the re.sub() function.
57 entity = matchobj.group(1)
59 # Known non-numeric HTML entity
60 if entity in htmlentitydefs.name2codepoint:
61 return unichr(htmlentitydefs.name2codepoint[entity])
64 mobj = re.match(ur'(?u)#(x?\d+)', entity)
66 numstr = mobj.group(1)
67 if numstr.startswith(u'x'):
69 numstr = u'0%s' % numstr
72 return unichr(long(numstr, base))
74 # Unknown entity in name, return its literal representation
75 return (u'&%s;' % entity)
77 HTMLParser.locatestarttagend = re.compile(r"""<[a-zA-Z][-.a-zA-Z0-9:_]*(?:\s+(?:(?<=['"\s])[^\s/>][^\s/=>]*(?:\s*=+\s*(?:'[^']*'|"[^"]*"|(?!['"])[^>\s]*))?\s*)*)?\s*""", re.VERBOSE) # backport bugfix
78 class IDParser(HTMLParser.HTMLParser):
79 """Modified HTMLParser that isolates a tag with the specified id"""
80 def __init__(self, id):
86 self.watch_startpos = False
88 HTMLParser.HTMLParser.__init__(self)
90 def error(self, message):
91 if self.error_count > 10 or self.started:
92 raise HTMLParser.HTMLParseError(message, self.getpos())
93 self.rawdata = '\n'.join(self.html.split('\n')[self.getpos()[0]:]) # skip one line
97 def loads(self, html):
102 def handle_starttag(self, tag, attrs):
105 self.find_startpos(None)
106 if 'id' in attrs and attrs['id'] == self.id:
109 self.watch_startpos = True
111 if not tag in self.depth: self.depth[tag] = 0
114 def handle_endtag(self, tag):
116 if tag in self.depth: self.depth[tag] -= 1
117 if self.depth[self.result[0]] == 0:
119 self.result.append(self.getpos())
121 def find_startpos(self, x):
122 """Needed to put the start position of the result (self.result[1])
123 after the opening tag with the requested id"""
124 if self.watch_startpos:
125 self.watch_startpos = False
126 self.result.append(self.getpos())
127 handle_entityref = handle_charref = handle_data = handle_comment = \
128 handle_decl = handle_pi = unknown_decl = find_startpos
130 def get_result(self):
131 if self.result == None: return None
132 if len(self.result) != 3: return None
133 lines = self.html.split('\n')
134 lines = lines[self.result[1][0]-1:self.result[2][0]]
135 lines[0] = lines[0][self.result[1][1]:]
137 lines[-1] = lines[-1][:self.result[2][1]-self.result[1][1]]
138 lines[-1] = lines[-1][:self.result[2][1]]
139 return '\n'.join(lines).strip()
141 def get_element_by_id(id, html):
142 """Return the content of the tag with the specified id in the passed HTML document"""
143 parser = IDParser(id)
146 except HTMLParser.HTMLParseError:
148 return parser.get_result()
151 def clean_html(html):
152 """Clean an HTML snippet into a readable string"""
154 html = html.replace('\n', ' ')
155 html = re.sub('\s*<\s*br\s*/?\s*>\s*', '\n', html)
157 html = re.sub('<.*?>', '', html)
158 # Replace html entities
159 html = unescapeHTML(html)
163 def sanitize_open(filename, open_mode):
164 """Try to open the given filename, and slightly tweak it if this fails.
166 Attempts to open the given filename. If this fails, it tries to change
167 the filename slightly, step by step, until it's either able to open it
168 or it fails and raises a final exception, like the standard open()
171 It returns the tuple (stream, definitive_file_name).
175 if sys.platform == 'win32':
177 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
178 return (sys.stdout, filename)
179 stream = open(encodeFilename(filename), open_mode)
180 return (stream, filename)
181 except (IOError, OSError), err:
182 # In case of error, try to remove win32 forbidden chars
183 filename = re.sub(ur'[/<>:"\|\?\*]', u'#', filename)
185 # An exception here should be caught in the caller
186 stream = open(encodeFilename(filename), open_mode)
187 return (stream, filename)
190 def timeconvert(timestr):
191 """Convert RFC 2822 defined time string into system timestamp"""
193 timetuple = email.utils.parsedate_tz(timestr)
194 if timetuple is not None:
195 timestamp = email.utils.mktime_tz(timetuple)
198 def sanitize_filename(s, restricted=False):
199 """Sanitizes a string so it could be used as part of a filename.
200 If restricted is set, use a stricter subset of allowed characters.
202 def replace_insane(char):
203 if char == '?' or ord(char) < 32 or ord(char) == 127:
206 return '' if restricted else '\''
208 return '_-' if restricted else ' -'
209 elif char in '\\/|*<>':
211 if restricted and (char in '&\'' or char.isspace()):
215 result = u''.join(map(replace_insane, s))
216 while '--' in result:
217 result = result.replace('--', '-')
218 return result.strip('-')
220 def orderedSet(iterable):
221 """ Remove all duplicates from the input iterable """
230 @param s a string (of type unicode)
232 assert type(s) == type(u'')
234 result = re.sub(ur'(?u)&(.+?);', htmlentity_transform, s)
237 def encodeFilename(s):
239 @param s The name of the file (of type unicode)
242 assert type(s) == type(u'')
244 if sys.platform == 'win32' and sys.getwindowsversion()[0] >= 5:
245 # Pass u'' directly to use Unicode APIs on Windows 2000 and up
246 # (Detecting Windows NT 4 is tricky because 'major >= 4' would
247 # match Windows 9x series as well. Besides, NT 4 is obsolete.)
250 return s.encode(sys.getfilesystemencoding(), 'ignore')
252 class DownloadError(Exception):
253 """Download Error exception.
255 This exception may be thrown by FileDownloader objects if they are not
256 configured to continue on errors. They will contain the appropriate
262 class SameFileError(Exception):
263 """Same File exception.
265 This exception will be thrown by FileDownloader objects if they detect
266 multiple files would have to be downloaded to the same file on disk.
271 class PostProcessingError(Exception):
272 """Post Processing exception.
274 This exception may be raised by PostProcessor's .run() method to
275 indicate an error in the postprocessing task.
279 class MaxDownloadsReached(Exception):
280 """ --max-downloads limit has been reached. """
284 class UnavailableVideoError(Exception):
285 """Unavailable Format exception.
287 This exception will be thrown when a video is requested
288 in a format that is not available for that video.
293 class ContentTooShortError(Exception):
294 """Content Too Short exception.
296 This exception may be raised by FileDownloader objects when a file they
297 download is too small for what the server announced first, indicating
298 the connection was probably interrupted.
304 def __init__(self, downloaded, expected):
305 self.downloaded = downloaded
306 self.expected = expected
309 class Trouble(Exception):
310 """Trouble helper exception
312 This is an exception to be handled with
313 FileDownloader.trouble
316 class YoutubeDLHandler(urllib2.HTTPHandler):
317 """Handler for HTTP requests and responses.
319 This class, when installed with an OpenerDirector, automatically adds
320 the standard headers to every HTTP request and handles gzipped and
321 deflated responses from web servers. If compression is to be avoided in
322 a particular request, the original request in the program code only has
323 to include the HTTP header "Youtubedl-No-Compression", which will be
324 removed before making the real request.
326 Part of this code was copied from:
328 http://techknack.net/python-urllib2-handlers/
330 Andrew Rowls, the author of that code, agreed to release it to the
337 return zlib.decompress(data, -zlib.MAX_WBITS)
339 return zlib.decompress(data)
342 def addinfourl_wrapper(stream, headers, url, code):
343 if hasattr(urllib2.addinfourl, 'getcode'):
344 return urllib2.addinfourl(stream, headers, url, code)
345 ret = urllib2.addinfourl(stream, headers, url)
349 def http_request(self, req):
350 for h in std_headers:
353 req.add_header(h, std_headers[h])
354 if 'Youtubedl-no-compression' in req.headers:
355 if 'Accept-encoding' in req.headers:
356 del req.headers['Accept-encoding']
357 del req.headers['Youtubedl-no-compression']
360 def http_response(self, req, resp):
363 if resp.headers.get('Content-encoding', '') == 'gzip':
364 gz = gzip.GzipFile(fileobj=StringIO.StringIO(resp.read()), mode='r')
365 resp = self.addinfourl_wrapper(gz, old_resp.headers, old_resp.url, old_resp.code)
366 resp.msg = old_resp.msg
368 if resp.headers.get('Content-encoding', '') == 'deflate':
369 gz = StringIO.StringIO(self.deflate(resp.read()))
370 resp = self.addinfourl_wrapper(gz, old_resp.headers, old_resp.url, old_resp.code)
371 resp.msg = old_resp.msg