You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

364 lines
10 KiB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import gzip
  4. import htmlentitydefs
  5. import HTMLParser
  6. import locale
  7. import os
  8. import re
  9. import sys
  10. import zlib
  11. import urllib2
  12. import email.utils
  13. import json
  14. try:
  15. import cStringIO as StringIO
  16. except ImportError:
  17. import StringIO
  18. std_headers = {
  19. 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20100101 Firefox/10.0',
  20. 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
  21. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  22. 'Accept-Encoding': 'gzip, deflate',
  23. 'Accept-Language': 'en-us,en;q=0.5',
  24. }
  25. def preferredencoding():
  26. """Get preferred encoding.
  27. Returns the best encoding scheme for the system, based on
  28. locale.getpreferredencoding() and some further tweaks.
  29. """
  30. def yield_preferredencoding():
  31. try:
  32. pref = locale.getpreferredencoding()
  33. u'TEST'.encode(pref)
  34. except:
  35. pref = 'UTF-8'
  36. while True:
  37. yield pref
  38. return yield_preferredencoding().next()
  39. def htmlentity_transform(matchobj):
  40. """Transforms an HTML entity to a Unicode character.
  41. This function receives a match object and is intended to be used with
  42. the re.sub() function.
  43. """
  44. entity = matchobj.group(1)
  45. # Known non-numeric HTML entity
  46. if entity in htmlentitydefs.name2codepoint:
  47. return unichr(htmlentitydefs.name2codepoint[entity])
  48. # Unicode character
  49. mobj = re.match(ur'(?u)#(x?\d+)', entity)
  50. if mobj is not None:
  51. numstr = mobj.group(1)
  52. if numstr.startswith(u'x'):
  53. base = 16
  54. numstr = u'0%s' % numstr
  55. else:
  56. base = 10
  57. return unichr(long(numstr, base))
  58. # Unknown entity in name, return its literal representation
  59. return (u'&%s;' % entity)
  60. 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
  61. class IDParser(HTMLParser.HTMLParser):
  62. """Modified HTMLParser that isolates a tag with the specified id"""
  63. def __init__(self, id):
  64. self.id = id
  65. self.result = None
  66. self.started = False
  67. self.depth = {}
  68. self.html = None
  69. self.watch_startpos = False
  70. self.error_count = 0
  71. HTMLParser.HTMLParser.__init__(self)
  72. def error(self, message):
  73. #print >> sys.stderr, self.getpos()
  74. if self.error_count > 10 or self.started:
  75. raise HTMLParser.HTMLParseError(message, self.getpos())
  76. self.rawdata = '\n'.join(self.html.split('\n')[self.getpos()[0]:]) # skip one line
  77. self.error_count += 1
  78. self.goahead(1)
  79. def loads(self, html):
  80. self.html = html
  81. self.feed(html)
  82. self.close()
  83. def handle_starttag(self, tag, attrs):
  84. attrs = dict(attrs)
  85. if self.started:
  86. self.find_startpos(None)
  87. if 'id' in attrs and attrs['id'] == self.id:
  88. self.result = [tag]
  89. self.started = True
  90. self.watch_startpos = True
  91. if self.started:
  92. if not tag in self.depth: self.depth[tag] = 0
  93. self.depth[tag] += 1
  94. def handle_endtag(self, tag):
  95. if self.started:
  96. if tag in self.depth: self.depth[tag] -= 1
  97. if self.depth[self.result[0]] == 0:
  98. self.started = False
  99. self.result.append(self.getpos())
  100. def find_startpos(self, x):
  101. """Needed to put the start position of the result (self.result[1])
  102. after the opening tag with the requested id"""
  103. if self.watch_startpos:
  104. self.watch_startpos = False
  105. self.result.append(self.getpos())
  106. handle_entityref = handle_charref = handle_data = handle_comment = \
  107. handle_decl = handle_pi = unknown_decl = find_startpos
  108. def get_result(self):
  109. if self.result == None: return None
  110. if len(self.result) != 3: return None
  111. lines = self.html.split('\n')
  112. lines = lines[self.result[1][0]-1:self.result[2][0]]
  113. lines[0] = lines[0][self.result[1][1]:]
  114. if len(lines) == 1:
  115. lines[-1] = lines[-1][:self.result[2][1]-self.result[1][1]]
  116. lines[-1] = lines[-1][:self.result[2][1]]
  117. return '\n'.join(lines).strip()
  118. def get_element_by_id(id, html):
  119. """Return the content of the tag with the specified id in the passed HTML document"""
  120. parser = IDParser(id)
  121. try:
  122. parser.loads(html)
  123. except HTMLParser.HTMLParseError:
  124. pass
  125. return parser.get_result()
  126. def clean_html(html):
  127. """Clean an HTML snippet into a readable string"""
  128. # Newline vs <br />
  129. html = html.replace('\n', ' ')
  130. html = re.sub('\s*<\s*br\s*/?\s*>\s*', '\n', html)
  131. # Strip html tags
  132. html = re.sub('<.*?>', '', html)
  133. # Replace html entities
  134. html = unescapeHTML(html)
  135. return html
  136. def sanitize_open(filename, open_mode):
  137. """Try to open the given filename, and slightly tweak it if this fails.
  138. Attempts to open the given filename. If this fails, it tries to change
  139. the filename slightly, step by step, until it's either able to open it
  140. or it fails and raises a final exception, like the standard open()
  141. function.
  142. It returns the tuple (stream, definitive_file_name).
  143. """
  144. try:
  145. if filename == u'-':
  146. if sys.platform == 'win32':
  147. import msvcrt
  148. msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
  149. return (sys.stdout, filename)
  150. stream = open(encodeFilename(filename), open_mode)
  151. return (stream, filename)
  152. except (IOError, OSError), err:
  153. # In case of error, try to remove win32 forbidden chars
  154. filename = re.sub(ur'[/<>:"\|\?\*]', u'#', filename)
  155. # An exception here should be caught in the caller
  156. stream = open(encodeFilename(filename), open_mode)
  157. return (stream, filename)
  158. def timeconvert(timestr):
  159. """Convert RFC 2822 defined time string into system timestamp"""
  160. timestamp = None
  161. timetuple = email.utils.parsedate_tz(timestr)
  162. if timetuple is not None:
  163. timestamp = email.utils.mktime_tz(timetuple)
  164. return timestamp
  165. def sanitize_filename(s):
  166. """Sanitizes a string so it could be used as part of a filename."""
  167. def replace_insane(char):
  168. if char == '?' or ord(char) < 32 or ord(char) == 127:
  169. return ''
  170. elif char == '"':
  171. return '\''
  172. elif char == ':':
  173. return ' -'
  174. elif char in '\\/|*<>':
  175. return '-'
  176. return char
  177. result = u''.join(map(replace_insane, s))
  178. while '--' in result:
  179. result = result.replace('--', '-')
  180. return result.strip('-')
  181. def orderedSet(iterable):
  182. """ Remove all duplicates from the input iterable """
  183. res = []
  184. for el in iterable:
  185. if el not in res:
  186. res.append(el)
  187. return res
  188. def unescapeHTML(s):
  189. """
  190. @param s a string (of type unicode)
  191. """
  192. assert type(s) == type(u'')
  193. result = re.sub(ur'(?u)&(.+?);', htmlentity_transform, s)
  194. return result
  195. def encodeFilename(s):
  196. """
  197. @param s The name of the file (of type unicode)
  198. """
  199. assert type(s) == type(u'')
  200. if sys.platform == 'win32' and sys.getwindowsversion()[0] >= 5:
  201. # Pass u'' directly to use Unicode APIs on Windows 2000 and up
  202. # (Detecting Windows NT 4 is tricky because 'major >= 4' would
  203. # match Windows 9x series as well. Besides, NT 4 is obsolete.)
  204. return s
  205. else:
  206. return s.encode(sys.getfilesystemencoding(), 'ignore')
  207. class DownloadError(Exception):
  208. """Download Error exception.
  209. This exception may be thrown by FileDownloader objects if they are not
  210. configured to continue on errors. They will contain the appropriate
  211. error message.
  212. """
  213. pass
  214. class SameFileError(Exception):
  215. """Same File exception.
  216. This exception will be thrown by FileDownloader objects if they detect
  217. multiple files would have to be downloaded to the same file on disk.
  218. """
  219. pass
  220. class PostProcessingError(Exception):
  221. """Post Processing exception.
  222. This exception may be raised by PostProcessor's .run() method to
  223. indicate an error in the postprocessing task.
  224. """
  225. pass
  226. class MaxDownloadsReached(Exception):
  227. """ --max-downloads limit has been reached. """
  228. pass
  229. class UnavailableVideoError(Exception):
  230. """Unavailable Format exception.
  231. This exception will be thrown when a video is requested
  232. in a format that is not available for that video.
  233. """
  234. pass
  235. class ContentTooShortError(Exception):
  236. """Content Too Short exception.
  237. This exception may be raised by FileDownloader objects when a file they
  238. download is too small for what the server announced first, indicating
  239. the connection was probably interrupted.
  240. """
  241. # Both in bytes
  242. downloaded = None
  243. expected = None
  244. def __init__(self, downloaded, expected):
  245. self.downloaded = downloaded
  246. self.expected = expected
  247. class Trouble(Exception):
  248. """Trouble helper exception
  249. This is an exception to be handled with
  250. FileDownloader.trouble
  251. """
  252. class YoutubeDLHandler(urllib2.HTTPHandler):
  253. """Handler for HTTP requests and responses.
  254. This class, when installed with an OpenerDirector, automatically adds
  255. the standard headers to every HTTP request and handles gzipped and
  256. deflated responses from web servers. If compression is to be avoided in
  257. a particular request, the original request in the program code only has
  258. to include the HTTP header "Youtubedl-No-Compression", which will be
  259. removed before making the real request.
  260. Part of this code was copied from:
  261. http://techknack.net/python-urllib2-handlers/
  262. Andrew Rowls, the author of that code, agreed to release it to the
  263. public domain.
  264. """
  265. @staticmethod
  266. def deflate(data):
  267. try:
  268. return zlib.decompress(data, -zlib.MAX_WBITS)
  269. except zlib.error:
  270. return zlib.decompress(data)
  271. @staticmethod
  272. def addinfourl_wrapper(stream, headers, url, code):
  273. if hasattr(urllib2.addinfourl, 'getcode'):
  274. return urllib2.addinfourl(stream, headers, url, code)
  275. ret = urllib2.addinfourl(stream, headers, url)
  276. ret.code = code
  277. return ret
  278. def http_request(self, req):
  279. for h in std_headers:
  280. if h in req.headers:
  281. del req.headers[h]
  282. req.add_header(h, std_headers[h])
  283. if 'Youtubedl-no-compression' in req.headers:
  284. if 'Accept-encoding' in req.headers:
  285. del req.headers['Accept-encoding']
  286. del req.headers['Youtubedl-no-compression']
  287. return req
  288. def http_response(self, req, resp):
  289. old_resp = resp
  290. # gzip
  291. if resp.headers.get('Content-encoding', '') == 'gzip':
  292. gz = gzip.GzipFile(fileobj=StringIO.StringIO(resp.read()), mode='r')
  293. resp = self.addinfourl_wrapper(gz, old_resp.headers, old_resp.url, old_resp.code)
  294. resp.msg = old_resp.msg
  295. # deflate
  296. if resp.headers.get('Content-encoding', '') == 'deflate':
  297. gz = StringIO.StringIO(self.deflate(resp.read()))
  298. resp = self.addinfourl_wrapper(gz, old_resp.headers, old_resp.url, old_resp.code)
  299. resp.msg = old_resp.msg
  300. return resp