]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/InfoExtractors.py
use _search_regex in GenericIE
[youtube-dl.git] / youtube_dl / InfoExtractors.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 from __future__ import absolute_import
5
6 import base64
7 import datetime
8 import itertools
9 import netrc
10 import os
11 import re
12 import socket
13 import time
14 import email.utils
15 import xml.etree.ElementTree
16 import random
17 import math
18 import operator
19 import hashlib
20 import binascii
21 import urllib
22
23 from .utils import *
24
25
26 class InfoExtractor(object):
27     """Information Extractor class.
28
29     Information extractors are the classes that, given a URL, extract
30     information about the video (or videos) the URL refers to. This
31     information includes the real video URL, the video title, author and
32     others. The information is stored in a dictionary which is then
33     passed to the FileDownloader. The FileDownloader processes this
34     information possibly downloading the video to the file system, among
35     other possible outcomes.
36
37     The dictionaries must include the following fields:
38
39     id:             Video identifier.
40     url:            Final video URL.
41     title:          Video title, unescaped.
42     ext:            Video filename extension.
43
44     The following fields are optional:
45
46     format:         The video format, defaults to ext (used for --get-format)
47     thumbnail:      Full URL to a video thumbnail image.
48     description:    One-line video description.
49     uploader:       Full name of the video uploader.
50     upload_date:    Video upload date (YYYYMMDD).
51     uploader_id:    Nickname or id of the video uploader.
52     location:       Physical location of the video.
53     player_url:     SWF Player URL (used for rtmpdump).
54     subtitles:      The subtitle file contents.
55     urlhandle:      [internal] The urlHandle to be used to download the file,
56                     like returned by urllib.request.urlopen
57
58     The fields should all be Unicode strings.
59
60     Subclasses of this one should re-define the _real_initialize() and
61     _real_extract() methods and define a _VALID_URL regexp.
62     Probably, they should also be added to the list of extractors.
63
64     _real_extract() must return a *list* of information dictionaries as
65     described above.
66
67     Finally, the _WORKING attribute should be set to False for broken IEs
68     in order to warn the users and skip the tests.
69     """
70
71     _ready = False
72     _downloader = None
73     _WORKING = True
74
75     def __init__(self, downloader=None):
76         """Constructor. Receives an optional downloader."""
77         self._ready = False
78         self.set_downloader(downloader)
79
80     @classmethod
81     def suitable(cls, url):
82         """Receives a URL and returns True if suitable for this IE."""
83         return re.match(cls._VALID_URL, url) is not None
84
85     @classmethod
86     def working(cls):
87         """Getter method for _WORKING."""
88         return cls._WORKING
89
90     def initialize(self):
91         """Initializes an instance (authentication, etc)."""
92         if not self._ready:
93             self._real_initialize()
94             self._ready = True
95
96     def extract(self, url):
97         """Extracts URL information and returns it in list of dicts."""
98         self.initialize()
99         return self._real_extract(url)
100
101     def set_downloader(self, downloader):
102         """Sets the downloader for this IE."""
103         self._downloader = downloader
104
105     def _real_initialize(self):
106         """Real initialization process. Redefine in subclasses."""
107         pass
108
109     def _real_extract(self, url):
110         """Real extraction process. Redefine in subclasses."""
111         pass
112
113     @property
114     def IE_NAME(self):
115         return type(self).__name__[:-2]
116
117     def _request_webpage(self, url_or_request, video_id, note=None, errnote=None):
118         """ Returns the response handle """
119         if note is None:
120             self.report_download_webpage(video_id)
121         elif note is not False:
122             self.to_screen(u'%s: %s' % (video_id, note))
123         try:
124             return compat_urllib_request.urlopen(url_or_request)
125         except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
126             if errnote is None:
127                 errnote = u'Unable to download webpage'
128             raise ExtractorError(u'%s: %s' % (errnote, compat_str(err)), sys.exc_info()[2])
129
130     def _download_webpage_handle(self, url_or_request, video_id, note=None, errnote=None):
131         """ Returns a tuple (page content as string, URL handle) """
132         urlh = self._request_webpage(url_or_request, video_id, note, errnote)
133         content_type = urlh.headers.get('Content-Type', '')
134         m = re.match(r'[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+\s*;\s*charset=(.+)', content_type)
135         if m:
136             encoding = m.group(1)
137         else:
138             encoding = 'utf-8'
139         webpage_bytes = urlh.read()
140         if self._downloader.params.get('dump_intermediate_pages', False):
141             try:
142                 url = url_or_request.get_full_url()
143             except AttributeError:
144                 url = url_or_request
145             self.to_screen(u'Dumping request to ' + url)
146             dump = base64.b64encode(webpage_bytes).decode('ascii')
147             self._downloader.to_screen(dump)
148         content = webpage_bytes.decode(encoding, 'replace')
149         return (content, urlh)
150
151     def _download_webpage(self, url_or_request, video_id, note=None, errnote=None):
152         """ Returns the data of the page as a string """
153         return self._download_webpage_handle(url_or_request, video_id, note, errnote)[0]
154
155     def to_screen(self, msg):
156         """Print msg to screen, prefixing it with '[ie_name]'"""
157         self._downloader.to_screen(u'[%s] %s' % (self.IE_NAME, msg))
158
159     def report_extraction(self, id_or_name):
160         """Report information extraction."""
161         self.to_screen(u'%s: Extracting information' % id_or_name)
162
163     def report_download_webpage(self, video_id):
164         """Report webpage download."""
165         self.to_screen(u'%s: Downloading webpage' % video_id)
166
167     def report_age_confirmation(self):
168         """Report attempt to confirm age."""
169         self.to_screen(u'Confirming age')
170
171     #Methods for following #608
172     #They set the correct value of the '_type' key
173     def video_result(self, video_info):
174         """Returns a video"""
175         video_info['_type'] = 'video'
176         return video_info
177     def url_result(self, url, ie=None):
178         """Returns a url that points to a page that should be processed"""
179         #TODO: ie should be the class used for getting the info
180         video_info = {'_type': 'url',
181                       'url': url,
182                       'ie_key': ie}
183         return video_info
184     def playlist_result(self, entries, playlist_id=None, playlist_title=None):
185         """Returns a playlist"""
186         video_info = {'_type': 'playlist',
187                       'entries': entries}
188         if playlist_id:
189             video_info['id'] = playlist_id
190         if playlist_title:
191             video_info['title'] = playlist_title
192         return video_info
193
194     def _search_regex(self, pattern, string, name, default=None, fatal=True, flags=0):
195         """
196         Perform a regex search on the given string, using a single or a list of
197         patterns returning the first matching group.
198         In case of failure return a default value or raise a WARNING or a
199         ExtractorError, depending on fatal, specifying the field name.
200         """
201         if isinstance(pattern, (str, compat_str, compiled_regex_type)):
202             mobj = re.search(pattern, string, flags)
203         else:
204             for p in pattern:
205                 mobj = re.search(p, string, flags)
206                 if mobj: break
207
208         if sys.stderr.isatty() and os.name != 'nt':
209             _name = u'\033[0;34m%s\033[0m' % name
210         else:
211             _name = name
212
213         if mobj:
214             # return the first matching group
215             return next(g for g in mobj.groups() if g is not None)
216         elif default is not None:
217             return default
218         elif fatal:
219             raise ExtractorError(u'Unable to extract %s' % _name)
220         else:
221             self._downloader.report_warning(u'unable to extract %s; '
222                 u'please report this issue on GitHub.' % _name)
223             return None
224
225     def _html_search_regex(self, pattern, string, name, default=None, fatal=True, flags=0):
226         """
227         Like _search_regex, but strips HTML tags and unescapes entities.
228         """
229         res = self._search_regex(pattern, string, name, default, fatal, flags)
230         if res:
231             return clean_html(res).strip()
232         else:
233             return res
234
235 class SearchInfoExtractor(InfoExtractor):
236     """
237     Base class for paged search queries extractors.
238     They accept urls in the format _SEARCH_KEY(|all|[0-9]):{query}
239     Instances should define _SEARCH_KEY and _MAX_RESULTS.
240     """
241
242     @classmethod
243     def _make_valid_url(cls):
244         return r'%s(?P<prefix>|[1-9][0-9]*|all):(?P<query>[\s\S]+)' % cls._SEARCH_KEY
245
246     @classmethod
247     def suitable(cls, url):
248         return re.match(cls._make_valid_url(), url) is not None
249
250     def _real_extract(self, query):
251         mobj = re.match(self._make_valid_url(), query)
252         if mobj is None:
253             raise ExtractorError(u'Invalid search query "%s"' % query)
254
255         prefix = mobj.group('prefix')
256         query = mobj.group('query')
257         if prefix == '':
258             return self._get_n_results(query, 1)
259         elif prefix == 'all':
260             return self._get_n_results(query, self._MAX_RESULTS)
261         else:
262             n = int(prefix)
263             if n <= 0:
264                 raise ExtractorError(u'invalid download number %s for query "%s"' % (n, query))
265             elif n > self._MAX_RESULTS:
266                 self._downloader.report_warning(u'%s returns max %i results (you requested %i)' % (self._SEARCH_KEY, self._MAX_RESULTS, n))
267                 n = self._MAX_RESULTS
268             return self._get_n_results(query, n)
269
270     def _get_n_results(self, query, n):
271         """Get a specified number of results for a query"""
272         raise NotImplementedError("This method must be implemented by sublclasses")
273
274
275 class YoutubeIE(InfoExtractor):
276     """Information extractor for youtube.com."""
277
278     _VALID_URL = r"""^
279                      (
280                          (?:https?://)?                                       # http(s):// (optional)
281                          (?:youtu\.be/|(?:\w+\.)?youtube(?:-nocookie)?\.com/|
282                             tube\.majestyc\.net/)                             # the various hostnames, with wildcard subdomains
283                          (?:.*?\#/)?                                          # handle anchor (#/) redirect urls
284                          (?:                                                  # the various things that can precede the ID:
285                              (?:(?:v|embed|e)/)                               # v/ or embed/ or e/
286                              |(?:                                             # or the v= param in all its forms
287                                  (?:watch(?:_popup)?(?:\.php)?)?              # preceding watch(_popup|.php) or nothing (like /?v=xxxx)
288                                  (?:\?|\#!?)                                  # the params delimiter ? or # or #!
289                                  (?:.*?&)?                                    # any other preceding param (like /?s=tuff&v=xxxx)
290                                  v=
291                              )
292                          )?                                                   # optional -> youtube.com/xxxx is OK
293                      )?                                                       # all until now is optional -> you can pass the naked ID
294                      ([0-9A-Za-z_-]+)                                         # here is it! the YouTube video ID
295                      (?(1).+)?                                                # if we found the ID, everything can follow
296                      $"""
297     _LANG_URL = r'https://www.youtube.com/?hl=en&persist_hl=1&gl=US&persist_gl=1&opt_out_ackd=1'
298     _LOGIN_URL = 'https://accounts.google.com/ServiceLogin'
299     _AGE_URL = 'http://www.youtube.com/verify_age?next_url=/&gl=US&hl=en'
300     _NEXT_URL_RE = r'[\?&]next_url=([^&]+)'
301     _NETRC_MACHINE = 'youtube'
302     # Listed in order of quality
303     _available_formats = ['38', '37', '46', '22', '45', '35', '44', '34', '18', '43', '6', '5', '17', '13']
304     _available_formats_prefer_free = ['38', '46', '37', '45', '22', '44', '35', '43', '34', '18', '6', '5', '17', '13']
305     _video_extensions = {
306         '13': '3gp',
307         '17': 'mp4',
308         '18': 'mp4',
309         '22': 'mp4',
310         '37': 'mp4',
311         '38': 'video', # You actually don't know if this will be MOV, AVI or whatever
312         '43': 'webm',
313         '44': 'webm',
314         '45': 'webm',
315         '46': 'webm',
316     }
317     _video_dimensions = {
318         '5': '240x400',
319         '6': '???',
320         '13': '???',
321         '17': '144x176',
322         '18': '360x640',
323         '22': '720x1280',
324         '34': '360x640',
325         '35': '480x854',
326         '37': '1080x1920',
327         '38': '3072x4096',
328         '43': '360x640',
329         '44': '480x854',
330         '45': '720x1280',
331         '46': '1080x1920',
332     }
333     IE_NAME = u'youtube'
334
335     @classmethod
336     def suitable(cls, url):
337         """Receives a URL and returns True if suitable for this IE."""
338         if YoutubePlaylistIE.suitable(url): return False
339         return re.match(cls._VALID_URL, url, re.VERBOSE) is not None
340
341     def report_lang(self):
342         """Report attempt to set language."""
343         self.to_screen(u'Setting language')
344
345     def report_login(self):
346         """Report attempt to log in."""
347         self.to_screen(u'Logging in')
348
349     def report_video_webpage_download(self, video_id):
350         """Report attempt to download video webpage."""
351         self.to_screen(u'%s: Downloading video webpage' % video_id)
352
353     def report_video_info_webpage_download(self, video_id):
354         """Report attempt to download video info webpage."""
355         self.to_screen(u'%s: Downloading video info webpage' % video_id)
356
357     def report_video_subtitles_download(self, video_id):
358         """Report attempt to download video info webpage."""
359         self.to_screen(u'%s: Checking available subtitles' % video_id)
360
361     def report_video_subtitles_request(self, video_id, sub_lang, format):
362         """Report attempt to download video info webpage."""
363         self.to_screen(u'%s: Downloading video subtitles for %s.%s' % (video_id, sub_lang, format))
364
365     def report_video_subtitles_available(self, video_id, sub_lang_list):
366         """Report available subtitles."""
367         sub_lang = ",".join(list(sub_lang_list.keys()))
368         self.to_screen(u'%s: Available subtitles for video: %s' % (video_id, sub_lang))
369
370     def report_information_extraction(self, video_id):
371         """Report attempt to extract video information."""
372         self.to_screen(u'%s: Extracting video information' % video_id)
373
374     def report_unavailable_format(self, video_id, format):
375         """Report extracted video URL."""
376         self.to_screen(u'%s: Format %s not available' % (video_id, format))
377
378     def report_rtmp_download(self):
379         """Indicate the download will use the RTMP protocol."""
380         self.to_screen(u'RTMP download detected')
381
382     def _get_available_subtitles(self, video_id):
383         self.report_video_subtitles_download(video_id)
384         request = compat_urllib_request.Request('http://video.google.com/timedtext?hl=en&type=list&v=%s' % video_id)
385         try:
386             sub_list = compat_urllib_request.urlopen(request).read().decode('utf-8')
387         except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
388             return (u'unable to download video subtitles: %s' % compat_str(err), None)
389         sub_lang_list = re.findall(r'name="([^"]*)"[^>]+lang_code="([\w\-]+)"', sub_list)
390         sub_lang_list = dict((l[1], l[0]) for l in sub_lang_list)
391         if not sub_lang_list:
392             return (u'video doesn\'t have subtitles', None)
393         return sub_lang_list
394
395     def _list_available_subtitles(self, video_id):
396         sub_lang_list = self._get_available_subtitles(video_id)
397         self.report_video_subtitles_available(video_id, sub_lang_list)
398
399     def _request_subtitle(self, sub_lang, sub_name, video_id, format):
400         """
401         Return tuple:
402         (error_message, sub_lang, sub)
403         """
404         self.report_video_subtitles_request(video_id, sub_lang, format)
405         params = compat_urllib_parse.urlencode({
406             'lang': sub_lang,
407             'name': sub_name,
408             'v': video_id,
409             'fmt': format,
410         })
411         url = 'http://www.youtube.com/api/timedtext?' + params
412         try:
413             sub = compat_urllib_request.urlopen(url).read().decode('utf-8')
414         except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
415             return (u'unable to download video subtitles: %s' % compat_str(err), None, None)
416         if not sub:
417             return (u'Did not fetch video subtitles', None, None)
418         return (None, sub_lang, sub)
419
420     def _request_automatic_caption(self, video_id, webpage):
421         """We need the webpage for getting the captions url, pass it as an
422            argument to speed up the process."""
423         sub_lang = self._downloader.params.get('subtitleslang')
424         sub_format = self._downloader.params.get('subtitlesformat')
425         self.to_screen(u'%s: Looking for automatic captions' % video_id)
426         mobj = re.search(r';ytplayer.config = ({.*?});', webpage)
427         err_msg = u'Couldn\'t find automatic captions for "%s"' % sub_lang
428         if mobj is None:
429             return [(err_msg, None, None)]
430         player_config = json.loads(mobj.group(1))
431         try:
432             args = player_config[u'args']
433             caption_url = args[u'ttsurl']
434             timestamp = args[u'timestamp']
435             params = compat_urllib_parse.urlencode({
436                 'lang': 'en',
437                 'tlang': sub_lang,
438                 'fmt': sub_format,
439                 'ts': timestamp,
440                 'kind': 'asr',
441             })
442             subtitles_url = caption_url + '&' + params
443             sub = self._download_webpage(subtitles_url, video_id, u'Downloading automatic captions')
444             return [(None, sub_lang, sub)]
445         except KeyError:
446             return [(err_msg, None, None)]
447
448     def _extract_subtitle(self, video_id):
449         """
450         Return a list with a tuple:
451         [(error_message, sub_lang, sub)]
452         """
453         sub_lang_list = self._get_available_subtitles(video_id)
454         sub_format = self._downloader.params.get('subtitlesformat')
455         if  isinstance(sub_lang_list,tuple): #There was some error, it didn't get the available subtitles
456             return [(sub_lang_list[0], None, None)]
457         if self._downloader.params.get('subtitleslang', False):
458             sub_lang = self._downloader.params.get('subtitleslang')
459         elif 'en' in sub_lang_list:
460             sub_lang = 'en'
461         else:
462             sub_lang = list(sub_lang_list.keys())[0]
463         if not sub_lang in sub_lang_list:
464             return [(u'no closed captions found in the specified language "%s"' % sub_lang, None, None)]
465
466         subtitle = self._request_subtitle(sub_lang, sub_lang_list[sub_lang].encode('utf-8'), video_id, sub_format)
467         return [subtitle]
468
469     def _extract_all_subtitles(self, video_id):
470         sub_lang_list = self._get_available_subtitles(video_id)
471         sub_format = self._downloader.params.get('subtitlesformat')
472         if  isinstance(sub_lang_list,tuple): #There was some error, it didn't get the available subtitles
473             return [(sub_lang_list[0], None, None)]
474         subtitles = []
475         for sub_lang in sub_lang_list:
476             subtitle = self._request_subtitle(sub_lang, sub_lang_list[sub_lang].encode('utf-8'), video_id, sub_format)
477             subtitles.append(subtitle)
478         return subtitles
479
480     def _print_formats(self, formats):
481         print('Available formats:')
482         for x in formats:
483             print('%s\t:\t%s\t[%s]' %(x, self._video_extensions.get(x, 'flv'), self._video_dimensions.get(x, '???')))
484
485     def _real_initialize(self):
486         if self._downloader is None:
487             return
488
489         username = None
490         password = None
491         downloader_params = self._downloader.params
492
493         # Attempt to use provided username and password or .netrc data
494         if downloader_params.get('username', None) is not None:
495             username = downloader_params['username']
496             password = downloader_params['password']
497         elif downloader_params.get('usenetrc', False):
498             try:
499                 info = netrc.netrc().authenticators(self._NETRC_MACHINE)
500                 if info is not None:
501                     username = info[0]
502                     password = info[2]
503                 else:
504                     raise netrc.NetrcParseError('No authenticators for %s' % self._NETRC_MACHINE)
505             except (IOError, netrc.NetrcParseError) as err:
506                 self._downloader.report_warning(u'parsing .netrc: %s' % compat_str(err))
507                 return
508
509         # Set language
510         request = compat_urllib_request.Request(self._LANG_URL)
511         try:
512             self.report_lang()
513             compat_urllib_request.urlopen(request).read()
514         except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
515             self._downloader.report_warning(u'unable to set language: %s' % compat_str(err))
516             return
517
518         # No authentication to be performed
519         if username is None:
520             return
521
522         request = compat_urllib_request.Request(self._LOGIN_URL)
523         try:
524             login_page = compat_urllib_request.urlopen(request).read().decode('utf-8')
525         except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
526             self._downloader.report_warning(u'unable to fetch login page: %s' % compat_str(err))
527             return
528
529         galx = None
530         dsh = None
531         match = re.search(re.compile(r'<input.+?name="GALX".+?value="(.+?)"', re.DOTALL), login_page)
532         if match:
533           galx = match.group(1)
534
535         match = re.search(re.compile(r'<input.+?name="dsh".+?value="(.+?)"', re.DOTALL), login_page)
536         if match:
537           dsh = match.group(1)
538
539         # Log in
540         login_form_strs = {
541                 u'continue': u'https://www.youtube.com/signin?action_handle_signin=true&feature=sign_in_button&hl=en_US&nomobiletemp=1',
542                 u'Email': username,
543                 u'GALX': galx,
544                 u'Passwd': password,
545                 u'PersistentCookie': u'yes',
546                 u'_utf8': u'霱',
547                 u'bgresponse': u'js_disabled',
548                 u'checkConnection': u'',
549                 u'checkedDomains': u'youtube',
550                 u'dnConn': u'',
551                 u'dsh': dsh,
552                 u'pstMsg': u'0',
553                 u'rmShown': u'1',
554                 u'secTok': u'',
555                 u'signIn': u'Sign in',
556                 u'timeStmp': u'',
557                 u'service': u'youtube',
558                 u'uilel': u'3',
559                 u'hl': u'en_US',
560         }
561         # Convert to UTF-8 *before* urlencode because Python 2.x's urlencode
562         # chokes on unicode
563         login_form = dict((k.encode('utf-8'), v.encode('utf-8')) for k,v in login_form_strs.items())
564         login_data = compat_urllib_parse.urlencode(login_form).encode('ascii')
565         request = compat_urllib_request.Request(self._LOGIN_URL, login_data)
566         try:
567             self.report_login()
568             login_results = compat_urllib_request.urlopen(request).read().decode('utf-8')
569             if re.search(r'(?i)<form[^>]* id="gaia_loginform"', login_results) is not None:
570                 self._downloader.report_warning(u'unable to log in: bad username or password')
571                 return
572         except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
573             self._downloader.report_warning(u'unable to log in: %s' % compat_str(err))
574             return
575
576         # Confirm age
577         age_form = {
578                 'next_url':     '/',
579                 'action_confirm':   'Confirm',
580                 }
581         request = compat_urllib_request.Request(self._AGE_URL, compat_urllib_parse.urlencode(age_form))
582         try:
583             self.report_age_confirmation()
584             age_results = compat_urllib_request.urlopen(request).read().decode('utf-8')
585         except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
586             raise ExtractorError(u'Unable to confirm age: %s' % compat_str(err))
587
588     def _extract_id(self, url):
589         mobj = re.match(self._VALID_URL, url, re.VERBOSE)
590         if mobj is None:
591             raise ExtractorError(u'Invalid URL: %s' % url)
592         video_id = mobj.group(2)
593         return video_id
594
595     def _real_extract(self, url):
596         # Extract original video URL from URL with redirection, like age verification, using next_url parameter
597         mobj = re.search(self._NEXT_URL_RE, url)
598         if mobj:
599             url = 'https://www.youtube.com/' + compat_urllib_parse.unquote(mobj.group(1)).lstrip('/')
600         video_id = self._extract_id(url)
601
602         # Get video webpage
603         self.report_video_webpage_download(video_id)
604         url = 'https://www.youtube.com/watch?v=%s&gl=US&hl=en&has_verified=1' % video_id
605         request = compat_urllib_request.Request(url)
606         try:
607             video_webpage_bytes = compat_urllib_request.urlopen(request).read()
608         except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
609             raise ExtractorError(u'Unable to download video webpage: %s' % compat_str(err))
610
611         video_webpage = video_webpage_bytes.decode('utf-8', 'ignore')
612
613         # Attempt to extract SWF player URL
614         mobj = re.search(r'swfConfig.*?"(http:\\/\\/.*?watch.*?-.*?\.swf)"', video_webpage)
615         if mobj is not None:
616             player_url = re.sub(r'\\(.)', r'\1', mobj.group(1))
617         else:
618             player_url = None
619
620         # Get video info
621         self.report_video_info_webpage_download(video_id)
622         for el_type in ['&el=embedded', '&el=detailpage', '&el=vevo', '']:
623             video_info_url = ('https://www.youtube.com/get_video_info?&video_id=%s%s&ps=default&eurl=&gl=US&hl=en'
624                     % (video_id, el_type))
625             video_info_webpage = self._download_webpage(video_info_url, video_id,
626                                     note=False,
627                                     errnote='unable to download video info webpage')
628             video_info = compat_parse_qs(video_info_webpage)
629             if 'token' in video_info:
630                 break
631         if 'token' not in video_info:
632             if 'reason' in video_info:
633                 raise ExtractorError(u'YouTube said: %s' % video_info['reason'][0])
634             else:
635                 raise ExtractorError(u'"token" parameter not in video info for unknown reason')
636
637         # Check for "rental" videos
638         if 'ypc_video_rental_bar_text' in video_info and 'author' not in video_info:
639             raise ExtractorError(u'"rental" videos not supported')
640
641         # Start extracting information
642         self.report_information_extraction(video_id)
643
644         # uploader
645         if 'author' not in video_info:
646             raise ExtractorError(u'Unable to extract uploader name')
647         video_uploader = compat_urllib_parse.unquote_plus(video_info['author'][0])
648
649         # uploader_id
650         video_uploader_id = None
651         mobj = re.search(r'<link itemprop="url" href="http://www.youtube.com/(?:user|channel)/([^"]+)">', video_webpage)
652         if mobj is not None:
653             video_uploader_id = mobj.group(1)
654         else:
655             self._downloader.report_warning(u'unable to extract uploader nickname')
656
657         # title
658         if 'title' not in video_info:
659             raise ExtractorError(u'Unable to extract video title')
660         video_title = compat_urllib_parse.unquote_plus(video_info['title'][0])
661
662         # thumbnail image
663         if 'thumbnail_url' not in video_info:
664             self._downloader.report_warning(u'unable to extract video thumbnail')
665             video_thumbnail = ''
666         else:   # don't panic if we can't find it
667             video_thumbnail = compat_urllib_parse.unquote_plus(video_info['thumbnail_url'][0])
668
669         # upload date
670         upload_date = None
671         mobj = re.search(r'id="eow-date.*?>(.*?)</span>', video_webpage, re.DOTALL)
672         if mobj is not None:
673             upload_date = ' '.join(re.sub(r'[/,-]', r' ', mobj.group(1)).split())
674             upload_date = unified_strdate(upload_date)
675
676         # description
677         video_description = get_element_by_id("eow-description", video_webpage)
678         if video_description:
679             video_description = clean_html(video_description)
680         else:
681             fd_mobj = re.search(r'<meta name="description" content="([^"]+)"', video_webpage)
682             if fd_mobj:
683                 video_description = unescapeHTML(fd_mobj.group(1))
684             else:
685                 video_description = u''
686
687         # subtitles
688         video_subtitles = None
689
690         if self._downloader.params.get('writesubtitles', False):
691             video_subtitles = self._extract_subtitle(video_id)
692             if video_subtitles:
693                 (sub_error, sub_lang, sub) = video_subtitles[0]
694                 if sub_error:
695                     # We try with the automatic captions
696                     video_subtitles = self._request_automatic_caption(video_id, video_webpage)
697                     (sub_error_auto, sub_lang, sub) = video_subtitles[0]
698                     if sub is not None:
699                         pass
700                     else:
701                         # We report the original error
702                         self._downloader.report_error(sub_error)
703
704         if self._downloader.params.get('allsubtitles', False):
705             video_subtitles = self._extract_all_subtitles(video_id)
706             for video_subtitle in video_subtitles:
707                 (sub_error, sub_lang, sub) = video_subtitle
708                 if sub_error:
709                     self._downloader.report_error(sub_error)
710
711         if self._downloader.params.get('listsubtitles', False):
712             sub_lang_list = self._list_available_subtitles(video_id)
713             return
714
715         if 'length_seconds' not in video_info:
716             self._downloader.report_warning(u'unable to extract video duration')
717             video_duration = ''
718         else:
719             video_duration = compat_urllib_parse.unquote_plus(video_info['length_seconds'][0])
720
721         # token
722         video_token = compat_urllib_parse.unquote_plus(video_info['token'][0])
723
724         # Decide which formats to download
725         req_format = self._downloader.params.get('format', None)
726
727         if 'conn' in video_info and video_info['conn'][0].startswith('rtmp'):
728             self.report_rtmp_download()
729             video_url_list = [(None, video_info['conn'][0])]
730         elif 'url_encoded_fmt_stream_map' in video_info and len(video_info['url_encoded_fmt_stream_map']) >= 1:
731             url_map = {}
732             for url_data_str in video_info['url_encoded_fmt_stream_map'][0].split(','):
733                 url_data = compat_parse_qs(url_data_str)
734                 if 'itag' in url_data and 'url' in url_data:
735                     url = url_data['url'][0] + '&signature=' + url_data['sig'][0]
736                     if not 'ratebypass' in url: url += '&ratebypass=yes'
737                     url_map[url_data['itag'][0]] = url
738
739             format_limit = self._downloader.params.get('format_limit', None)
740             available_formats = self._available_formats_prefer_free if self._downloader.params.get('prefer_free_formats', False) else self._available_formats
741             if format_limit is not None and format_limit in available_formats:
742                 format_list = available_formats[available_formats.index(format_limit):]
743             else:
744                 format_list = available_formats
745             existing_formats = [x for x in format_list if x in url_map]
746             if len(existing_formats) == 0:
747                 raise ExtractorError(u'no known formats available for video')
748             if self._downloader.params.get('listformats', None):
749                 self._print_formats(existing_formats)
750                 return
751             if req_format is None or req_format == 'best':
752                 video_url_list = [(existing_formats[0], url_map[existing_formats[0]])] # Best quality
753             elif req_format == 'worst':
754                 video_url_list = [(existing_formats[len(existing_formats)-1], url_map[existing_formats[len(existing_formats)-1]])] # worst quality
755             elif req_format in ('-1', 'all'):
756                 video_url_list = [(f, url_map[f]) for f in existing_formats] # All formats
757             else:
758                 # Specific formats. We pick the first in a slash-delimeted sequence.
759                 # For example, if '1/2/3/4' is requested and '2' and '4' are available, we pick '2'.
760                 req_formats = req_format.split('/')
761                 video_url_list = None
762                 for rf in req_formats:
763                     if rf in url_map:
764                         video_url_list = [(rf, url_map[rf])]
765                         break
766                 if video_url_list is None:
767                     raise ExtractorError(u'requested format not available')
768         else:
769             raise ExtractorError(u'no conn or url_encoded_fmt_stream_map information found in video info')
770
771         results = []
772         for format_param, video_real_url in video_url_list:
773             # Extension
774             video_extension = self._video_extensions.get(format_param, 'flv')
775
776             video_format = '{0} - {1}'.format(format_param if format_param else video_extension,
777                                               self._video_dimensions.get(format_param, '???'))
778
779             results.append({
780                 'id':       video_id,
781                 'url':      video_real_url,
782                 'uploader': video_uploader,
783                 'uploader_id': video_uploader_id,
784                 'upload_date':  upload_date,
785                 'title':    video_title,
786                 'ext':      video_extension,
787                 'format':   video_format,
788                 'thumbnail':    video_thumbnail,
789                 'description':  video_description,
790                 'player_url':   player_url,
791                 'subtitles':    video_subtitles,
792                 'duration':     video_duration
793             })
794         return results
795
796
797 class MetacafeIE(InfoExtractor):
798     """Information Extractor for metacafe.com."""
799
800     _VALID_URL = r'(?:http://)?(?:www\.)?metacafe\.com/watch/([^/]+)/([^/]+)/.*'
801     _DISCLAIMER = 'http://www.metacafe.com/family_filter/'
802     _FILTER_POST = 'http://www.metacafe.com/f/index.php?inputType=filter&controllerGroup=user'
803     IE_NAME = u'metacafe'
804
805     def report_disclaimer(self):
806         """Report disclaimer retrieval."""
807         self.to_screen(u'Retrieving disclaimer')
808
809     def _real_initialize(self):
810         # Retrieve disclaimer
811         request = compat_urllib_request.Request(self._DISCLAIMER)
812         try:
813             self.report_disclaimer()
814             disclaimer = compat_urllib_request.urlopen(request).read()
815         except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
816             raise ExtractorError(u'Unable to retrieve disclaimer: %s' % compat_str(err))
817
818         # Confirm age
819         disclaimer_form = {
820             'filters': '0',
821             'submit': "Continue - I'm over 18",
822             }
823         request = compat_urllib_request.Request(self._FILTER_POST, compat_urllib_parse.urlencode(disclaimer_form))
824         try:
825             self.report_age_confirmation()
826             disclaimer = compat_urllib_request.urlopen(request).read()
827         except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
828             raise ExtractorError(u'Unable to confirm age: %s' % compat_str(err))
829
830     def _real_extract(self, url):
831         # Extract id and simplified title from URL
832         mobj = re.match(self._VALID_URL, url)
833         if mobj is None:
834             raise ExtractorError(u'Invalid URL: %s' % url)
835
836         video_id = mobj.group(1)
837
838         # Check if video comes from YouTube
839         mobj2 = re.match(r'^yt-(.*)$', video_id)
840         if mobj2 is not None:
841             return [self.url_result('http://www.youtube.com/watch?v=%s' % mobj2.group(1), 'Youtube')]
842
843         # Retrieve video webpage to extract further information
844         webpage = self._download_webpage('http://www.metacafe.com/watch/%s/' % video_id, video_id)
845
846         # Extract URL, uploader and title from webpage
847         self.report_extraction(video_id)
848         mobj = re.search(r'(?m)&mediaURL=([^&]+)', webpage)
849         if mobj is not None:
850             mediaURL = compat_urllib_parse.unquote(mobj.group(1))
851             video_extension = mediaURL[-3:]
852
853             # Extract gdaKey if available
854             mobj = re.search(r'(?m)&gdaKey=(.*?)&', webpage)
855             if mobj is None:
856                 video_url = mediaURL
857             else:
858                 gdaKey = mobj.group(1)
859                 video_url = '%s?__gda__=%s' % (mediaURL, gdaKey)
860         else:
861             mobj = re.search(r' name="flashvars" value="(.*?)"', webpage)
862             if mobj is None:
863                 raise ExtractorError(u'Unable to extract media URL')
864             vardict = compat_parse_qs(mobj.group(1))
865             if 'mediaData' not in vardict:
866                 raise ExtractorError(u'Unable to extract media URL')
867             mobj = re.search(r'"mediaURL":"(?P<mediaURL>http.*?)",(.*?)"key":"(?P<key>.*?)"', vardict['mediaData'][0])
868             if mobj is None:
869                 raise ExtractorError(u'Unable to extract media URL')
870             mediaURL = mobj.group('mediaURL').replace('\\/', '/')
871             video_extension = mediaURL[-3:]
872             video_url = '%s?__gda__=%s' % (mediaURL, mobj.group('key'))
873
874         mobj = re.search(r'(?im)<title>(.*) - Video</title>', webpage)
875         if mobj is None:
876             raise ExtractorError(u'Unable to extract title')
877         video_title = mobj.group(1).decode('utf-8')
878
879         mobj = re.search(r'submitter=(.*?);', webpage)
880         if mobj is None:
881             raise ExtractorError(u'Unable to extract uploader nickname')
882         video_uploader = mobj.group(1)
883
884         return [{
885             'id':       video_id.decode('utf-8'),
886             'url':      video_url.decode('utf-8'),
887             'uploader': video_uploader.decode('utf-8'),
888             'upload_date':  None,
889             'title':    video_title,
890             'ext':      video_extension.decode('utf-8'),
891         }]
892
893 class DailymotionIE(InfoExtractor):
894     """Information Extractor for Dailymotion"""
895
896     _VALID_URL = r'(?i)(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/video/([^/]+)'
897     IE_NAME = u'dailymotion'
898
899     def _real_extract(self, url):
900         # Extract id and simplified title from URL
901         mobj = re.match(self._VALID_URL, url)
902         if mobj is None:
903             raise ExtractorError(u'Invalid URL: %s' % url)
904
905         video_id = mobj.group(1).split('_')[0].split('?')[0]
906
907         video_extension = 'mp4'
908
909         # Retrieve video webpage to extract further information
910         request = compat_urllib_request.Request(url)
911         request.add_header('Cookie', 'family_filter=off')
912         webpage = self._download_webpage(request, video_id)
913
914         # Extract URL, uploader and title from webpage
915         self.report_extraction(video_id)
916         mobj = re.search(r'\s*var flashvars = (.*)', webpage)
917         if mobj is None:
918             raise ExtractorError(u'Unable to extract media URL')
919         flashvars = compat_urllib_parse.unquote(mobj.group(1))
920
921         for key in ['hd1080URL', 'hd720URL', 'hqURL', 'sdURL', 'ldURL', 'video_url']:
922             if key in flashvars:
923                 max_quality = key
924                 self.to_screen(u'Using %s' % key)
925                 break
926         else:
927             raise ExtractorError(u'Unable to extract video URL')
928
929         mobj = re.search(r'"' + max_quality + r'":"(.+?)"', flashvars)
930         if mobj is None:
931             raise ExtractorError(u'Unable to extract video URL')
932
933         video_url = compat_urllib_parse.unquote(mobj.group(1)).replace('\\/', '/')
934
935         # TODO: support choosing qualities
936
937         mobj = re.search(r'<meta property="og:title" content="(?P<title>[^"]*)" />', webpage)
938         if mobj is None:
939             raise ExtractorError(u'Unable to extract title')
940         video_title = unescapeHTML(mobj.group('title'))
941
942         video_uploader = None
943         mobj = re.search(r'(?im)<span class="owner[^\"]+?">[^<]+?<a [^>]+?>([^<]+?)</a>', webpage)
944         if mobj is None:
945             # lookin for official user
946             mobj_official = re.search(r'<span rel="author"[^>]+?>([^<]+?)</span>', webpage)
947             if mobj_official is None:
948                 self._downloader.report_warning(u'unable to extract uploader nickname')
949             else:
950                 video_uploader = mobj_official.group(1)
951         else:
952             video_uploader = mobj.group(1)
953
954         video_upload_date = None
955         mobj = re.search(r'<div class="[^"]*uploaded_cont[^"]*" title="[^"]*">([0-9]{2})-([0-9]{2})-([0-9]{4})</div>', webpage)
956         if mobj is not None:
957             video_upload_date = mobj.group(3) + mobj.group(2) + mobj.group(1)
958
959         return [{
960             'id':       video_id,
961             'url':      video_url,
962             'uploader': video_uploader,
963             'upload_date':  video_upload_date,
964             'title':    video_title,
965             'ext':      video_extension,
966         }]
967
968
969 class PhotobucketIE(InfoExtractor):
970     """Information extractor for photobucket.com."""
971
972     # TODO: the original _VALID_URL was:
973     # r'(?:http://)?(?:[a-z0-9]+\.)?photobucket\.com/.*[\?\&]current=(.*\.flv)'
974     # Check if it's necessary to keep the old extracion process
975     _VALID_URL = r'(?:http://)?(?:[a-z0-9]+\.)?photobucket\.com/.*(([\?\&]current=)|_)(?P<id>.*)\.(?P<ext>(flv)|(mp4))'
976     IE_NAME = u'photobucket'
977
978     def _real_extract(self, url):
979         # Extract id from URL
980         mobj = re.match(self._VALID_URL, url)
981         if mobj is None:
982             raise ExtractorError(u'Invalid URL: %s' % url)
983
984         video_id = mobj.group('id')
985
986         video_extension = mobj.group('ext')
987
988         # Retrieve video webpage to extract further information
989         webpage = self._download_webpage(url, video_id)
990
991         # Extract URL, uploader, and title from webpage
992         self.report_extraction(video_id)
993         # We try first by looking the javascript code:
994         mobj = re.search(r'Pb\.Data\.Shared\.put\(Pb\.Data\.Shared\.MEDIA, (?P<json>.*?)\);', webpage)
995         if mobj is not None:
996             info = json.loads(mobj.group('json'))
997             return [{
998                 'id':       video_id,
999                 'url':      info[u'downloadUrl'],
1000                 'uploader': info[u'username'],
1001                 'upload_date':  datetime.date.fromtimestamp(info[u'creationDate']).strftime('%Y%m%d'),
1002                 'title':    info[u'title'],
1003                 'ext':      video_extension,
1004                 'thumbnail': info[u'thumbUrl'],
1005             }]
1006
1007         # We try looking in other parts of the webpage
1008         video_url = self._search_regex(r'<link rel="video_src" href=".*\?file=([^"]+)" />',
1009             webpage, u'video URL')
1010
1011         mobj = re.search(r'<title>(.*) video by (.*) - Photobucket</title>', webpage)
1012         if mobj is None:
1013             raise ExtractorError(u'Unable to extract title')
1014         video_title = mobj.group(1).decode('utf-8')
1015         video_uploader = mobj.group(2).decode('utf-8')
1016
1017         return [{
1018             'id':       video_id.decode('utf-8'),
1019             'url':      video_url.decode('utf-8'),
1020             'uploader': video_uploader,
1021             'upload_date':  None,
1022             'title':    video_title,
1023             'ext':      video_extension.decode('utf-8'),
1024         }]
1025
1026
1027 class YahooIE(InfoExtractor):
1028     """Information extractor for screen.yahoo.com."""
1029     _VALID_URL = r'http://screen\.yahoo\.com/.*?-(?P<id>\d*?)\.html'
1030
1031     def _real_extract(self, url):
1032         mobj = re.match(self._VALID_URL, url)
1033         if mobj is None:
1034             raise ExtractorError(u'Invalid URL: %s' % url)
1035         video_id = mobj.group('id')
1036         webpage = self._download_webpage(url, video_id)
1037         m_id = re.search(r'YUI\.namespace\("Media"\)\.CONTENT_ID = "(?P<new_id>.+?)";', webpage)
1038
1039         if m_id is None: 
1040             # TODO: Check which url parameters are required
1041             info_url = 'http://cosmos.bcst.yahoo.com/rest/v2/pops;lmsoverride=1;outputformat=mrss;cb=974419660;id=%s;rd=news.yahoo.com;datacontext=mdb;lg=KCa2IihxG3qE60vQ7HtyUy' % video_id
1042             webpage = self._download_webpage(info_url, video_id, u'Downloading info webpage')
1043             info_re = r'''<title><!\[CDATA\[(?P<title>.*?)\]\]></title>.*
1044                         <description><!\[CDATA\[(?P<description>.*?)\]\]></description>.*
1045                         <media:pubStart><!\[CDATA\[(?P<date>.*?)\ .*\]\]></media:pubStart>.*
1046                         <media:content\ medium="image"\ url="(?P<thumb>.*?)"\ name="LARGETHUMB"
1047                         '''
1048             self.report_extraction(video_id)
1049             m_info = re.search(info_re, webpage, re.VERBOSE|re.DOTALL)
1050             if m_info is None:
1051                 raise ExtractorError(u'Unable to extract video info')
1052             video_title = m_info.group('title')
1053             video_description = m_info.group('description')
1054             video_thumb = m_info.group('thumb')
1055             video_date = m_info.group('date')
1056             video_date = datetime.datetime.strptime(video_date, '%m/%d/%Y').strftime('%Y%m%d')
1057     
1058             # TODO: Find a way to get mp4 videos
1059             rest_url = 'http://cosmos.bcst.yahoo.com/rest/v2/pops;element=stream;outputformat=mrss;id=%s;lmsoverride=1;bw=375;dynamicstream=1;cb=83521105;tech=flv,mp4;rd=news.yahoo.com;datacontext=mdb;lg=KCa2IihxG3qE60vQ7HtyUy' % video_id
1060             webpage = self._download_webpage(rest_url, video_id, u'Downloading video url webpage')
1061             m_rest = re.search(r'<media:content url="(?P<url>.*?)" path="(?P<path>.*?)"', webpage)
1062             video_url = m_rest.group('url')
1063             video_path = m_rest.group('path')
1064             if m_rest is None:
1065                 raise ExtractorError(u'Unable to extract video url')
1066
1067         else: # We have to use a different method if another id is defined
1068             long_id = m_id.group('new_id')
1069             info_url = 'http://video.query.yahoo.com/v1/public/yql?q=SELECT%20*%20FROM%20yahoo.media.video.streams%20WHERE%20id%3D%22' + long_id + '%22%20AND%20format%3D%22mp4%2Cflv%22%20AND%20protocol%3D%22rtmp%2Chttp%22%20AND%20plrs%3D%2286Gj0vCaSzV_Iuf6hNylf2%22%20AND%20acctid%3D%22389%22%20AND%20plidl%3D%22%22%20AND%20pspid%3D%22792700001%22%20AND%20offnetwork%3D%22false%22%20AND%20site%3D%22ivy%22%20AND%20lang%3D%22en-US%22%20AND%20region%3D%22US%22%20AND%20override%3D%22none%22%3B&env=prod&format=json&callback=YUI.Env.JSONP.yui_3_8_1_1_1368368376830_335'
1070             webpage = self._download_webpage(info_url, video_id, u'Downloading info json')
1071             json_str = re.search(r'YUI.Env.JSONP.yui.*?\((.*?)\);', webpage).group(1)
1072             info = json.loads(json_str)
1073             res = info[u'query'][u'results'][u'mediaObj'][0]
1074             stream = res[u'streams'][0]
1075             video_path = stream[u'path']
1076             video_url = stream[u'host']
1077             meta = res[u'meta']
1078             video_title = meta[u'title']
1079             video_description = meta[u'description']
1080             video_thumb = meta[u'thumbnail']
1081             video_date = None # I can't find it
1082
1083         info_dict = {
1084                      'id': video_id,
1085                      'url': video_url,
1086                      'play_path': video_path,
1087                      'title':video_title,
1088                      'description': video_description,
1089                      'thumbnail': video_thumb,
1090                      'upload_date': video_date,
1091                      'ext': 'flv',
1092                      }
1093         return info_dict
1094
1095 class VimeoIE(InfoExtractor):
1096     """Information extractor for vimeo.com."""
1097
1098     # _VALID_URL matches Vimeo URLs
1099     _VALID_URL = r'(?P<proto>https?://)?(?:(?:www|player)\.)?vimeo(?P<pro>pro)?\.com/(?:(?:(?:groups|album)/[^/]+)|(?:.*?)/)?(?P<direct_link>play_redirect_hls\?clip_id=)?(?:videos?/)?(?P<id>[0-9]+)'
1100     IE_NAME = u'vimeo'
1101
1102     def _real_extract(self, url, new_video=True):
1103         # Extract ID from URL
1104         mobj = re.match(self._VALID_URL, url)
1105         if mobj is None:
1106             raise ExtractorError(u'Invalid URL: %s' % url)
1107
1108         video_id = mobj.group('id')
1109         if not mobj.group('proto'):
1110             url = 'https://' + url
1111         if mobj.group('direct_link') or mobj.group('pro'):
1112             url = 'https://vimeo.com/' + video_id
1113
1114         # Retrieve video webpage to extract further information
1115         request = compat_urllib_request.Request(url, None, std_headers)
1116         webpage = self._download_webpage(request, video_id)
1117
1118         # Now we begin extracting as much information as we can from what we
1119         # retrieved. First we extract the information common to all extractors,
1120         # and latter we extract those that are Vimeo specific.
1121         self.report_extraction(video_id)
1122
1123         # Extract the config JSON
1124         try:
1125             config = webpage.split(' = {config:')[1].split(',assets:')[0]
1126             config = json.loads(config)
1127         except:
1128             if re.search('The creator of this video has not given you permission to embed it on this domain.', webpage):
1129                 raise ExtractorError(u'The author has restricted the access to this video, try with the "--referer" option')
1130             else:
1131                 raise ExtractorError(u'Unable to extract info section')
1132
1133         # Extract title
1134         video_title = config["video"]["title"]
1135
1136         # Extract uploader and uploader_id
1137         video_uploader = config["video"]["owner"]["name"]
1138         video_uploader_id = config["video"]["owner"]["url"].split('/')[-1] if config["video"]["owner"]["url"] else None
1139
1140         # Extract video thumbnail
1141         video_thumbnail = config["video"]["thumbnail"]
1142
1143         # Extract video description
1144         video_description = get_element_by_attribute("itemprop", "description", webpage)
1145         if video_description: video_description = clean_html(video_description)
1146         else: video_description = u''
1147
1148         # Extract upload date
1149         video_upload_date = None
1150         mobj = re.search(r'<meta itemprop="dateCreated" content="(\d{4})-(\d{2})-(\d{2})T', webpage)
1151         if mobj is not None:
1152             video_upload_date = mobj.group(1) + mobj.group(2) + mobj.group(3)
1153
1154         # Vimeo specific: extract request signature and timestamp
1155         sig = config['request']['signature']
1156         timestamp = config['request']['timestamp']
1157
1158         # Vimeo specific: extract video codec and quality information
1159         # First consider quality, then codecs, then take everything
1160         # TODO bind to format param
1161         codecs = [('h264', 'mp4'), ('vp8', 'flv'), ('vp6', 'flv')]
1162         files = { 'hd': [], 'sd': [], 'other': []}
1163         for codec_name, codec_extension in codecs:
1164             if codec_name in config["video"]["files"]:
1165                 if 'hd' in config["video"]["files"][codec_name]:
1166                     files['hd'].append((codec_name, codec_extension, 'hd'))
1167                 elif 'sd' in config["video"]["files"][codec_name]:
1168                     files['sd'].append((codec_name, codec_extension, 'sd'))
1169                 else:
1170                     files['other'].append((codec_name, codec_extension, config["video"]["files"][codec_name][0]))
1171
1172         for quality in ('hd', 'sd', 'other'):
1173             if len(files[quality]) > 0:
1174                 video_quality = files[quality][0][2]
1175                 video_codec = files[quality][0][0]
1176                 video_extension = files[quality][0][1]
1177                 self.to_screen(u'%s: Downloading %s file at %s quality' % (video_id, video_codec.upper(), video_quality))
1178                 break
1179         else:
1180             raise ExtractorError(u'No known codec found')
1181
1182         video_url = "http://player.vimeo.com/play_redirect?clip_id=%s&sig=%s&time=%s&quality=%s&codecs=%s&type=moogaloop_local&embed_location=" \
1183                     %(video_id, sig, timestamp, video_quality, video_codec.upper())
1184
1185         return [{
1186             'id':       video_id,
1187             'url':      video_url,
1188             'uploader': video_uploader,
1189             'uploader_id': video_uploader_id,
1190             'upload_date':  video_upload_date,
1191             'title':    video_title,
1192             'ext':      video_extension,
1193             'thumbnail':    video_thumbnail,
1194             'description':  video_description,
1195         }]
1196
1197
1198 class ArteTvIE(InfoExtractor):
1199     """arte.tv information extractor."""
1200
1201     _VALID_URL = r'(?:http://)?videos\.arte\.tv/(?:fr|de)/videos/.*'
1202     _LIVE_URL = r'index-[0-9]+\.html$'
1203
1204     IE_NAME = u'arte.tv'
1205
1206     def fetch_webpage(self, url):
1207         request = compat_urllib_request.Request(url)
1208         try:
1209             self.report_download_webpage(url)
1210             webpage = compat_urllib_request.urlopen(request).read()
1211         except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
1212             raise ExtractorError(u'Unable to retrieve video webpage: %s' % compat_str(err))
1213         except ValueError as err:
1214             raise ExtractorError(u'Invalid URL: %s' % url)
1215         return webpage
1216
1217     def grep_webpage(self, url, regex, regexFlags, matchTuples):
1218         page = self.fetch_webpage(url)
1219         mobj = re.search(regex, page, regexFlags)
1220         info = {}
1221
1222         if mobj is None:
1223             raise ExtractorError(u'Invalid URL: %s' % url)
1224
1225         for (i, key, err) in matchTuples:
1226             if mobj.group(i) is None:
1227                 raise ExtractorError(err)
1228             else:
1229                 info[key] = mobj.group(i)
1230
1231         return info
1232
1233     def extractLiveStream(self, url):
1234         video_lang = url.split('/')[-4]
1235         info = self.grep_webpage(
1236             url,
1237             r'src="(.*?/videothek_js.*?\.js)',
1238             0,
1239             [
1240                 (1, 'url', u'Invalid URL: %s' % url)
1241             ]
1242         )
1243         http_host = url.split('/')[2]
1244         next_url = 'http://%s%s' % (http_host, compat_urllib_parse.unquote(info.get('url')))
1245         info = self.grep_webpage(
1246             next_url,
1247             r'(s_artestras_scst_geoFRDE_' + video_lang + '.*?)\'.*?' +
1248                 '(http://.*?\.swf).*?' +
1249                 '(rtmp://.*?)\'',
1250             re.DOTALL,
1251             [
1252                 (1, 'path',   u'could not extract video path: %s' % url),
1253                 (2, 'player', u'could not extract video player: %s' % url),
1254                 (3, 'url',    u'could not extract video url: %s' % url)
1255             ]
1256         )
1257         video_url = u'%s/%s' % (info.get('url'), info.get('path'))
1258
1259     def extractPlus7Stream(self, url):
1260         video_lang = url.split('/')[-3]
1261         info = self.grep_webpage(
1262             url,
1263             r'param name="movie".*?videorefFileUrl=(http[^\'"&]*)',
1264             0,
1265             [
1266                 (1, 'url', u'Invalid URL: %s' % url)
1267             ]
1268         )
1269         next_url = compat_urllib_parse.unquote(info.get('url'))
1270         info = self.grep_webpage(
1271             next_url,
1272             r'<video lang="%s" ref="(http[^\'"&]*)' % video_lang,
1273             0,
1274             [
1275                 (1, 'url', u'Could not find <video> tag: %s' % url)
1276             ]
1277         )
1278         next_url = compat_urllib_parse.unquote(info.get('url'))
1279
1280         info = self.grep_webpage(
1281             next_url,
1282             r'<video id="(.*?)".*?>.*?' +
1283                 '<name>(.*?)</name>.*?' +
1284                 '<dateVideo>(.*?)</dateVideo>.*?' +
1285                 '<url quality="hd">(.*?)</url>',
1286             re.DOTALL,
1287             [
1288                 (1, 'id',    u'could not extract video id: %s' % url),
1289                 (2, 'title', u'could not extract video title: %s' % url),
1290                 (3, 'date',  u'could not extract video date: %s' % url),
1291                 (4, 'url',   u'could not extract video url: %s' % url)
1292             ]
1293         )
1294
1295         return {
1296             'id':           info.get('id'),
1297             'url':          compat_urllib_parse.unquote(info.get('url')),
1298             'uploader':     u'arte.tv',
1299             'upload_date':  unified_strdate(info.get('date')),
1300             'title':        info.get('title').decode('utf-8'),
1301             'ext':          u'mp4',
1302             'format':       u'NA',
1303             'player_url':   None,
1304         }
1305
1306     def _real_extract(self, url):
1307         video_id = url.split('/')[-1]
1308         self.report_extraction(video_id)
1309
1310         if re.search(self._LIVE_URL, video_id) is not None:
1311             self.extractLiveStream(url)
1312             return
1313         else:
1314             info = self.extractPlus7Stream(url)
1315
1316         return [info]
1317
1318
1319 class GenericIE(InfoExtractor):
1320     """Generic last-resort information extractor."""
1321
1322     _VALID_URL = r'.*'
1323     IE_NAME = u'generic'
1324
1325     def report_download_webpage(self, video_id):
1326         """Report webpage download."""
1327         if not self._downloader.params.get('test', False):
1328             self._downloader.report_warning(u'Falling back on generic information extractor.')
1329         super(GenericIE, self).report_download_webpage(video_id)
1330
1331     def report_following_redirect(self, new_url):
1332         """Report information extraction."""
1333         self._downloader.to_screen(u'[redirect] Following redirect to %s' % new_url)
1334
1335     def _test_redirect(self, url):
1336         """Check if it is a redirect, like url shorteners, in case return the new url."""
1337         class HeadRequest(compat_urllib_request.Request):
1338             def get_method(self):
1339                 return "HEAD"
1340
1341         class HEADRedirectHandler(compat_urllib_request.HTTPRedirectHandler):
1342             """
1343             Subclass the HTTPRedirectHandler to make it use our
1344             HeadRequest also on the redirected URL
1345             """
1346             def redirect_request(self, req, fp, code, msg, headers, newurl):
1347                 if code in (301, 302, 303, 307):
1348                     newurl = newurl.replace(' ', '%20')
1349                     newheaders = dict((k,v) for k,v in req.headers.items()
1350                                       if k.lower() not in ("content-length", "content-type"))
1351                     return HeadRequest(newurl,
1352                                        headers=newheaders,
1353                                        origin_req_host=req.get_origin_req_host(),
1354                                        unverifiable=True)
1355                 else:
1356                     raise compat_urllib_error.HTTPError(req.get_full_url(), code, msg, headers, fp)
1357
1358         class HTTPMethodFallback(compat_urllib_request.BaseHandler):
1359             """
1360             Fallback to GET if HEAD is not allowed (405 HTTP error)
1361             """
1362             def http_error_405(self, req, fp, code, msg, headers):
1363                 fp.read()
1364                 fp.close()
1365
1366                 newheaders = dict((k,v) for k,v in req.headers.items()
1367                                   if k.lower() not in ("content-length", "content-type"))
1368                 return self.parent.open(compat_urllib_request.Request(req.get_full_url(),
1369                                                  headers=newheaders,
1370                                                  origin_req_host=req.get_origin_req_host(),
1371                                                  unverifiable=True))
1372
1373         # Build our opener
1374         opener = compat_urllib_request.OpenerDirector()
1375         for handler in [compat_urllib_request.HTTPHandler, compat_urllib_request.HTTPDefaultErrorHandler,
1376                         HTTPMethodFallback, HEADRedirectHandler,
1377                         compat_urllib_request.HTTPErrorProcessor, compat_urllib_request.HTTPSHandler]:
1378             opener.add_handler(handler())
1379
1380         response = opener.open(HeadRequest(url))
1381         if response is None:
1382             raise ExtractorError(u'Invalid URL protocol')
1383         new_url = response.geturl()
1384
1385         if url == new_url:
1386             return False
1387
1388         self.report_following_redirect(new_url)
1389         return new_url
1390
1391     def _real_extract(self, url):
1392         new_url = self._test_redirect(url)
1393         if new_url: return [self.url_result(new_url)]
1394
1395         video_id = url.split('/')[-1]
1396         try:
1397             webpage = self._download_webpage(url, video_id)
1398         except ValueError as err:
1399             # since this is the last-resort InfoExtractor, if
1400             # this error is thrown, it'll be thrown here
1401             raise ExtractorError(u'Invalid URL: %s' % url)
1402
1403         self.report_extraction(video_id)
1404         # Start with something easy: JW Player in SWFObject
1405         mobj = re.search(r'flashvars: [\'"](?:.*&)?file=(http[^\'"&]*)', webpage)
1406         if mobj is None:
1407             # Broaden the search a little bit
1408             mobj = re.search(r'[^A-Za-z0-9]?(?:file|source)=(http[^\'"&]*)', webpage)
1409         if mobj is None:
1410             # Broaden the search a little bit: JWPlayer JS loader
1411             mobj = re.search(r'[^A-Za-z0-9]?file:\s*["\'](http[^\'"&]*)', webpage)
1412         if mobj is None:
1413             raise ExtractorError(u'Invalid URL: %s' % url)
1414
1415         # It's possible that one of the regexes
1416         # matched, but returned an empty group:
1417         if mobj.group(1) is None:
1418             raise ExtractorError(u'Invalid URL: %s' % url)
1419
1420         video_url = compat_urllib_parse.unquote(mobj.group(1))
1421         video_id = os.path.basename(video_url)
1422
1423         # here's a fun little line of code for you:
1424         video_extension = os.path.splitext(video_id)[1][1:]
1425         video_id = os.path.splitext(video_id)[0]
1426
1427         # it's tempting to parse this further, but you would
1428         # have to take into account all the variations like
1429         #   Video Title - Site Name
1430         #   Site Name | Video Title
1431         #   Video Title - Tagline | Site Name
1432         # and so on and so forth; it's just not practical
1433         video_title = self._html_search_regex(r'<title>(.*)</title>',
1434             webpage, u'video title')
1435
1436         # video uploader is domain name
1437         video_uploader = self._search_regex(r'(?:https?://)?([^/]*)/.*',
1438             url, u'video uploader')
1439
1440         return [{
1441             'id':       video_id,
1442             'url':      video_url,
1443             'uploader': video_uploader,
1444             'upload_date':  None,
1445             'title':    video_title,
1446             'ext':      video_extension,
1447         }]
1448
1449
1450 class YoutubeSearchIE(SearchInfoExtractor):
1451     """Information Extractor for YouTube search queries."""
1452     _API_URL = 'https://gdata.youtube.com/feeds/api/videos?q=%s&start-index=%i&max-results=50&v=2&alt=jsonc'
1453     _MAX_RESULTS = 1000
1454     IE_NAME = u'youtube:search'
1455     _SEARCH_KEY = 'ytsearch'
1456
1457     def report_download_page(self, query, pagenum):
1458         """Report attempt to download search page with given number."""
1459         query = query.decode(preferredencoding())
1460         self._downloader.to_screen(u'[youtube] query "%s": Downloading page %s' % (query, pagenum))
1461
1462     def _get_n_results(self, query, n):
1463         """Get a specified number of results for a query"""
1464
1465         video_ids = []
1466         pagenum = 0
1467         limit = n
1468
1469         while (50 * pagenum) < limit:
1470             self.report_download_page(query, pagenum+1)
1471             result_url = self._API_URL % (compat_urllib_parse.quote_plus(query), (50*pagenum)+1)
1472             request = compat_urllib_request.Request(result_url)
1473             try:
1474                 data = compat_urllib_request.urlopen(request).read().decode('utf-8')
1475             except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
1476                 raise ExtractorError(u'Unable to download API page: %s' % compat_str(err))
1477             api_response = json.loads(data)['data']
1478
1479             if not 'items' in api_response:
1480                 raise ExtractorError(u'[youtube] No video results')
1481
1482             new_ids = list(video['id'] for video in api_response['items'])
1483             video_ids += new_ids
1484
1485             limit = min(n, api_response['totalItems'])
1486             pagenum += 1
1487
1488         if len(video_ids) > n:
1489             video_ids = video_ids[:n]
1490         videos = [self.url_result('http://www.youtube.com/watch?v=%s' % id, 'Youtube') for id in video_ids]
1491         return self.playlist_result(videos, query)
1492
1493
1494 class GoogleSearchIE(SearchInfoExtractor):
1495     """Information Extractor for Google Video search queries."""
1496     _MORE_PAGES_INDICATOR = r'id="pnnext" class="pn"'
1497     _MAX_RESULTS = 1000
1498     IE_NAME = u'video.google:search'
1499     _SEARCH_KEY = 'gvsearch'
1500
1501     def _get_n_results(self, query, n):
1502         """Get a specified number of results for a query"""
1503
1504         res = {
1505             '_type': 'playlist',
1506             'id': query,
1507             'entries': []
1508         }
1509
1510         for pagenum in itertools.count(1):
1511             result_url = u'http://www.google.com/search?tbm=vid&q=%s&start=%s&hl=en' % (compat_urllib_parse.quote_plus(query), pagenum*10)
1512             webpage = self._download_webpage(result_url, u'gvsearch:' + query,
1513                                              note='Downloading result page ' + str(pagenum))
1514
1515             for mobj in re.finditer(r'<h3 class="r"><a href="([^"]+)"', webpage):
1516                 e = {
1517                     '_type': 'url',
1518                     'url': mobj.group(1)
1519                 }
1520                 res['entries'].append(e)
1521
1522             if (pagenum * 10 > n) or not re.search(self._MORE_PAGES_INDICATOR, webpage):
1523                 return res
1524
1525 class YahooSearchIE(SearchInfoExtractor):
1526     """Information Extractor for Yahoo! Video search queries."""
1527
1528     _MAX_RESULTS = 1000
1529     IE_NAME = u'screen.yahoo:search'
1530     _SEARCH_KEY = 'yvsearch'
1531
1532     def _get_n_results(self, query, n):
1533         """Get a specified number of results for a query"""
1534
1535         res = {
1536             '_type': 'playlist',
1537             'id': query,
1538             'entries': []
1539         }
1540         for pagenum in itertools.count(0): 
1541             result_url = u'http://video.search.yahoo.com/search/?p=%s&fr=screen&o=js&gs=0&b=%d' % (compat_urllib_parse.quote_plus(query), pagenum * 30)
1542             webpage = self._download_webpage(result_url, query,
1543                                              note='Downloading results page '+str(pagenum+1))
1544             info = json.loads(webpage)
1545             m = info[u'm']
1546             results = info[u'results']
1547
1548             for (i, r) in enumerate(results):
1549                 if (pagenum * 30) +i >= n:
1550                     break
1551                 mobj = re.search(r'(?P<url>screen\.yahoo\.com/.*?-\d*?\.html)"', r)
1552                 e = self.url_result('http://' + mobj.group('url'), 'Yahoo')
1553                 res['entries'].append(e)
1554             if (pagenum * 30 +i >= n) or (m[u'last'] >= (m[u'total'] -1 )):
1555                 break
1556
1557         return res
1558
1559
1560 class YoutubePlaylistIE(InfoExtractor):
1561     """Information Extractor for YouTube playlists."""
1562
1563     _VALID_URL = r"""(?:
1564                         (?:https?://)?
1565                         (?:\w+\.)?
1566                         youtube\.com/
1567                         (?:
1568                            (?:course|view_play_list|my_playlists|artist|playlist|watch)
1569                            \? (?:.*?&)*? (?:p|a|list)=
1570                         |  p/
1571                         )
1572                         ((?:PL|EC|UU)?[0-9A-Za-z-_]{10,})
1573                         .*
1574                      |
1575                         ((?:PL|EC|UU)[0-9A-Za-z-_]{10,})
1576                      )"""
1577     _TEMPLATE_URL = 'https://gdata.youtube.com/feeds/api/playlists/%s?max-results=%i&start-index=%i&v=2&alt=json'
1578     _MAX_RESULTS = 50
1579     IE_NAME = u'youtube:playlist'
1580
1581     @classmethod
1582     def suitable(cls, url):
1583         """Receives a URL and returns True if suitable for this IE."""
1584         return re.match(cls._VALID_URL, url, re.VERBOSE) is not None
1585
1586     def _real_extract(self, url):
1587         # Extract playlist id
1588         mobj = re.match(self._VALID_URL, url, re.VERBOSE)
1589         if mobj is None:
1590             raise ExtractorError(u'Invalid URL: %s' % url)
1591
1592         # Download playlist videos from API
1593         playlist_id = mobj.group(1) or mobj.group(2)
1594         page_num = 1
1595         videos = []
1596
1597         while True:
1598             url = self._TEMPLATE_URL % (playlist_id, self._MAX_RESULTS, self._MAX_RESULTS * (page_num - 1) + 1)
1599             page = self._download_webpage(url, playlist_id, u'Downloading page #%s' % page_num)
1600
1601             try:
1602                 response = json.loads(page)
1603             except ValueError as err:
1604                 raise ExtractorError(u'Invalid JSON in API response: ' + compat_str(err))
1605
1606             if 'feed' not in response:
1607                 raise ExtractorError(u'Got a malformed response from YouTube API')
1608             playlist_title = response['feed']['title']['$t']
1609             if 'entry' not in response['feed']:
1610                 # Number of videos is a multiple of self._MAX_RESULTS
1611                 break
1612
1613             videos += [ (entry['yt$position']['$t'], entry['content']['src'])
1614                         for entry in response['feed']['entry']
1615                         if 'content' in entry ]
1616
1617             if len(response['feed']['entry']) < self._MAX_RESULTS:
1618                 break
1619             page_num += 1
1620
1621         videos = [v[1] for v in sorted(videos)]
1622
1623         url_results = [self.url_result(url, 'Youtube') for url in videos]
1624         return [self.playlist_result(url_results, playlist_id, playlist_title)]
1625
1626
1627 class YoutubeChannelIE(InfoExtractor):
1628     """Information Extractor for YouTube channels."""
1629
1630     _VALID_URL = r"^(?:https?://)?(?:youtu\.be|(?:\w+\.)?youtube(?:-nocookie)?\.com)/channel/([0-9A-Za-z_-]+)"
1631     _TEMPLATE_URL = 'http://www.youtube.com/channel/%s/videos?sort=da&flow=list&view=0&page=%s&gl=US&hl=en'
1632     _MORE_PAGES_INDICATOR = 'yt-uix-load-more'
1633     _MORE_PAGES_URL = 'http://www.youtube.com/channel_ajax?action_load_more_videos=1&flow=list&paging=%s&view=0&sort=da&channel_id=%s'
1634     IE_NAME = u'youtube:channel'
1635
1636     def extract_videos_from_page(self, page):
1637         ids_in_page = []
1638         for mobj in re.finditer(r'href="/watch\?v=([0-9A-Za-z_-]+)&?', page):
1639             if mobj.group(1) not in ids_in_page:
1640                 ids_in_page.append(mobj.group(1))
1641         return ids_in_page
1642
1643     def _real_extract(self, url):
1644         # Extract channel id
1645         mobj = re.match(self._VALID_URL, url)
1646         if mobj is None:
1647             raise ExtractorError(u'Invalid URL: %s' % url)
1648
1649         # Download channel page
1650         channel_id = mobj.group(1)
1651         video_ids = []
1652         pagenum = 1
1653
1654         url = self._TEMPLATE_URL % (channel_id, pagenum)
1655         page = self._download_webpage(url, channel_id,
1656                                       u'Downloading page #%s' % pagenum)
1657
1658         # Extract video identifiers
1659         ids_in_page = self.extract_videos_from_page(page)
1660         video_ids.extend(ids_in_page)
1661
1662         # Download any subsequent channel pages using the json-based channel_ajax query
1663         if self._MORE_PAGES_INDICATOR in page:
1664             while True:
1665                 pagenum = pagenum + 1
1666
1667                 url = self._MORE_PAGES_URL % (pagenum, channel_id)
1668                 page = self._download_webpage(url, channel_id,
1669                                               u'Downloading page #%s' % pagenum)
1670
1671                 page = json.loads(page)
1672
1673                 ids_in_page = self.extract_videos_from_page(page['content_html'])
1674                 video_ids.extend(ids_in_page)
1675
1676                 if self._MORE_PAGES_INDICATOR  not in page['load_more_widget_html']:
1677                     break
1678
1679         self._downloader.to_screen(u'[youtube] Channel %s: Found %i videos' % (channel_id, len(video_ids)))
1680
1681         urls = ['http://www.youtube.com/watch?v=%s' % id for id in video_ids]
1682         url_entries = [self.url_result(url, 'Youtube') for url in urls]
1683         return [self.playlist_result(url_entries, channel_id)]
1684
1685
1686 class YoutubeUserIE(InfoExtractor):
1687     """Information Extractor for YouTube users."""
1688
1689     _VALID_URL = r'(?:(?:(?:https?://)?(?:\w+\.)?youtube\.com/user/)|ytuser:)([A-Za-z0-9_-]+)'
1690     _TEMPLATE_URL = 'http://gdata.youtube.com/feeds/api/users/%s'
1691     _GDATA_PAGE_SIZE = 50
1692     _GDATA_URL = 'http://gdata.youtube.com/feeds/api/users/%s/uploads?max-results=%d&start-index=%d'
1693     _VIDEO_INDICATOR = r'/watch\?v=(.+?)[\<&]'
1694     IE_NAME = u'youtube:user'
1695
1696     def _real_extract(self, url):
1697         # Extract username
1698         mobj = re.match(self._VALID_URL, url)
1699         if mobj is None:
1700             raise ExtractorError(u'Invalid URL: %s' % url)
1701
1702         username = mobj.group(1)
1703
1704         # Download video ids using YouTube Data API. Result size per
1705         # query is limited (currently to 50 videos) so we need to query
1706         # page by page until there are no video ids - it means we got
1707         # all of them.
1708
1709         video_ids = []
1710         pagenum = 0
1711
1712         while True:
1713             start_index = pagenum * self._GDATA_PAGE_SIZE + 1
1714
1715             gdata_url = self._GDATA_URL % (username, self._GDATA_PAGE_SIZE, start_index)
1716             page = self._download_webpage(gdata_url, username,
1717                                           u'Downloading video ids from %d to %d' % (start_index, start_index + self._GDATA_PAGE_SIZE))
1718
1719             # Extract video identifiers
1720             ids_in_page = []
1721
1722             for mobj in re.finditer(self._VIDEO_INDICATOR, page):
1723                 if mobj.group(1) not in ids_in_page:
1724                     ids_in_page.append(mobj.group(1))
1725
1726             video_ids.extend(ids_in_page)
1727
1728             # A little optimization - if current page is not
1729             # "full", ie. does not contain PAGE_SIZE video ids then
1730             # we can assume that this page is the last one - there
1731             # are no more ids on further pages - no need to query
1732             # again.
1733
1734             if len(ids_in_page) < self._GDATA_PAGE_SIZE:
1735                 break
1736
1737             pagenum += 1
1738
1739         urls = ['http://www.youtube.com/watch?v=%s' % video_id for video_id in video_ids]
1740         url_results = [self.url_result(url, 'Youtube') for url in urls]
1741         return [self.playlist_result(url_results, playlist_title = username)]
1742
1743
1744 class BlipTVUserIE(InfoExtractor):
1745     """Information Extractor for blip.tv users."""
1746
1747     _VALID_URL = r'(?:(?:(?:https?://)?(?:\w+\.)?blip\.tv/)|bliptvuser:)([^/]+)/*$'
1748     _PAGE_SIZE = 12
1749     IE_NAME = u'blip.tv:user'
1750
1751     def _real_extract(self, url):
1752         # Extract username
1753         mobj = re.match(self._VALID_URL, url)
1754         if mobj is None:
1755             raise ExtractorError(u'Invalid URL: %s' % url)
1756
1757         username = mobj.group(1)
1758
1759         page_base = 'http://m.blip.tv/pr/show_get_full_episode_list?users_id=%s&lite=0&esi=1'
1760
1761         page = self._download_webpage(url, username, u'Downloading user page')
1762         mobj = re.search(r'data-users-id="([^"]+)"', page)
1763         page_base = page_base % mobj.group(1)
1764
1765
1766         # Download video ids using BlipTV Ajax calls. Result size per
1767         # query is limited (currently to 12 videos) so we need to query
1768         # page by page until there are no video ids - it means we got
1769         # all of them.
1770
1771         video_ids = []
1772         pagenum = 1
1773
1774         while True:
1775             url = page_base + "&page=" + str(pagenum)
1776             page = self._download_webpage(url, username,
1777                                           u'Downloading video ids from page %d' % pagenum)
1778
1779             # Extract video identifiers
1780             ids_in_page = []
1781
1782             for mobj in re.finditer(r'href="/([^"]+)"', page):
1783                 if mobj.group(1) not in ids_in_page:
1784                     ids_in_page.append(unescapeHTML(mobj.group(1)))
1785
1786             video_ids.extend(ids_in_page)
1787
1788             # A little optimization - if current page is not
1789             # "full", ie. does not contain PAGE_SIZE video ids then
1790             # we can assume that this page is the last one - there
1791             # are no more ids on further pages - no need to query
1792             # again.
1793
1794             if len(ids_in_page) < self._PAGE_SIZE:
1795                 break
1796
1797             pagenum += 1
1798
1799         urls = [u'http://blip.tv/%s' % video_id for video_id in video_ids]
1800         url_entries = [self.url_result(url, 'BlipTV') for url in urls]
1801         return [self.playlist_result(url_entries, playlist_title = username)]
1802
1803
1804 class DepositFilesIE(InfoExtractor):
1805     """Information extractor for depositfiles.com"""
1806
1807     _VALID_URL = r'(?:http://)?(?:\w+\.)?depositfiles\.com/(?:../(?#locale))?files/(.+)'
1808
1809     def _real_extract(self, url):
1810         file_id = url.split('/')[-1]
1811         # Rebuild url in english locale
1812         url = 'http://depositfiles.com/en/files/' + file_id
1813
1814         # Retrieve file webpage with 'Free download' button pressed
1815         free_download_indication = { 'gateway_result' : '1' }
1816         request = compat_urllib_request.Request(url, compat_urllib_parse.urlencode(free_download_indication))
1817         try:
1818             self.report_download_webpage(file_id)
1819             webpage = compat_urllib_request.urlopen(request).read()
1820         except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
1821             raise ExtractorError(u'Unable to retrieve file webpage: %s' % compat_str(err))
1822
1823         # Search for the real file URL
1824         mobj = re.search(r'<form action="(http://fileshare.+?)"', webpage)
1825         if (mobj is None) or (mobj.group(1) is None):
1826             # Try to figure out reason of the error.
1827             mobj = re.search(r'<strong>(Attention.*?)</strong>', webpage, re.DOTALL)
1828             if (mobj is not None) and (mobj.group(1) is not None):
1829                 restriction_message = re.sub('\s+', ' ', mobj.group(1)).strip()
1830                 raise ExtractorError(u'%s' % restriction_message)
1831             else:
1832                 raise ExtractorError(u'Unable to extract download URL from: %s' % url)
1833
1834         file_url = mobj.group(1)
1835         file_extension = os.path.splitext(file_url)[1][1:]
1836
1837         # Search for file title
1838         file_title = self._search_regex(r'<b title="(.*?)">', webpage, u'title')
1839
1840         return [{
1841             'id':       file_id.decode('utf-8'),
1842             'url':      file_url.decode('utf-8'),
1843             'uploader': None,
1844             'upload_date':  None,
1845             'title':    file_title,
1846             'ext':      file_extension.decode('utf-8'),
1847         }]
1848
1849
1850 class FacebookIE(InfoExtractor):
1851     """Information Extractor for Facebook"""
1852
1853     _VALID_URL = r'^(?:https?://)?(?:\w+\.)?facebook\.com/(?:video/video|photo)\.php\?(?:.*?)v=(?P<ID>\d+)(?:.*)'
1854     _LOGIN_URL = 'https://login.facebook.com/login.php?m&next=http%3A%2F%2Fm.facebook.com%2Fhome.php&'
1855     _NETRC_MACHINE = 'facebook'
1856     IE_NAME = u'facebook'
1857
1858     def report_login(self):
1859         """Report attempt to log in."""
1860         self.to_screen(u'Logging in')
1861
1862     def _real_initialize(self):
1863         if self._downloader is None:
1864             return
1865
1866         useremail = None
1867         password = None
1868         downloader_params = self._downloader.params
1869
1870         # Attempt to use provided username and password or .netrc data
1871         if downloader_params.get('username', None) is not None:
1872             useremail = downloader_params['username']
1873             password = downloader_params['password']
1874         elif downloader_params.get('usenetrc', False):
1875             try:
1876                 info = netrc.netrc().authenticators(self._NETRC_MACHINE)
1877                 if info is not None:
1878                     useremail = info[0]
1879                     password = info[2]
1880                 else:
1881                     raise netrc.NetrcParseError('No authenticators for %s' % self._NETRC_MACHINE)
1882             except (IOError, netrc.NetrcParseError) as err:
1883                 self._downloader.report_warning(u'parsing .netrc: %s' % compat_str(err))
1884                 return
1885
1886         if useremail is None:
1887             return
1888
1889         # Log in
1890         login_form = {
1891             'email': useremail,
1892             'pass': password,
1893             'login': 'Log+In'
1894             }
1895         request = compat_urllib_request.Request(self._LOGIN_URL, compat_urllib_parse.urlencode(login_form))
1896         try:
1897             self.report_login()
1898             login_results = compat_urllib_request.urlopen(request).read()
1899             if re.search(r'<form(.*)name="login"(.*)</form>', login_results) is not None:
1900                 self._downloader.report_warning(u'unable to log in: bad username/password, or exceded login rate limit (~3/min). Check credentials or wait.')
1901                 return
1902         except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
1903             self._downloader.report_warning(u'unable to log in: %s' % compat_str(err))
1904             return
1905
1906     def _real_extract(self, url):
1907         mobj = re.match(self._VALID_URL, url)
1908         if mobj is None:
1909             raise ExtractorError(u'Invalid URL: %s' % url)
1910         video_id = mobj.group('ID')
1911
1912         url = 'https://www.facebook.com/video/video.php?v=%s' % video_id
1913         webpage = self._download_webpage(url, video_id)
1914
1915         BEFORE = '{swf.addParam(param[0], param[1]);});\n'
1916         AFTER = '.forEach(function(variable) {swf.addVariable(variable[0], variable[1]);});'
1917         m = re.search(re.escape(BEFORE) + '(.*?)' + re.escape(AFTER), webpage)
1918         if not m:
1919             raise ExtractorError(u'Cannot parse data')
1920         data = dict(json.loads(m.group(1)))
1921         params_raw = compat_urllib_parse.unquote(data['params'])
1922         params = json.loads(params_raw)
1923         video_data = params['video_data'][0]
1924         video_url = video_data.get('hd_src')
1925         if not video_url:
1926             video_url = video_data['sd_src']
1927         if not video_url:
1928             raise ExtractorError(u'Cannot find video URL')
1929         video_duration = int(video_data['video_duration'])
1930         thumbnail = video_data['thumbnail_src']
1931
1932         video_title = self._html_search_regex('<h2 class="uiHeaderTitle">([^<]+)</h2>',
1933             webpage, u'title')
1934
1935         info = {
1936             'id': video_id,
1937             'title': video_title,
1938             'url': video_url,
1939             'ext': 'mp4',
1940             'duration': video_duration,
1941             'thumbnail': thumbnail,
1942         }
1943         return [info]
1944
1945
1946 class BlipTVIE(InfoExtractor):
1947     """Information extractor for blip.tv"""
1948
1949     _VALID_URL = r'^(?:https?://)?(?:\w+\.)?blip\.tv/((.+/)|(play/)|(api\.swf#))(.+)$'
1950     _URL_EXT = r'^.*\.([a-z0-9]+)$'
1951     IE_NAME = u'blip.tv'
1952
1953     def report_direct_download(self, title):
1954         """Report information extraction."""
1955         self.to_screen(u'%s: Direct download detected' % title)
1956
1957     def _real_extract(self, url):
1958         mobj = re.match(self._VALID_URL, url)
1959         if mobj is None:
1960             raise ExtractorError(u'Invalid URL: %s' % url)
1961
1962         # See https://github.com/rg3/youtube-dl/issues/857
1963         api_mobj = re.match(r'http://a\.blip\.tv/api\.swf#(?P<video_id>[\d\w]+)', url)
1964         if api_mobj is not None:
1965             url = 'http://blip.tv/play/g_%s' % api_mobj.group('video_id')
1966         urlp = compat_urllib_parse_urlparse(url)
1967         if urlp.path.startswith('/play/'):
1968             request = compat_urllib_request.Request(url)
1969             response = compat_urllib_request.urlopen(request)
1970             redirecturl = response.geturl()
1971             rurlp = compat_urllib_parse_urlparse(redirecturl)
1972             file_id = compat_parse_qs(rurlp.fragment)['file'][0].rpartition('/')[2]
1973             url = 'http://blip.tv/a/a-' + file_id
1974             return self._real_extract(url)
1975
1976
1977         if '?' in url:
1978             cchar = '&'
1979         else:
1980             cchar = '?'
1981         json_url = url + cchar + 'skin=json&version=2&no_wrap=1'
1982         request = compat_urllib_request.Request(json_url)
1983         request.add_header('User-Agent', 'iTunes/10.6.1')
1984         self.report_extraction(mobj.group(1))
1985         info = None
1986         try:
1987             urlh = compat_urllib_request.urlopen(request)
1988             if urlh.headers.get('Content-Type', '').startswith('video/'): # Direct download
1989                 basename = url.split('/')[-1]
1990                 title,ext = os.path.splitext(basename)
1991                 title = title.decode('UTF-8')
1992                 ext = ext.replace('.', '')
1993                 self.report_direct_download(title)
1994                 info = {
1995                     'id': title,
1996                     'url': url,
1997                     'uploader': None,
1998                     'upload_date': None,
1999                     'title': title,
2000                     'ext': ext,
2001                     'urlhandle': urlh
2002                 }
2003         except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
2004             raise ExtractorError(u'ERROR: unable to download video info webpage: %s' % compat_str(err))
2005         if info is None: # Regular URL
2006             try:
2007                 json_code_bytes = urlh.read()
2008                 json_code = json_code_bytes.decode('utf-8')
2009             except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
2010                 raise ExtractorError(u'Unable to read video info webpage: %s' % compat_str(err))
2011
2012             try:
2013                 json_data = json.loads(json_code)
2014                 if 'Post' in json_data:
2015                     data = json_data['Post']
2016                 else:
2017                     data = json_data
2018
2019                 upload_date = datetime.datetime.strptime(data['datestamp'], '%m-%d-%y %H:%M%p').strftime('%Y%m%d')
2020                 video_url = data['media']['url']
2021                 umobj = re.match(self._URL_EXT, video_url)
2022                 if umobj is None:
2023                     raise ValueError('Can not determine filename extension')
2024                 ext = umobj.group(1)
2025
2026                 info = {
2027                     'id': data['item_id'],
2028                     'url': video_url,
2029                     'uploader': data['display_name'],
2030                     'upload_date': upload_date,
2031                     'title': data['title'],
2032                     'ext': ext,
2033                     'format': data['media']['mimeType'],
2034                     'thumbnail': data['thumbnailUrl'],
2035                     'description': data['description'],
2036                     'player_url': data['embedUrl'],
2037                     'user_agent': 'iTunes/10.6.1',
2038                 }
2039             except (ValueError,KeyError) as err:
2040                 raise ExtractorError(u'Unable to parse video information: %s' % repr(err))
2041
2042         return [info]
2043
2044
2045 class MyVideoIE(InfoExtractor):
2046     """Information Extractor for myvideo.de."""
2047
2048     _VALID_URL = r'(?:http://)?(?:www\.)?myvideo\.de/watch/([0-9]+)/([^?/]+).*'
2049     IE_NAME = u'myvideo'
2050
2051     # Original Code from: https://github.com/dersphere/plugin.video.myvideo_de.git
2052     # Released into the Public Domain by Tristan Fischer on 2013-05-19
2053     # https://github.com/rg3/youtube-dl/pull/842
2054     def __rc4crypt(self,data, key):
2055         x = 0
2056         box = list(range(256))
2057         for i in list(range(256)):
2058             x = (x + box[i] + compat_ord(key[i % len(key)])) % 256
2059             box[i], box[x] = box[x], box[i]
2060         x = 0
2061         y = 0
2062         out = ''
2063         for char in data:
2064             x = (x + 1) % 256
2065             y = (y + box[x]) % 256
2066             box[x], box[y] = box[y], box[x]
2067             out += chr(compat_ord(char) ^ box[(box[x] + box[y]) % 256])
2068         return out
2069
2070     def __md5(self,s):
2071         return hashlib.md5(s).hexdigest().encode()
2072
2073     def _real_extract(self,url):
2074         mobj = re.match(self._VALID_URL, url)
2075         if mobj is None:
2076             raise ExtractorError(u'invalid URL: %s' % url)
2077
2078         video_id = mobj.group(1)
2079
2080         GK = (
2081           b'WXpnME1EZGhNRGhpTTJNM01XVmhOREU0WldNNVpHTTJOakpt'
2082           b'TW1FMU5tVTBNR05pWkRaa05XRXhNVFJoWVRVd1ptSXhaVEV3'
2083           b'TnpsbA0KTVRkbU1tSTRNdz09'
2084         )
2085
2086         # Get video webpage
2087         webpage_url = 'http://www.myvideo.de/watch/%s' % video_id
2088         webpage = self._download_webpage(webpage_url, video_id)
2089
2090         mobj = re.search('source src=\'(.+?)[.]([^.]+)\'', webpage)
2091         if mobj is not None:
2092             self.report_extraction(video_id)
2093             video_url = mobj.group(1) + '.flv'
2094
2095             video_title = self._html_search_regex('<title>([^<]+)</title>',
2096                 webpage, u'title')
2097
2098             video_ext = self._search_regex('[.](.+?)$', video_url, u'extension')
2099
2100             return [{
2101                 'id':       video_id,
2102                 'url':      video_url,
2103                 'uploader': None,
2104                 'upload_date':  None,
2105                 'title':    video_title,
2106                 'ext':      u'flv',
2107             }]
2108
2109         # try encxml
2110         mobj = re.search('var flashvars={(.+?)}', webpage)
2111         if mobj is None:
2112             raise ExtractorError(u'Unable to extract video')
2113
2114         params = {}
2115         encxml = ''
2116         sec = mobj.group(1)
2117         for (a, b) in re.findall('(.+?):\'(.+?)\',?', sec):
2118             if not a == '_encxml':
2119                 params[a] = b
2120             else:
2121                 encxml = compat_urllib_parse.unquote(b)
2122         if not params.get('domain'):
2123             params['domain'] = 'www.myvideo.de'
2124         xmldata_url = '%s?%s' % (encxml, compat_urllib_parse.urlencode(params))
2125         if 'flash_playertype=MTV' in xmldata_url:
2126             self._downloader.report_warning(u'avoiding MTV player')
2127             xmldata_url = (
2128                 'http://www.myvideo.de/dynamic/get_player_video_xml.php'
2129                 '?flash_playertype=D&ID=%s&_countlimit=4&autorun=yes'
2130             ) % video_id
2131
2132         # get enc data
2133         enc_data = self._download_webpage(xmldata_url, video_id).split('=')[1]
2134         enc_data_b = binascii.unhexlify(enc_data)
2135         sk = self.__md5(
2136             base64.b64decode(base64.b64decode(GK)) +
2137             self.__md5(
2138                 str(video_id).encode('utf-8')
2139             )
2140         )
2141         dec_data = self.__rc4crypt(enc_data_b, sk)
2142
2143         # extracting infos
2144         self.report_extraction(video_id)
2145
2146         video_url = None
2147         mobj = re.search('connectionurl=\'(.*?)\'', dec_data)
2148         if mobj:
2149             video_url = compat_urllib_parse.unquote(mobj.group(1))
2150             if 'myvideo2flash' in video_url:
2151                 self._downloader.report_warning(u'forcing RTMPT ...')
2152                 video_url = video_url.replace('rtmpe://', 'rtmpt://')
2153
2154         if not video_url:
2155             # extract non rtmp videos
2156             mobj = re.search('path=\'(http.*?)\' source=\'(.*?)\'', dec_data)
2157             if mobj is None:
2158                 raise ExtractorError(u'unable to extract url')
2159             video_url = compat_urllib_parse.unquote(mobj.group(1)) + compat_urllib_parse.unquote(mobj.group(2))
2160
2161         video_file = self._search_regex('source=\'(.*?)\'', dec_data, u'video file')
2162         video_file = compat_urllib_parse.unquote(video_file)
2163
2164         if not video_file.endswith('f4m'):
2165             ppath, prefix = video_file.split('.')
2166             video_playpath = '%s:%s' % (prefix, ppath)
2167             video_hls_playlist = ''
2168         else:
2169             video_playpath = ''
2170             video_hls_playlist = (
2171                 video_filepath + video_file
2172             ).replace('.f4m', '.m3u8')
2173
2174         video_swfobj = self._search_regex('swfobject.embedSWF\(\'(.+?)\'', webpage, u'swfobj')
2175         video_swfobj = compat_urllib_parse.unquote(video_swfobj)
2176
2177         video_title = self._html_search_regex("<h1(?: class='globalHd')?>(.*?)</h1>",
2178             webpage, u'title')
2179
2180         return [{
2181             'id':                 video_id,
2182             'url':                video_url,
2183             'tc_url':             video_url,
2184             'uploader':           None,
2185             'upload_date':        None,
2186             'title':              video_title,
2187             'ext':                u'flv',
2188             'play_path':          video_playpath,
2189             'video_file':         video_file,
2190             'video_hls_playlist': video_hls_playlist,
2191             'player_url':         video_swfobj,
2192         }]
2193
2194
2195 class ComedyCentralIE(InfoExtractor):
2196     """Information extractor for The Daily Show and Colbert Report """
2197
2198     # urls can be abbreviations like :thedailyshow or :colbert
2199     # urls for episodes like:
2200     # or urls for clips like: http://www.thedailyshow.com/watch/mon-december-10-2012/any-given-gun-day
2201     #                     or: http://www.colbertnation.com/the-colbert-report-videos/421667/november-29-2012/moon-shattering-news
2202     #                     or: http://www.colbertnation.com/the-colbert-report-collections/422008/festival-of-lights/79524
2203     _VALID_URL = r"""^(:(?P<shortname>tds|thedailyshow|cr|colbert|colbertnation|colbertreport)
2204                       |(https?://)?(www\.)?
2205                           (?P<showname>thedailyshow|colbertnation)\.com/
2206                          (full-episodes/(?P<episode>.*)|
2207                           (?P<clip>
2208                               (the-colbert-report-(videos|collections)/(?P<clipID>[0-9]+)/[^/]*/(?P<cntitle>.*?))
2209                               |(watch/(?P<date>[^/]*)/(?P<tdstitle>.*)))))
2210                      $"""
2211
2212     _available_formats = ['3500', '2200', '1700', '1200', '750', '400']
2213
2214     _video_extensions = {
2215         '3500': 'mp4',
2216         '2200': 'mp4',
2217         '1700': 'mp4',
2218         '1200': 'mp4',
2219         '750': 'mp4',
2220         '400': 'mp4',
2221     }
2222     _video_dimensions = {
2223         '3500': '1280x720',
2224         '2200': '960x540',
2225         '1700': '768x432',
2226         '1200': '640x360',
2227         '750': '512x288',
2228         '400': '384x216',
2229     }
2230
2231     @classmethod
2232     def suitable(cls, url):
2233         """Receives a URL and returns True if suitable for this IE."""
2234         return re.match(cls._VALID_URL, url, re.VERBOSE) is not None
2235
2236     def _print_formats(self, formats):
2237         print('Available formats:')
2238         for x in formats:
2239             print('%s\t:\t%s\t[%s]' %(x, self._video_extensions.get(x, 'mp4'), self._video_dimensions.get(x, '???')))
2240
2241
2242     def _real_extract(self, url):
2243         mobj = re.match(self._VALID_URL, url, re.VERBOSE)
2244         if mobj is None:
2245             raise ExtractorError(u'Invalid URL: %s' % url)
2246
2247         if mobj.group('shortname'):
2248             if mobj.group('shortname') in ('tds', 'thedailyshow'):
2249                 url = u'http://www.thedailyshow.com/full-episodes/'
2250             else:
2251                 url = u'http://www.colbertnation.com/full-episodes/'
2252             mobj = re.match(self._VALID_URL, url, re.VERBOSE)
2253             assert mobj is not None
2254
2255         if mobj.group('clip'):
2256             if mobj.group('showname') == 'thedailyshow':
2257                 epTitle = mobj.group('tdstitle')
2258             else:
2259                 epTitle = mobj.group('cntitle')
2260             dlNewest = False
2261         else:
2262             dlNewest = not mobj.group('episode')
2263             if dlNewest:
2264                 epTitle = mobj.group('showname')
2265             else:
2266                 epTitle = mobj.group('episode')
2267
2268         self.report_extraction(epTitle)
2269         webpage,htmlHandle = self._download_webpage_handle(url, epTitle)
2270         if dlNewest:
2271             url = htmlHandle.geturl()
2272             mobj = re.match(self._VALID_URL, url, re.VERBOSE)
2273             if mobj is None:
2274                 raise ExtractorError(u'Invalid redirected URL: ' + url)
2275             if mobj.group('episode') == '':
2276                 raise ExtractorError(u'Redirected URL is still not specific: ' + url)
2277             epTitle = mobj.group('episode')
2278
2279         mMovieParams = re.findall('(?:<param name="movie" value="|var url = ")(http://media.mtvnservices.com/([^"]*(?:episode|video).*?:.*?))"', webpage)
2280
2281         if len(mMovieParams) == 0:
2282             # The Colbert Report embeds the information in a without
2283             # a URL prefix; so extract the alternate reference
2284             # and then add the URL prefix manually.
2285
2286             altMovieParams = re.findall('data-mgid="([^"]*(?:episode|video).*?:.*?)"', webpage)
2287             if len(altMovieParams) == 0:
2288                 raise ExtractorError(u'unable to find Flash URL in webpage ' + url)
2289             else:
2290                 mMovieParams = [("http://media.mtvnservices.com/" + altMovieParams[0], altMovieParams[0])]
2291
2292         uri = mMovieParams[0][1]
2293         indexUrl = 'http://shadow.comedycentral.com/feeds/video_player/mrss/?' + compat_urllib_parse.urlencode({'uri': uri})
2294         indexXml = self._download_webpage(indexUrl, epTitle,
2295                                           u'Downloading show index',
2296                                           u'unable to download episode index')
2297
2298         results = []
2299
2300         idoc = xml.etree.ElementTree.fromstring(indexXml)
2301         itemEls = idoc.findall('.//item')
2302         for partNum,itemEl in enumerate(itemEls):
2303             mediaId = itemEl.findall('./guid')[0].text
2304             shortMediaId = mediaId.split(':')[-1]
2305             showId = mediaId.split(':')[-2].replace('.com', '')
2306             officialTitle = itemEl.findall('./title')[0].text
2307             officialDate = unified_strdate(itemEl.findall('./pubDate')[0].text)
2308
2309             configUrl = ('http://www.comedycentral.com/global/feeds/entertainment/media/mediaGenEntertainment.jhtml?' +
2310                         compat_urllib_parse.urlencode({'uri': mediaId}))
2311             configXml = self._download_webpage(configUrl, epTitle,
2312                                                u'Downloading configuration for %s' % shortMediaId)
2313
2314             cdoc = xml.etree.ElementTree.fromstring(configXml)
2315             turls = []
2316             for rendition in cdoc.findall('.//rendition'):
2317                 finfo = (rendition.attrib['bitrate'], rendition.findall('./src')[0].text)
2318                 turls.append(finfo)
2319
2320             if len(turls) == 0:
2321                 self._downloader.report_error(u'unable to download ' + mediaId + ': No videos found')
2322                 continue
2323
2324             if self._downloader.params.get('listformats', None):
2325                 self._print_formats([i[0] for i in turls])
2326                 return
2327
2328             # For now, just pick the highest bitrate
2329             format,rtmp_video_url = turls[-1]
2330
2331             # Get the format arg from the arg stream
2332             req_format = self._downloader.params.get('format', None)
2333
2334             # Select format if we can find one
2335             for f,v in turls:
2336                 if f == req_format:
2337                     format, rtmp_video_url = f, v
2338                     break
2339
2340             m = re.match(r'^rtmpe?://.*?/(?P<finalid>gsp.comedystor/.*)$', rtmp_video_url)
2341             if not m:
2342                 raise ExtractorError(u'Cannot transform RTMP url')
2343             base = 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=1+_pxI0=Ripod-h264+_pxL0=undefined+_pxM0=+_pxK=18639+_pxE=mp4/44620/mtvnorigin/'
2344             video_url = base + m.group('finalid')
2345
2346             effTitle = showId + u'-' + epTitle + u' part ' + compat_str(partNum+1)
2347             info = {
2348                 'id': shortMediaId,
2349                 'url': video_url,
2350                 'uploader': showId,
2351                 'upload_date': officialDate,
2352                 'title': effTitle,
2353                 'ext': 'mp4',
2354                 'format': format,
2355                 'thumbnail': None,
2356                 'description': officialTitle,
2357             }
2358             results.append(info)
2359
2360         return results
2361
2362
2363 class EscapistIE(InfoExtractor):
2364     """Information extractor for The Escapist """
2365
2366     _VALID_URL = r'^(https?://)?(www\.)?escapistmagazine\.com/videos/view/(?P<showname>[^/]+)/(?P<episode>[^/?]+)[/?]?.*$'
2367     IE_NAME = u'escapist'
2368
2369     def _real_extract(self, url):
2370         mobj = re.match(self._VALID_URL, url)
2371         if mobj is None:
2372             raise ExtractorError(u'Invalid URL: %s' % url)
2373         showName = mobj.group('showname')
2374         videoId = mobj.group('episode')
2375
2376         self.report_extraction(videoId)
2377         webpage = self._download_webpage(url, videoId)
2378
2379         videoDesc = self._html_search_regex('<meta name="description" content="([^"]*)"',
2380             webpage, u'description', fatal=False)
2381
2382         imgUrl = self._html_search_regex('<meta property="og:image" content="([^"]*)"',
2383             webpage, u'thumbnail', fatal=False)
2384
2385         playerUrl = self._html_search_regex('<meta property="og:video" content="([^"]*)"',
2386             webpage, u'player url')
2387
2388         title = self._html_search_regex('<meta name="title" content="([^"]*)"',
2389             webpage, u'player url').split(' : ')[-1]
2390
2391         configUrl = self._search_regex('config=(.*)$', playerUrl, u'config url')
2392         configUrl = compat_urllib_parse.unquote(configUrl)
2393
2394         configJSON = self._download_webpage(configUrl, videoId,
2395                                             u'Downloading configuration',
2396                                             u'unable to download configuration')
2397
2398         # Technically, it's JavaScript, not JSON
2399         configJSON = configJSON.replace("'", '"')
2400
2401         try:
2402             config = json.loads(configJSON)
2403         except (ValueError,) as err:
2404             raise ExtractorError(u'Invalid JSON in configuration file: ' + compat_str(err))
2405
2406         playlist = config['playlist']
2407         videoUrl = playlist[1]['url']
2408
2409         info = {
2410             'id': videoId,
2411             'url': videoUrl,
2412             'uploader': showName,
2413             'upload_date': None,
2414             'title': title,
2415             'ext': 'mp4',
2416             'thumbnail': imgUrl,
2417             'description': videoDesc,
2418             'player_url': playerUrl,
2419         }
2420
2421         return [info]
2422
2423 class CollegeHumorIE(InfoExtractor):
2424     """Information extractor for collegehumor.com"""
2425
2426     _WORKING = False
2427     _VALID_URL = r'^(?:https?://)?(?:www\.)?collegehumor\.com/video/(?P<videoid>[0-9]+)/(?P<shorttitle>.*)$'
2428     IE_NAME = u'collegehumor'
2429
2430     def report_manifest(self, video_id):
2431         """Report information extraction."""
2432         self.to_screen(u'%s: Downloading XML manifest' % video_id)
2433
2434     def _real_extract(self, url):
2435         mobj = re.match(self._VALID_URL, url)
2436         if mobj is None:
2437             raise ExtractorError(u'Invalid URL: %s' % url)
2438         video_id = mobj.group('videoid')
2439
2440         info = {
2441             'id': video_id,
2442             'uploader': None,
2443             'upload_date': None,
2444         }
2445
2446         self.report_extraction(video_id)
2447         xmlUrl = 'http://www.collegehumor.com/moogaloop/video/' + video_id
2448         try:
2449             metaXml = compat_urllib_request.urlopen(xmlUrl).read()
2450         except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
2451             raise ExtractorError(u'Unable to download video info XML: %s' % compat_str(err))
2452
2453         mdoc = xml.etree.ElementTree.fromstring(metaXml)
2454         try:
2455             videoNode = mdoc.findall('./video')[0]
2456             info['description'] = videoNode.findall('./description')[0].text
2457             info['title'] = videoNode.findall('./caption')[0].text
2458             info['thumbnail'] = videoNode.findall('./thumbnail')[0].text
2459             manifest_url = videoNode.findall('./file')[0].text
2460         except IndexError:
2461             raise ExtractorError(u'Invalid metadata XML file')
2462
2463         manifest_url += '?hdcore=2.10.3'
2464         self.report_manifest(video_id)
2465         try:
2466             manifestXml = compat_urllib_request.urlopen(manifest_url).read()
2467         except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
2468             raise ExtractorError(u'Unable to download video info XML: %s' % compat_str(err))
2469
2470         adoc = xml.etree.ElementTree.fromstring(manifestXml)
2471         try:
2472             media_node = adoc.findall('./{http://ns.adobe.com/f4m/1.0}media')[0]
2473             node_id = media_node.attrib['url']
2474             video_id = adoc.findall('./{http://ns.adobe.com/f4m/1.0}id')[0].text
2475         except IndexError as err:
2476             raise ExtractorError(u'Invalid manifest file')
2477
2478         url_pr = compat_urllib_parse_urlparse(manifest_url)
2479         url = url_pr.scheme + '://' + url_pr.netloc + '/z' + video_id[:-2] + '/' + node_id + 'Seg1-Frag1'
2480
2481         info['url'] = url
2482         info['ext'] = 'f4f'
2483         return [info]
2484
2485
2486 class XVideosIE(InfoExtractor):
2487     """Information extractor for xvideos.com"""
2488
2489     _VALID_URL = r'^(?:https?://)?(?:www\.)?xvideos\.com/video([0-9]+)(?:.*)'
2490     IE_NAME = u'xvideos'
2491
2492     def _real_extract(self, url):
2493         mobj = re.match(self._VALID_URL, url)
2494         if mobj is None:
2495             raise ExtractorError(u'Invalid URL: %s' % url)
2496         video_id = mobj.group(1)
2497
2498         webpage = self._download_webpage(url, video_id)
2499
2500         self.report_extraction(video_id)
2501
2502         # Extract video URL
2503         video_url = compat_urllib_parse.unquote(self._search_regex(r'flv_url=(.+?)&',
2504             webpage, u'video URL'))
2505
2506         # Extract title
2507         video_title = self._html_search_regex(r'<title>(.*?)\s+-\s+XVID',
2508             webpage, u'title')
2509
2510         # Extract video thumbnail
2511         video_thumbnail = self._search_regex(r'http://(?:img.*?\.)xvideos.com/videos/thumbs/[a-fA-F0-9]+/[a-fA-F0-9]+/[a-fA-F0-9]+/[a-fA-F0-9]+/([a-fA-F0-9.]+jpg)',
2512             webpage, u'thumbnail', fatal=False)
2513
2514         info = {
2515             'id': video_id,
2516             'url': video_url,
2517             'uploader': None,
2518             'upload_date': None,
2519             'title': video_title,
2520             'ext': 'flv',
2521             'thumbnail': video_thumbnail,
2522             'description': None,
2523         }
2524
2525         return [info]
2526
2527
2528 class SoundcloudIE(InfoExtractor):
2529     """Information extractor for soundcloud.com
2530        To access the media, the uid of the song and a stream token
2531        must be extracted from the page source and the script must make
2532        a request to media.soundcloud.com/crossdomain.xml. Then
2533        the media can be grabbed by requesting from an url composed
2534        of the stream token and uid
2535      """
2536
2537     _VALID_URL = r'^(?:https?://)?(?:www\.)?soundcloud\.com/([\w\d-]+)/([\w\d-]+)'
2538     IE_NAME = u'soundcloud'
2539
2540     def report_resolve(self, video_id):
2541         """Report information extraction."""
2542         self.to_screen(u'%s: Resolving id' % video_id)
2543
2544     def _real_extract(self, url):
2545         mobj = re.match(self._VALID_URL, url)
2546         if mobj is None:
2547             raise ExtractorError(u'Invalid URL: %s' % url)
2548
2549         # extract uploader (which is in the url)
2550         uploader = mobj.group(1)
2551         # extract simple title (uploader + slug of song title)
2552         slug_title =  mobj.group(2)
2553         simple_title = uploader + u'-' + slug_title
2554         full_title = '%s/%s' % (uploader, slug_title)
2555
2556         self.report_resolve(full_title)
2557
2558         url = 'http://soundcloud.com/%s/%s' % (uploader, slug_title)
2559         resolv_url = 'http://api.soundcloud.com/resolve.json?url=' + url + '&client_id=b45b1aa10f1ac2941910a7f0d10f8e28'
2560         info_json = self._download_webpage(resolv_url, full_title, u'Downloading info JSON')
2561
2562         info = json.loads(info_json)
2563         video_id = info['id']
2564         self.report_extraction(full_title)
2565
2566         streams_url = 'https://api.sndcdn.com/i1/tracks/' + str(video_id) + '/streams?client_id=b45b1aa10f1ac2941910a7f0d10f8e28'
2567         stream_json = self._download_webpage(streams_url, full_title,
2568                                              u'Downloading stream definitions',
2569                                              u'unable to download stream definitions')
2570
2571         streams = json.loads(stream_json)
2572         mediaURL = streams['http_mp3_128_url']
2573         upload_date = unified_strdate(info['created_at'])
2574
2575         return [{
2576             'id':       info['id'],
2577             'url':      mediaURL,
2578             'uploader': info['user']['username'],
2579             'upload_date': upload_date,
2580             'title':    info['title'],
2581             'ext':      u'mp3',
2582             'description': info['description'],
2583         }]
2584
2585 class SoundcloudSetIE(InfoExtractor):
2586     """Information extractor for soundcloud.com sets
2587        To access the media, the uid of the song and a stream token
2588        must be extracted from the page source and the script must make
2589        a request to media.soundcloud.com/crossdomain.xml. Then
2590        the media can be grabbed by requesting from an url composed
2591        of the stream token and uid
2592      """
2593
2594     _VALID_URL = r'^(?:https?://)?(?:www\.)?soundcloud\.com/([\w\d-]+)/sets/([\w\d-]+)'
2595     IE_NAME = u'soundcloud:set'
2596
2597     def report_resolve(self, video_id):
2598         """Report information extraction."""
2599         self.to_screen(u'%s: Resolving id' % video_id)
2600
2601     def _real_extract(self, url):
2602         mobj = re.match(self._VALID_URL, url)
2603         if mobj is None:
2604             raise ExtractorError(u'Invalid URL: %s' % url)
2605
2606         # extract uploader (which is in the url)
2607         uploader = mobj.group(1)
2608         # extract simple title (uploader + slug of song title)
2609         slug_title =  mobj.group(2)
2610         simple_title = uploader + u'-' + slug_title
2611         full_title = '%s/sets/%s' % (uploader, slug_title)
2612
2613         self.report_resolve(full_title)
2614
2615         url = 'http://soundcloud.com/%s/sets/%s' % (uploader, slug_title)
2616         resolv_url = 'http://api.soundcloud.com/resolve.json?url=' + url + '&client_id=b45b1aa10f1ac2941910a7f0d10f8e28'
2617         info_json = self._download_webpage(resolv_url, full_title)
2618
2619         videos = []
2620         info = json.loads(info_json)
2621         if 'errors' in info:
2622             for err in info['errors']:
2623                 self._downloader.report_error(u'unable to download video webpage: %s' % compat_str(err['error_message']))
2624             return
2625
2626         self.report_extraction(full_title)
2627         for track in info['tracks']:
2628             video_id = track['id']
2629
2630             streams_url = 'https://api.sndcdn.com/i1/tracks/' + str(video_id) + '/streams?client_id=b45b1aa10f1ac2941910a7f0d10f8e28'
2631             stream_json = self._download_webpage(streams_url, video_id, u'Downloading track info JSON')
2632
2633             self.report_extraction(video_id)
2634             streams = json.loads(stream_json)
2635             mediaURL = streams['http_mp3_128_url']
2636
2637             videos.append({
2638                 'id':       video_id,
2639                 'url':      mediaURL,
2640                 'uploader': track['user']['username'],
2641                 'upload_date':  unified_strdate(track['created_at']),
2642                 'title':    track['title'],
2643                 'ext':      u'mp3',
2644                 'description': track['description'],
2645             })
2646         return videos
2647
2648
2649 class InfoQIE(InfoExtractor):
2650     """Information extractor for infoq.com"""
2651     _VALID_URL = r'^(?:https?://)?(?:www\.)?infoq\.com/[^/]+/[^/]+$'
2652
2653     def _real_extract(self, url):
2654         mobj = re.match(self._VALID_URL, url)
2655         if mobj is None:
2656             raise ExtractorError(u'Invalid URL: %s' % url)
2657
2658         webpage = self._download_webpage(url, video_id=url)
2659         self.report_extraction(url)
2660
2661         # Extract video URL
2662         mobj = re.search(r"jsclassref ?= ?'([^']*)'", webpage)
2663         if mobj is None:
2664             raise ExtractorError(u'Unable to extract video url')
2665         real_id = compat_urllib_parse.unquote(base64.b64decode(mobj.group(1).encode('ascii')).decode('utf-8'))
2666         video_url = 'rtmpe://video.infoq.com/cfx/st/' + real_id
2667
2668         # Extract title
2669         video_title = self._search_regex(r'contentTitle = "(.*?)";',
2670             webpage, u'title')
2671
2672         # Extract description
2673         video_description = self._html_search_regex(r'<meta name="description" content="(.*)"(?:\s*/)?>',
2674             webpage, u'description', fatal=False)
2675
2676         video_filename = video_url.split('/')[-1]
2677         video_id, extension = video_filename.split('.')
2678
2679         info = {
2680             'id': video_id,
2681             'url': video_url,
2682             'uploader': None,
2683             'upload_date': None,
2684             'title': video_title,
2685             'ext': extension, # Extension is always(?) mp4, but seems to be flv
2686             'thumbnail': None,
2687             'description': video_description,
2688         }
2689
2690         return [info]
2691
2692 class MixcloudIE(InfoExtractor):
2693     """Information extractor for www.mixcloud.com"""
2694
2695     _WORKING = False # New API, but it seems good http://www.mixcloud.com/developers/documentation/
2696     _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/([\w\d-]+)/([\w\d-]+)'
2697     IE_NAME = u'mixcloud'
2698
2699     def report_download_json(self, file_id):
2700         """Report JSON download."""
2701         self.to_screen(u'Downloading json')
2702
2703     def get_urls(self, jsonData, fmt, bitrate='best'):
2704         """Get urls from 'audio_formats' section in json"""
2705         file_url = None
2706         try:
2707             bitrate_list = jsonData[fmt]
2708             if bitrate is None or bitrate == 'best' or bitrate not in bitrate_list:
2709                 bitrate = max(bitrate_list) # select highest
2710
2711             url_list = jsonData[fmt][bitrate]
2712         except TypeError: # we have no bitrate info.
2713             url_list = jsonData[fmt]
2714         return url_list
2715
2716     def check_urls(self, url_list):
2717         """Returns 1st active url from list"""
2718         for url in url_list:
2719             try:
2720                 compat_urllib_request.urlopen(url)
2721                 return url
2722             except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
2723                 url = None
2724
2725         return None
2726
2727     def _print_formats(self, formats):
2728         print('Available formats:')
2729         for fmt in formats.keys():
2730             for b in formats[fmt]:
2731                 try:
2732                     ext = formats[fmt][b][0]
2733                     print('%s\t%s\t[%s]' % (fmt, b, ext.split('.')[-1]))
2734                 except TypeError: # we have no bitrate info
2735                     ext = formats[fmt][0]
2736                     print('%s\t%s\t[%s]' % (fmt, '??', ext.split('.')[-1]))
2737                     break
2738
2739     def _real_extract(self, url):
2740         mobj = re.match(self._VALID_URL, url)
2741         if mobj is None:
2742             raise ExtractorError(u'Invalid URL: %s' % url)
2743         # extract uploader & filename from url
2744         uploader = mobj.group(1).decode('utf-8')
2745         file_id = uploader + "-" + mobj.group(2).decode('utf-8')
2746
2747         # construct API request
2748         file_url = 'http://www.mixcloud.com/api/1/cloudcast/' + '/'.join(url.split('/')[-3:-1]) + '.json'
2749         # retrieve .json file with links to files
2750         request = compat_urllib_request.Request(file_url)
2751         try:
2752             self.report_download_json(file_url)
2753             jsonData = compat_urllib_request.urlopen(request).read()
2754         except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
2755             raise ExtractorError(u'Unable to retrieve file: %s' % compat_str(err))
2756
2757         # parse JSON
2758         json_data = json.loads(jsonData)
2759         player_url = json_data['player_swf_url']
2760         formats = dict(json_data['audio_formats'])
2761
2762         req_format = self._downloader.params.get('format', None)
2763         bitrate = None
2764
2765         if self._downloader.params.get('listformats', None):
2766             self._print_formats(formats)
2767             return
2768
2769         if req_format is None or req_format == 'best':
2770             for format_param in formats.keys():
2771                 url_list = self.get_urls(formats, format_param)
2772                 # check urls
2773                 file_url = self.check_urls(url_list)
2774                 if file_url is not None:
2775                     break # got it!
2776         else:
2777             if req_format not in formats:
2778                 raise ExtractorError(u'Format is not available')
2779
2780             url_list = self.get_urls(formats, req_format)
2781             file_url = self.check_urls(url_list)
2782             format_param = req_format
2783
2784         return [{
2785             'id': file_id.decode('utf-8'),
2786             'url': file_url.decode('utf-8'),
2787             'uploader': uploader.decode('utf-8'),
2788             'upload_date': None,
2789             'title': json_data['name'],
2790             'ext': file_url.split('.')[-1].decode('utf-8'),
2791             'format': (format_param is None and u'NA' or format_param.decode('utf-8')),
2792             'thumbnail': json_data['thumbnail_url'],
2793             'description': json_data['description'],
2794             'player_url': player_url.decode('utf-8'),
2795         }]
2796
2797 class StanfordOpenClassroomIE(InfoExtractor):
2798     """Information extractor for Stanford's Open ClassRoom"""
2799
2800     _VALID_URL = r'^(?:https?://)?openclassroom.stanford.edu(?P<path>/?|(/MainFolder/(?:HomePage|CoursePage|VideoPage)\.php([?]course=(?P<course>[^&]+)(&video=(?P<video>[^&]+))?(&.*)?)?))$'
2801     IE_NAME = u'stanfordoc'
2802
2803     def _real_extract(self, url):
2804         mobj = re.match(self._VALID_URL, url)
2805         if mobj is None:
2806             raise ExtractorError(u'Invalid URL: %s' % url)
2807
2808         if mobj.group('course') and mobj.group('video'): # A specific video
2809             course = mobj.group('course')
2810             video = mobj.group('video')
2811             info = {
2812                 'id': course + '_' + video,
2813                 'uploader': None,
2814                 'upload_date': None,
2815             }
2816
2817             self.report_extraction(info['id'])
2818             baseUrl = 'http://openclassroom.stanford.edu/MainFolder/courses/' + course + '/videos/'
2819             xmlUrl = baseUrl + video + '.xml'
2820             try:
2821                 metaXml = compat_urllib_request.urlopen(xmlUrl).read()
2822             except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
2823                 raise ExtractorError(u'Unable to download video info XML: %s' % compat_str(err))
2824             mdoc = xml.etree.ElementTree.fromstring(metaXml)
2825             try:
2826                 info['title'] = mdoc.findall('./title')[0].text
2827                 info['url'] = baseUrl + mdoc.findall('./videoFile')[0].text
2828             except IndexError:
2829                 raise ExtractorError(u'Invalid metadata XML file')
2830             info['ext'] = info['url'].rpartition('.')[2]
2831             return [info]
2832         elif mobj.group('course'): # A course page
2833             course = mobj.group('course')
2834             info = {
2835                 'id': course,
2836                 'type': 'playlist',
2837                 'uploader': None,
2838                 'upload_date': None,
2839             }
2840
2841             coursepage = self._download_webpage(url, info['id'],
2842                                         note='Downloading course info page',
2843                                         errnote='Unable to download course info page')
2844
2845             info['title'] = self._html_search_regex('<h1>([^<]+)</h1>', coursepage, 'title', default=info['id'])
2846
2847             info['description'] = self._html_search_regex('<description>([^<]+)</description>',
2848                 coursepage, u'description', fatal=False)
2849
2850             links = orderedSet(re.findall('<a href="(VideoPage.php\?[^"]+)">', coursepage))
2851             info['list'] = [
2852                 {
2853                     'type': 'reference',
2854                     'url': 'http://openclassroom.stanford.edu/MainFolder/' + unescapeHTML(vpage),
2855                 }
2856                     for vpage in links]
2857             results = []
2858             for entry in info['list']:
2859                 assert entry['type'] == 'reference'
2860                 results += self.extract(entry['url'])
2861             return results
2862         else: # Root page
2863             info = {
2864                 'id': 'Stanford OpenClassroom',
2865                 'type': 'playlist',
2866                 'uploader': None,
2867                 'upload_date': None,
2868             }
2869
2870             self.report_download_webpage(info['id'])
2871             rootURL = 'http://openclassroom.stanford.edu/MainFolder/HomePage.php'
2872             try:
2873                 rootpage = compat_urllib_request.urlopen(rootURL).read()
2874             except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
2875                 raise ExtractorError(u'Unable to download course info page: ' + compat_str(err))
2876
2877             info['title'] = info['id']
2878
2879             links = orderedSet(re.findall('<a href="(CoursePage.php\?[^"]+)">', rootpage))
2880             info['list'] = [
2881                 {
2882                     'type': 'reference',
2883                     'url': 'http://openclassroom.stanford.edu/MainFolder/' + unescapeHTML(cpage),
2884                 }
2885                     for cpage in links]
2886
2887             results = []
2888             for entry in info['list']:
2889                 assert entry['type'] == 'reference'
2890                 results += self.extract(entry['url'])
2891             return results
2892
2893 class MTVIE(InfoExtractor):
2894     """Information extractor for MTV.com"""
2895
2896     _VALID_URL = r'^(?P<proto>https?://)?(?:www\.)?mtv\.com/videos/[^/]+/(?P<videoid>[0-9]+)/[^/]+$'
2897     IE_NAME = u'mtv'
2898
2899     def _real_extract(self, url):
2900         mobj = re.match(self._VALID_URL, url)
2901         if mobj is None:
2902             raise ExtractorError(u'Invalid URL: %s' % url)
2903         if not mobj.group('proto'):
2904             url = 'http://' + url
2905         video_id = mobj.group('videoid')
2906
2907         webpage = self._download_webpage(url, video_id)
2908
2909         song_name = self._html_search_regex(r'<meta name="mtv_vt" content="([^"]+)"/>',
2910             webpage, u'song name', fatal=False)
2911
2912         video_title = self._html_search_regex(r'<meta name="mtv_an" content="([^"]+)"/>',
2913             webpage, u'title')
2914
2915         mtvn_uri = self._html_search_regex(r'<meta name="mtvn_uri" content="([^"]+)"/>',
2916             webpage, u'mtvn_uri', fatal=False)
2917
2918         content_id = self._search_regex(r'MTVN.Player.defaultPlaylistId = ([0-9]+);',
2919             webpage, u'content id', fatal=False)
2920
2921         videogen_url = 'http://www.mtv.com/player/includes/mediaGen.jhtml?uri=' + mtvn_uri + '&id=' + content_id + '&vid=' + video_id + '&ref=www.mtvn.com&viewUri=' + mtvn_uri
2922         self.report_extraction(video_id)
2923         request = compat_urllib_request.Request(videogen_url)
2924         try:
2925             metadataXml = compat_urllib_request.urlopen(request).read()
2926         except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
2927             raise ExtractorError(u'Unable to download video metadata: %s' % compat_str(err))
2928
2929         mdoc = xml.etree.ElementTree.fromstring(metadataXml)
2930         renditions = mdoc.findall('.//rendition')
2931
2932         # For now, always pick the highest quality.
2933         rendition = renditions[-1]
2934
2935         try:
2936             _,_,ext = rendition.attrib['type'].partition('/')
2937             format = ext + '-' + rendition.attrib['width'] + 'x' + rendition.attrib['height'] + '_' + rendition.attrib['bitrate']
2938             video_url = rendition.find('./src').text
2939         except KeyError:
2940             raise ExtractorError('Invalid rendition field.')
2941
2942         info = {
2943             'id': video_id,
2944             'url': video_url,
2945             'uploader': performer,
2946             'upload_date': None,
2947             'title': video_title,
2948             'ext': ext,
2949             'format': format,
2950         }
2951
2952         return [info]
2953
2954
2955 class YoukuIE(InfoExtractor):
2956     _VALID_URL =  r'(?:http://)?v\.youku\.com/v_show/id_(?P<ID>[A-Za-z0-9]+)\.html'
2957
2958     def _gen_sid(self):
2959         nowTime = int(time.time() * 1000)
2960         random1 = random.randint(1000,1998)
2961         random2 = random.randint(1000,9999)
2962
2963         return "%d%d%d" %(nowTime,random1,random2)
2964
2965     def _get_file_ID_mix_string(self, seed):
2966         mixed = []
2967         source = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\:._-1234567890")
2968         seed = float(seed)
2969         for i in range(len(source)):
2970             seed  =  (seed * 211 + 30031 ) % 65536
2971             index  =  math.floor(seed / 65536 * len(source) )
2972             mixed.append(source[int(index)])
2973             source.remove(source[int(index)])
2974         #return ''.join(mixed)
2975         return mixed
2976
2977     def _get_file_id(self, fileId, seed):
2978         mixed = self._get_file_ID_mix_string(seed)
2979         ids = fileId.split('*')
2980         realId = []
2981         for ch in ids:
2982             if ch:
2983                 realId.append(mixed[int(ch)])
2984         return ''.join(realId)
2985
2986     def _real_extract(self, url):
2987         mobj = re.match(self._VALID_URL, url)
2988         if mobj is None:
2989             raise ExtractorError(u'Invalid URL: %s' % url)
2990         video_id = mobj.group('ID')
2991
2992         info_url = 'http://v.youku.com/player/getPlayList/VideoIDS/' + video_id
2993
2994         jsondata = self._download_webpage(info_url, video_id)
2995
2996         self.report_extraction(video_id)
2997         try:
2998             config = json.loads(jsondata)
2999
3000             video_title =  config['data'][0]['title']
3001             seed = config['data'][0]['seed']
3002
3003             format = self._downloader.params.get('format', None)
3004             supported_format = list(config['data'][0]['streamfileids'].keys())
3005
3006             if format is None or format == 'best':
3007                 if 'hd2' in supported_format:
3008                     format = 'hd2'
3009                 else:
3010                     format = 'flv'
3011                 ext = u'flv'
3012             elif format == 'worst':
3013                 format = 'mp4'
3014                 ext = u'mp4'
3015             else:
3016                 format = 'flv'
3017                 ext = u'flv'
3018
3019
3020             fileid = config['data'][0]['streamfileids'][format]
3021             keys = [s['k'] for s in config['data'][0]['segs'][format]]
3022         except (UnicodeDecodeError, ValueError, KeyError):
3023             raise ExtractorError(u'Unable to extract info section')
3024
3025         files_info=[]
3026         sid = self._gen_sid()
3027         fileid = self._get_file_id(fileid, seed)
3028
3029         #column 8,9 of fileid represent the segment number
3030         #fileid[7:9] should be changed
3031         for index, key in enumerate(keys):
3032
3033             temp_fileid = '%s%02X%s' % (fileid[0:8], index, fileid[10:])
3034             download_url = 'http://f.youku.com/player/getFlvPath/sid/%s_%02X/st/flv/fileid/%s?k=%s' % (sid, index, temp_fileid, key)
3035
3036             info = {
3037                 'id': '%s_part%02d' % (video_id, index),
3038                 'url': download_url,
3039                 'uploader': None,
3040                 'upload_date': None,
3041                 'title': video_title,
3042                 'ext': ext,
3043             }
3044             files_info.append(info)
3045
3046         return files_info
3047
3048
3049 class XNXXIE(InfoExtractor):
3050     """Information extractor for xnxx.com"""
3051
3052     _VALID_URL = r'^(?:https?://)?video\.xnxx\.com/video([0-9]+)/(.*)'
3053     IE_NAME = u'xnxx'
3054     VIDEO_URL_RE = r'flv_url=(.*?)&amp;'
3055     VIDEO_TITLE_RE = r'<title>(.*?)\s+-\s+XNXX.COM'
3056     VIDEO_THUMB_RE = r'url_bigthumb=(.*?)&amp;'
3057
3058     def _real_extract(self, url):
3059         mobj = re.match(self._VALID_URL, url)
3060         if mobj is None:
3061             raise ExtractorError(u'Invalid URL: %s' % url)
3062         video_id = mobj.group(1)
3063
3064         # Get webpage content
3065         webpage = self._download_webpage(url, video_id)
3066
3067         video_url = self._search_regex(self.VIDEO_URL_RE,
3068             webpage, u'video URL')
3069         video_url = compat_urllib_parse.unquote(video_url)
3070
3071         video_title = self._html_search_regex(self.VIDEO_TITLE_RE,
3072             webpage, u'title')
3073
3074         video_thumbnail = self._search_regex(self.VIDEO_THUMB_RE,
3075             webpage, u'thumbnail', fatal=False)
3076
3077         return [{
3078             'id': video_id,
3079             'url': video_url,
3080             'uploader': None,
3081             'upload_date': None,
3082             'title': video_title,
3083             'ext': 'flv',
3084             'thumbnail': video_thumbnail,
3085             'description': None,
3086         }]
3087
3088
3089 class GooglePlusIE(InfoExtractor):
3090     """Information extractor for plus.google.com."""
3091
3092     _VALID_URL = r'(?:https://)?plus\.google\.com/(?:[^/]+/)*?posts/(\w+)'
3093     IE_NAME = u'plus.google'
3094
3095     def _real_extract(self, url):
3096         # Extract id from URL
3097         mobj = re.match(self._VALID_URL, url)
3098         if mobj is None:
3099             raise ExtractorError(u'Invalid URL: %s' % url)
3100
3101         post_url = mobj.group(0)
3102         video_id = mobj.group(1)
3103
3104         video_extension = 'flv'
3105
3106         # Step 1, Retrieve post webpage to extract further information
3107         webpage = self._download_webpage(post_url, video_id, u'Downloading entry webpage')
3108
3109         self.report_extraction(video_id)
3110
3111         # Extract update date
3112         upload_date = self._html_search_regex('title="Timestamp">(.*?)</a>',
3113             webpage, u'upload date', fatal=False)
3114         if upload_date:
3115             # Convert timestring to a format suitable for filename
3116             upload_date = datetime.datetime.strptime(upload_date, "%Y-%m-%d")
3117             upload_date = upload_date.strftime('%Y%m%d')
3118
3119         # Extract uploader
3120         uploader = self._html_search_regex(r'rel\="author".*?>(.*?)</a>',
3121             webpage, u'uploader', fatal=False)
3122
3123         # Extract title
3124         # Get the first line for title
3125         video_title = self._html_search_regex(r'<meta name\=\"Description\" content\=\"(.*?)[\n<"]',
3126             webpage, 'title', default=u'NA')
3127
3128         # Step 2, Stimulate clicking the image box to launch video
3129         video_page = self._search_regex('"(https\://plus\.google\.com/photos/.*?)",,"image/jpeg","video"\]',
3130             webpage, u'video page URL')
3131         webpage = self._download_webpage(video_page, video_id, u'Downloading video page')
3132
3133         # Extract video links on video page
3134         """Extract video links of all sizes"""
3135         pattern = '\d+,\d+,(\d+),"(http\://redirector\.googlevideo\.com.*?)"'
3136         mobj = re.findall(pattern, webpage)
3137         if len(mobj) == 0:
3138             raise ExtractorError(u'Unable to extract video links')
3139
3140         # Sort in resolution
3141         links = sorted(mobj)
3142
3143         # Choose the lowest of the sort, i.e. highest resolution
3144         video_url = links[-1]
3145         # Only get the url. The resolution part in the tuple has no use anymore
3146         video_url = video_url[-1]
3147         # Treat escaped \u0026 style hex
3148         try:
3149             video_url = video_url.decode("unicode_escape")
3150         except AttributeError: # Python 3
3151             video_url = bytes(video_url, 'ascii').decode('unicode-escape')
3152
3153
3154         return [{
3155             'id':       video_id,
3156             'url':      video_url,
3157             'uploader': uploader,
3158             'upload_date':  upload_date,
3159             'title':    video_title,
3160             'ext':      video_extension,
3161         }]
3162
3163 class NBAIE(InfoExtractor):
3164     _VALID_URL = r'^(?:https?://)?(?:watch\.|www\.)?nba\.com/(?:nba/)?video(/[^?]*?)(?:/index\.html)?(?:\?.*)?$'
3165     IE_NAME = u'nba'
3166
3167     def _real_extract(self, url):
3168         mobj = re.match(self._VALID_URL, url)
3169         if mobj is None:
3170             raise ExtractorError(u'Invalid URL: %s' % url)
3171
3172         video_id = mobj.group(1)
3173
3174         webpage = self._download_webpage(url, video_id)
3175
3176         video_url = u'http://ht-mobile.cdn.turner.com/nba/big' + video_id + '_nba_1280x720.mp4'
3177
3178         shortened_video_id = video_id.rpartition('/')[2]
3179         title = self._html_search_regex(r'<meta property="og:title" content="(.*?)"',
3180             webpage, 'title', default=shortened_video_id).replace('NBA.com: ', '')
3181
3182         # It isn't there in the HTML it returns to us
3183         # uploader_date = self._html_search_regex(r'<b>Date:</b> (.*?)</div>', webpage, 'upload_date', fatal=False)
3184
3185         description = self._html_search_regex(r'<meta name="description" (?:content|value)="(.*?)" />', webpage, 'description', fatal=False)
3186
3187         info = {
3188             'id': shortened_video_id,
3189             'url': video_url,
3190             'ext': 'mp4',
3191             'title': title,
3192             # 'uploader_date': uploader_date,
3193             'description': description,
3194         }
3195         return [info]
3196
3197 class JustinTVIE(InfoExtractor):
3198     """Information extractor for justin.tv and twitch.tv"""
3199     # TODO: One broadcast may be split into multiple videos. The key
3200     # 'broadcast_id' is the same for all parts, and 'broadcast_part'
3201     # starts at 1 and increases. Can we treat all parts as one video?
3202
3203     _VALID_URL = r"""(?x)^(?:http://)?(?:www\.)?(?:twitch|justin)\.tv/
3204         (?:
3205             (?P<channelid>[^/]+)|
3206             (?:(?:[^/]+)/b/(?P<videoid>[^/]+))|
3207             (?:(?:[^/]+)/c/(?P<chapterid>[^/]+))
3208         )
3209         /?(?:\#.*)?$
3210         """
3211     _JUSTIN_PAGE_LIMIT = 100
3212     IE_NAME = u'justin.tv'
3213
3214     def report_download_page(self, channel, offset):
3215         """Report attempt to download a single page of videos."""
3216         self.to_screen(u'%s: Downloading video information from %d to %d' %
3217                 (channel, offset, offset + self._JUSTIN_PAGE_LIMIT))
3218
3219     # Return count of items, list of *valid* items
3220     def _parse_page(self, url, video_id):
3221         webpage = self._download_webpage(url, video_id,
3222                                          u'Downloading video info JSON',
3223                                          u'unable to download video info JSON')
3224
3225         response = json.loads(webpage)
3226         if type(response) != list:
3227             error_text = response.get('error', 'unknown error')
3228             raise ExtractorError(u'Justin.tv API: %s' % error_text)
3229         info = []
3230         for clip in response:
3231             video_url = clip['video_file_url']
3232             if video_url:
3233                 video_extension = os.path.splitext(video_url)[1][1:]
3234                 video_date = re.sub('-', '', clip['start_time'][:10])
3235                 video_uploader_id = clip.get('user_id', clip.get('channel_id'))
3236                 video_id = clip['id']
3237                 video_title = clip.get('title', video_id)
3238                 info.append({
3239                     'id': video_id,
3240                     'url': video_url,
3241                     'title': video_title,
3242                     'uploader': clip.get('channel_name', video_uploader_id),
3243                     'uploader_id': video_uploader_id,
3244                     'upload_date': video_date,
3245                     'ext': video_extension,
3246                 })
3247         return (len(response), info)
3248
3249     def _real_extract(self, url):
3250         mobj = re.match(self._VALID_URL, url)
3251         if mobj is None:
3252             raise ExtractorError(u'invalid URL: %s' % url)
3253
3254         api_base = 'http://api.justin.tv'
3255         paged = False
3256         if mobj.group('channelid'):
3257             paged = True
3258             video_id = mobj.group('channelid')
3259             api = api_base + '/channel/archives/%s.json' % video_id
3260         elif mobj.group('chapterid'):
3261             chapter_id = mobj.group('chapterid')
3262
3263             webpage = self._download_webpage(url, chapter_id)
3264             m = re.search(r'PP\.archive_id = "([0-9]+)";', webpage)
3265             if not m:
3266                 raise ExtractorError(u'Cannot find archive of a chapter')
3267             archive_id = m.group(1)
3268
3269             api = api_base + '/broadcast/by_chapter/%s.xml' % chapter_id
3270             chapter_info_xml = self._download_webpage(api, chapter_id,
3271                                              note=u'Downloading chapter information',
3272                                              errnote=u'Chapter information download failed')
3273             doc = xml.etree.ElementTree.fromstring(chapter_info_xml)
3274             for a in doc.findall('.//archive'):
3275                 if archive_id == a.find('./id').text:
3276                     break
3277             else:
3278                 raise ExtractorError(u'Could not find chapter in chapter information')
3279
3280             video_url = a.find('./video_file_url').text
3281             video_ext = video_url.rpartition('.')[2] or u'flv'
3282
3283             chapter_api_url = u'https://api.twitch.tv/kraken/videos/c' + chapter_id
3284             chapter_info_json = self._download_webpage(chapter_api_url, u'c' + chapter_id,
3285                                    note='Downloading chapter metadata',
3286                                    errnote='Download of chapter metadata failed')
3287             chapter_info = json.loads(chapter_info_json)
3288
3289             bracket_start = int(doc.find('.//bracket_start').text)
3290             bracket_end = int(doc.find('.//bracket_end').text)
3291
3292             # TODO determine start (and probably fix up file)
3293             #  youtube-dl -v http://www.twitch.tv/firmbelief/c/1757457
3294             #video_url += u'?start=' + TODO:start_timestamp
3295             # bracket_start is 13290, but we want 51670615
3296             self._downloader.report_warning(u'Chapter detected, but we can just download the whole file. '
3297                                             u'Chapter starts at %s and ends at %s' % (formatSeconds(bracket_start), formatSeconds(bracket_end)))
3298
3299             info = {
3300                 'id': u'c' + chapter_id,
3301                 'url': video_url,
3302                 'ext': video_ext,
3303                 'title': chapter_info['title'],
3304                 'thumbnail': chapter_info['preview'],
3305                 'description': chapter_info['description'],
3306                 'uploader': chapter_info['channel']['display_name'],
3307                 'uploader_id': chapter_info['channel']['name'],
3308             }
3309             return [info]
3310         else:
3311             video_id = mobj.group('videoid')
3312             api = api_base + '/broadcast/by_archive/%s.json' % video_id
3313
3314         self.report_extraction(video_id)
3315
3316         info = []
3317         offset = 0
3318         limit = self._JUSTIN_PAGE_LIMIT
3319         while True:
3320             if paged:
3321                 self.report_download_page(video_id, offset)
3322             page_url = api + ('?offset=%d&limit=%d' % (offset, limit))
3323             page_count, page_info = self._parse_page(page_url, video_id)
3324             info.extend(page_info)
3325             if not paged or page_count != limit:
3326                 break
3327             offset += limit
3328         return info
3329
3330 class FunnyOrDieIE(InfoExtractor):
3331     _VALID_URL = r'^(?:https?://)?(?:www\.)?funnyordie\.com/videos/(?P<id>[0-9a-f]+)/.*$'
3332
3333     def _real_extract(self, url):
3334         mobj = re.match(self._VALID_URL, url)
3335         if mobj is None:
3336             raise ExtractorError(u'invalid URL: %s' % url)
3337
3338         video_id = mobj.group('id')
3339         webpage = self._download_webpage(url, video_id)
3340
3341         video_url = self._html_search_regex(r'<video[^>]*>\s*<source[^>]*>\s*<source src="(?P<url>[^"]+)"',
3342             webpage, u'video URL', flags=re.DOTALL)
3343
3344         title = self._html_search_regex((r"<h1 class='player_page_h1'.*?>(?P<title>.*?)</h1>",
3345             r'<title>(?P<title>[^<]+?)</title>'), webpage, 'title', flags=re.DOTALL)
3346
3347         video_description = self._html_search_regex(r'<meta property="og:description" content="(?P<desc>.*?)"',
3348             webpage, u'description', fatal=False, flags=re.DOTALL)
3349
3350         info = {
3351             'id': video_id,
3352             'url': video_url,
3353             'ext': 'mp4',
3354             'title': title,
3355             'description': video_description,
3356         }
3357         return [info]
3358
3359 class SteamIE(InfoExtractor):
3360     _VALID_URL = r"""http://store\.steampowered\.com/
3361                 (agecheck/)?
3362                 (?P<urltype>video|app)/ #If the page is only for videos or for a game
3363                 (?P<gameID>\d+)/?
3364                 (?P<videoID>\d*)(?P<extra>\??) #For urltype == video we sometimes get the videoID
3365                 """
3366
3367     @classmethod
3368     def suitable(cls, url):
3369         """Receives a URL and returns True if suitable for this IE."""
3370         return re.match(cls._VALID_URL, url, re.VERBOSE) is not None
3371
3372     def _real_extract(self, url):
3373         m = re.match(self._VALID_URL, url, re.VERBOSE)
3374         gameID = m.group('gameID')
3375         videourl = 'http://store.steampowered.com/agecheck/video/%s/?snr=1_agecheck_agecheck__age-gate&ageDay=1&ageMonth=January&ageYear=1970' % gameID
3376         self.report_age_confirmation()
3377         webpage = self._download_webpage(videourl, gameID)
3378         game_title = re.search(r'<h2 class="pageheader">(?P<game_title>.*?)</h2>', webpage).group('game_title')
3379         
3380         urlRE = r"'movie_(?P<videoID>\d+)': \{\s*FILENAME: \"(?P<videoURL>[\w:/\.\?=]+)\"(,\s*MOVIE_NAME: \"(?P<videoName>[\w:/\.\?=\+-]+)\")?\s*\},"
3381         mweb = re.finditer(urlRE, webpage)
3382         namesRE = r'<span class="title">(?P<videoName>.+?)</span>'
3383         titles = re.finditer(namesRE, webpage)
3384         thumbsRE = r'<img class="movie_thumb" src="(?P<thumbnail>.+?)">'
3385         thumbs = re.finditer(thumbsRE, webpage)
3386         videos = []
3387         for vid,vtitle,thumb in zip(mweb,titles,thumbs):
3388             video_id = vid.group('videoID')
3389             title = vtitle.group('videoName')
3390             video_url = vid.group('videoURL')
3391             video_thumb = thumb.group('thumbnail')
3392             if not video_url:
3393                 raise ExtractorError(u'Cannot find video url for %s' % video_id)
3394             info = {
3395                 'id':video_id,
3396                 'url':video_url,
3397                 'ext': 'flv',
3398                 'title': unescapeHTML(title),
3399                 'thumbnail': video_thumb
3400                   }
3401             videos.append(info)
3402         return [self.playlist_result(videos, gameID, game_title)]
3403
3404 class UstreamIE(InfoExtractor):
3405     _VALID_URL = r'https?://www\.ustream\.tv/recorded/(?P<videoID>\d+)'
3406     IE_NAME = u'ustream'
3407
3408     def _real_extract(self, url):
3409         m = re.match(self._VALID_URL, url)
3410         video_id = m.group('videoID')
3411
3412         video_url = u'http://tcdn.ustream.tv/video/%s' % video_id
3413         webpage = self._download_webpage(url, video_id)
3414
3415         self.report_extraction(video_id)
3416
3417         video_title = self._html_search_regex(r'data-title="(?P<title>.+)"',
3418             webpage, u'title')
3419
3420         uploader = self._html_search_regex(r'data-content-type="channel".*?>(?P<uploader>.*?)</a>',
3421             webpage, u'uploader', fatal=False, flags=re.DOTALL)
3422
3423         thumbnail = self._html_search_regex(r'<link rel="image_src" href="(?P<thumb>.*?)"',
3424             webpage, u'thumbnail', fatal=False)
3425
3426         info = {
3427                 'id': video_id,
3428                 'url': video_url,
3429                 'ext': 'flv',
3430                 'title': video_title,
3431                 'uploader': uploader,
3432                 'thumbnail': thumbnail,
3433                }
3434         return info
3435
3436 class WorldStarHipHopIE(InfoExtractor):
3437     _VALID_URL = r'https?://(?:www|m)\.worldstar(?:candy|hiphop)\.com/videos/video\.php\?v=(?P<id>.*)'
3438     IE_NAME = u'WorldStarHipHop'
3439
3440     def _real_extract(self, url):
3441         m = re.match(self._VALID_URL, url)
3442         video_id = m.group('id')
3443
3444         webpage_src = self._download_webpage(url, video_id)
3445
3446         video_url = self._search_regex(r'so\.addVariable\("file","(.*?)"\)',
3447             webpage_src, u'video URL')
3448
3449         if 'mp4' in video_url:
3450             ext = 'mp4'
3451         else:
3452             ext = 'flv'
3453
3454         video_title = self._html_search_regex(r"<title>(.*)</title>",
3455             webpage_src, u'title')
3456
3457         # Getting thumbnail and if not thumbnail sets correct title for WSHH candy video.
3458         thumbnail = self._html_search_regex(r'rel="image_src" href="(.*)" />',
3459             webpage_src, u'thumbnail', fatal=False)
3460
3461         if not thumbnail:
3462             _title = r"""candytitles.*>(.*)</span>"""
3463             mobj = re.search(_title, webpage_src)
3464             if mobj is not None:
3465                 video_title = mobj.group(1)
3466
3467         results = [{
3468                     'id': video_id,
3469                     'url' : video_url,
3470                     'title' : video_title,
3471                     'thumbnail' : thumbnail,
3472                     'ext' : ext,
3473                     }]
3474         return results
3475
3476 class RBMARadioIE(InfoExtractor):
3477     _VALID_URL = r'https?://(?:www\.)?rbmaradio\.com/shows/(?P<videoID>[^/]+)$'
3478
3479     def _real_extract(self, url):
3480         m = re.match(self._VALID_URL, url)
3481         video_id = m.group('videoID')
3482
3483         webpage = self._download_webpage(url, video_id)
3484
3485         json_data = self._search_regex(r'<script>window.gon = {.*?};gon\.show=(.+?);</script>',
3486             webpage, u'json data')
3487
3488         try:
3489             data = json.loads(json_data)
3490         except ValueError as e:
3491             raise ExtractorError(u'Invalid JSON: ' + str(e))
3492
3493         video_url = data['akamai_url'] + '&cbr=256'
3494         url_parts = compat_urllib_parse_urlparse(video_url)
3495         video_ext = url_parts.path.rpartition('.')[2]
3496         info = {
3497                 'id': video_id,
3498                 'url': video_url,
3499                 'ext': video_ext,
3500                 'title': data['title'],
3501                 'description': data.get('teaser_text'),
3502                 'location': data.get('country_of_origin'),
3503                 'uploader': data.get('host', {}).get('name'),
3504                 'uploader_id': data.get('host', {}).get('slug'),
3505                 'thumbnail': data.get('image', {}).get('large_url_2x'),
3506                 'duration': data.get('duration'),
3507         }
3508         return [info]
3509
3510
3511 class YouPornIE(InfoExtractor):
3512     """Information extractor for youporn.com."""
3513     _VALID_URL = r'^(?:https?://)?(?:\w+\.)?youporn\.com/watch/(?P<videoid>[0-9]+)/(?P<title>[^/]+)'
3514
3515     def _print_formats(self, formats):
3516         """Print all available formats"""
3517         print(u'Available formats:')
3518         print(u'ext\t\tformat')
3519         print(u'---------------------------------')
3520         for format in formats:
3521             print(u'%s\t\t%s'  % (format['ext'], format['format']))
3522
3523     def _specific(self, req_format, formats):
3524         for x in formats:
3525             if(x["format"]==req_format):
3526                 return x
3527         return None
3528
3529     def _real_extract(self, url):
3530         mobj = re.match(self._VALID_URL, url)
3531         if mobj is None:
3532             raise ExtractorError(u'Invalid URL: %s' % url)
3533         video_id = mobj.group('videoid')
3534
3535         req = compat_urllib_request.Request(url)
3536         req.add_header('Cookie', 'age_verified=1')
3537         webpage = self._download_webpage(req, video_id)
3538
3539         # Get JSON parameters
3540         json_params = self._search_regex(r'var currentVideo = new Video\((.*)\);', webpage, u'JSON parameters')
3541         try:
3542             params = json.loads(json_params)
3543         except:
3544             raise ExtractorError(u'Invalid JSON')
3545
3546         self.report_extraction(video_id)
3547         try:
3548             video_title = params['title']
3549             upload_date = unified_strdate(params['release_date_f'])
3550             video_description = params['description']
3551             video_uploader = params['submitted_by']
3552             thumbnail = params['thumbnails'][0]['image']
3553         except KeyError:
3554             raise ExtractorError('Missing JSON parameter: ' + sys.exc_info()[1])
3555
3556         # Get all of the formats available
3557         DOWNLOAD_LIST_RE = r'(?s)<ul class="downloadList">(?P<download_list>.*?)</ul>'
3558         download_list_html = self._search_regex(DOWNLOAD_LIST_RE,
3559             webpage, u'download list').strip()
3560
3561         # Get all of the links from the page
3562         LINK_RE = r'(?s)<a href="(?P<url>[^"]+)">'
3563         links = re.findall(LINK_RE, download_list_html)
3564         if(len(links) == 0):
3565             raise ExtractorError(u'ERROR: no known formats available for video')
3566
3567         self.to_screen(u'Links found: %d' % len(links))
3568
3569         formats = []
3570         for link in links:
3571
3572             # A link looks like this:
3573             # http://cdn1.download.youporn.phncdn.com/201210/31/8004515/480p_370k_8004515/YouPorn%20-%20Nubile%20Films%20The%20Pillow%20Fight.mp4?nvb=20121113051249&nva=20121114051249&ir=1200&sr=1200&hash=014b882080310e95fb6a0
3574             # A path looks like this:
3575             # /201210/31/8004515/480p_370k_8004515/YouPorn%20-%20Nubile%20Films%20The%20Pillow%20Fight.mp4
3576             video_url = unescapeHTML( link )
3577             path = compat_urllib_parse_urlparse( video_url ).path
3578             extension = os.path.splitext( path )[1][1:]
3579             format = path.split('/')[4].split('_')[:2]
3580             size = format[0]
3581             bitrate = format[1]
3582             format = "-".join( format )
3583             # title = u'%s-%s-%s' % (video_title, size, bitrate)
3584
3585             formats.append({
3586                 'id': video_id,
3587                 'url': video_url,
3588                 'uploader': video_uploader,
3589                 'upload_date': upload_date,
3590                 'title': video_title,
3591                 'ext': extension,
3592                 'format': format,
3593                 'thumbnail': thumbnail,
3594                 'description': video_description
3595             })
3596
3597         if self._downloader.params.get('listformats', None):
3598             self._print_formats(formats)
3599             return
3600
3601         req_format = self._downloader.params.get('format', None)
3602         self.to_screen(u'Format: %s' % req_format)
3603
3604         if req_format is None or req_format == 'best':
3605             return [formats[0]]
3606         elif req_format == 'worst':
3607             return [formats[-1]]
3608         elif req_format in ('-1', 'all'):
3609             return formats
3610         else:
3611             format = self._specific( req_format, formats )
3612             if result is None:
3613                 raise ExtractorError(u'Requested format not available')
3614             return [format]
3615
3616
3617
3618 class PornotubeIE(InfoExtractor):
3619     """Information extractor for pornotube.com."""
3620     _VALID_URL = r'^(?:https?://)?(?:\w+\.)?pornotube\.com(/c/(?P<channel>[0-9]+))?(/m/(?P<videoid>[0-9]+))(/(?P<title>.+))$'
3621
3622     def _real_extract(self, url):
3623         mobj = re.match(self._VALID_URL, url)
3624         if mobj is None:
3625             raise ExtractorError(u'Invalid URL: %s' % url)
3626
3627         video_id = mobj.group('videoid')
3628         video_title = mobj.group('title')
3629
3630         # Get webpage content
3631         webpage = self._download_webpage(url, video_id)
3632
3633         # Get the video URL
3634         VIDEO_URL_RE = r'url: "(?P<url>http://video[0-9].pornotube.com/.+\.flv)",'
3635         video_url = self._search_regex(VIDEO_URL_RE, webpage, u'video url')
3636         video_url = compat_urllib_parse.unquote(video_url)
3637
3638         #Get the uploaded date
3639         VIDEO_UPLOADED_RE = r'<div class="video_added_by">Added (?P<date>[0-9\/]+) by'
3640         upload_date = self._html_search_regex(VIDEO_UPLOADED_RE, webpage, u'upload date', fatal=False)
3641         if upload_date: upload_date = unified_strdate(upload_date)
3642
3643         info = {'id': video_id,
3644                 'url': video_url,
3645                 'uploader': None,
3646                 'upload_date': upload_date,
3647                 'title': video_title,
3648                 'ext': 'flv',
3649                 'format': 'flv'}
3650
3651         return [info]
3652
3653 class YouJizzIE(InfoExtractor):
3654     """Information extractor for youjizz.com."""
3655     _VALID_URL = r'^(?:https?://)?(?:\w+\.)?youjizz\.com/videos/(?P<videoid>[^.]+).html$'
3656
3657     def _real_extract(self, url):
3658         mobj = re.match(self._VALID_URL, url)
3659         if mobj is None:
3660             raise ExtractorError(u'Invalid URL: %s' % url)
3661
3662         video_id = mobj.group('videoid')
3663
3664         # Get webpage content
3665         webpage = self._download_webpage(url, video_id)
3666
3667         # Get the video title
3668         video_title = self._html_search_regex(r'<title>(?P<title>.*)</title>',
3669             webpage, u'title').strip()
3670
3671         # Get the embed page
3672         result = re.search(r'https?://www.youjizz.com/videos/embed/(?P<videoid>[0-9]+)', webpage)
3673         if result is None:
3674             raise ExtractorError(u'ERROR: unable to extract embed page')
3675
3676         embed_page_url = result.group(0).strip()
3677         video_id = result.group('videoid')
3678
3679         webpage = self._download_webpage(embed_page_url, video_id)
3680
3681         # Get the video URL
3682         video_url = self._search_regex(r'so.addVariable\("file",encodeURIComponent\("(?P<source>[^"]+)"\)\);',
3683             webpage, u'video URL')
3684
3685         info = {'id': video_id,
3686                 'url': video_url,
3687                 'title': video_title,
3688                 'ext': 'flv',
3689                 'format': 'flv',
3690                 'player_url': embed_page_url}
3691
3692         return [info]
3693
3694 class EightTracksIE(InfoExtractor):
3695     IE_NAME = '8tracks'
3696     _VALID_URL = r'https?://8tracks.com/(?P<user>[^/]+)/(?P<id>[^/#]+)(?:#.*)?$'
3697
3698     def _real_extract(self, url):
3699         mobj = re.match(self._VALID_URL, url)
3700         if mobj is None:
3701             raise ExtractorError(u'Invalid URL: %s' % url)
3702         playlist_id = mobj.group('id')
3703
3704         webpage = self._download_webpage(url, playlist_id)
3705
3706         json_like = self._search_regex(r"PAGE.mix = (.*?);\n", webpage, u'trax information', flags=re.DOTALL)
3707         data = json.loads(json_like)
3708
3709         session = str(random.randint(0, 1000000000))
3710         mix_id = data['id']
3711         track_count = data['tracks_count']
3712         first_url = 'http://8tracks.com/sets/%s/play?player=sm&mix_id=%s&format=jsonh' % (session, mix_id)
3713         next_url = first_url
3714         res = []
3715         for i in itertools.count():
3716             api_json = self._download_webpage(next_url, playlist_id,
3717                 note=u'Downloading song information %s/%s' % (str(i+1), track_count),
3718                 errnote=u'Failed to download song information')
3719             api_data = json.loads(api_json)
3720             track_data = api_data[u'set']['track']
3721             info = {
3722                 'id': track_data['id'],
3723                 'url': track_data['track_file_stream_url'],
3724                 'title': track_data['performer'] + u' - ' + track_data['name'],
3725                 'raw_title': track_data['name'],
3726                 'uploader_id': data['user']['login'],
3727                 'ext': 'm4a',
3728             }
3729             res.append(info)
3730             if api_data['set']['at_last_track']:
3731                 break
3732             next_url = 'http://8tracks.com/sets/%s/next?player=sm&mix_id=%s&format=jsonh&track_id=%s' % (session, mix_id, track_data['id'])
3733         return res
3734
3735 class KeekIE(InfoExtractor):
3736     _VALID_URL = r'http://(?:www\.)?keek\.com/(?:!|\w+/keeks/)(?P<videoID>\w+)'
3737     IE_NAME = u'keek'
3738
3739     def _real_extract(self, url):
3740         m = re.match(self._VALID_URL, url)
3741         video_id = m.group('videoID')
3742
3743         video_url = u'http://cdn.keek.com/keek/video/%s' % video_id
3744         thumbnail = u'http://cdn.keek.com/keek/thumbnail/%s/w100/h75' % video_id
3745         webpage = self._download_webpage(url, video_id)
3746
3747         video_title = self._html_search_regex(r'<meta property="og:title" content="(?P<title>.*?)"',
3748             webpage, u'title')
3749
3750         uploader = self._html_search_regex(r'<div class="user-name-and-bio">[\S\s]+?<h2>(?P<uploader>.+?)</h2>',
3751             webpage, u'uploader', fatal=False)
3752
3753         info = {
3754                 'id': video_id,
3755                 'url': video_url,
3756                 'ext': 'mp4',
3757                 'title': video_title,
3758                 'thumbnail': thumbnail,
3759                 'uploader': uploader
3760         }
3761         return [info]
3762
3763 class TEDIE(InfoExtractor):
3764     _VALID_URL=r'''http://www\.ted\.com/
3765                    (
3766                         ((?P<type_playlist>playlists)/(?P<playlist_id>\d+)) # We have a playlist
3767                         |
3768                         ((?P<type_talk>talks)) # We have a simple talk
3769                    )
3770                    (/lang/(.*?))? # The url may contain the language
3771                    /(?P<name>\w+) # Here goes the name and then ".html"
3772                    '''
3773
3774     @classmethod
3775     def suitable(cls, url):
3776         """Receives a URL and returns True if suitable for this IE."""
3777         return re.match(cls._VALID_URL, url, re.VERBOSE) is not None
3778
3779     def _real_extract(self, url):
3780         m=re.match(self._VALID_URL, url, re.VERBOSE)
3781         if m.group('type_talk'):
3782             return [self._talk_info(url)]
3783         else :
3784             playlist_id=m.group('playlist_id')
3785             name=m.group('name')
3786             self.to_screen(u'Getting info of playlist %s: "%s"' % (playlist_id,name))
3787             return [self._playlist_videos_info(url,name,playlist_id)]
3788
3789     def _talk_video_link(self,mediaSlug):
3790         '''Returns the video link for that mediaSlug'''
3791         return 'http://download.ted.com/talks/%s.mp4' % mediaSlug
3792
3793     def _playlist_videos_info(self,url,name,playlist_id=0):
3794         '''Returns the videos of the playlist'''
3795         video_RE=r'''
3796                      <li\ id="talk_(\d+)"([.\s]*?)data-id="(?P<video_id>\d+)"
3797                      ([.\s]*?)data-playlist_item_id="(\d+)"
3798                      ([.\s]*?)data-mediaslug="(?P<mediaSlug>.+?)"
3799                      '''
3800         video_name_RE=r'<p\ class="talk-title"><a href="(?P<talk_url>/talks/(.+).html)">(?P<fullname>.+?)</a></p>'
3801         webpage=self._download_webpage(url, playlist_id, 'Downloading playlist webpage')
3802         m_videos=re.finditer(video_RE,webpage,re.VERBOSE)
3803         m_names=re.finditer(video_name_RE,webpage)
3804
3805         playlist_RE = r'div class="headline">(\s*?)<h1>(\s*?)<span>(?P<playlist_title>.*?)</span>'
3806         m_playlist = re.search(playlist_RE, webpage)
3807         playlist_title = m_playlist.group('playlist_title')
3808
3809         playlist_entries = []
3810         for m_video, m_name in zip(m_videos,m_names):
3811             video_id=m_video.group('video_id')
3812             talk_url='http://www.ted.com%s' % m_name.group('talk_url')
3813             playlist_entries.append(self.url_result(talk_url, 'TED'))
3814         return self.playlist_result(playlist_entries, playlist_id = playlist_id, playlist_title = playlist_title)
3815
3816     def _talk_info(self, url, video_id=0):
3817         """Return the video for the talk in the url"""
3818         m=re.match(self._VALID_URL, url,re.VERBOSE)
3819         videoName=m.group('name')
3820         webpage=self._download_webpage(url, video_id, 'Downloading \"%s\" page' % videoName)
3821         # If the url includes the language we get the title translated
3822         title_RE=r'<span id="altHeadline" >(?P<title>.*)</span>'
3823         title=re.search(title_RE, webpage).group('title')
3824         info_RE=r'''<script\ type="text/javascript">var\ talkDetails\ =(.*?)
3825                         "id":(?P<videoID>[\d]+).*?
3826                         "mediaSlug":"(?P<mediaSlug>[\w\d]+?)"'''
3827         thumb_RE=r'</span>[\s.]*</div>[\s.]*<img src="(?P<thumbnail>.*?)"'
3828         thumb_match=re.search(thumb_RE,webpage)
3829         info_match=re.search(info_RE,webpage,re.VERBOSE)
3830         video_id=info_match.group('videoID')
3831         mediaSlug=info_match.group('mediaSlug')
3832         video_url=self._talk_video_link(mediaSlug)
3833         info = {
3834                 'id': video_id,
3835                 'url': video_url,
3836                 'ext': 'mp4',
3837                 'title': title,
3838                 'thumbnail': thumb_match.group('thumbnail')
3839                 }
3840         return info
3841
3842 class MySpassIE(InfoExtractor):
3843     _VALID_URL = r'http://www.myspass.de/.*'
3844
3845     def _real_extract(self, url):
3846         META_DATA_URL_TEMPLATE = 'http://www.myspass.de/myspass/includes/apps/video/getvideometadataxml.php?id=%s'
3847
3848         # video id is the last path element of the URL
3849         # usually there is a trailing slash, so also try the second but last
3850         url_path = compat_urllib_parse_urlparse(url).path
3851         url_parent_path, video_id = os.path.split(url_path)
3852         if not video_id:
3853             _, video_id = os.path.split(url_parent_path)
3854
3855         # get metadata
3856         metadata_url = META_DATA_URL_TEMPLATE % video_id
3857         metadata_text = self._download_webpage(metadata_url, video_id)
3858         metadata = xml.etree.ElementTree.fromstring(metadata_text.encode('utf-8'))
3859
3860         # extract values from metadata
3861         url_flv_el = metadata.find('url_flv')
3862         if url_flv_el is None:
3863             raise ExtractorError(u'Unable to extract download url')
3864         video_url = url_flv_el.text
3865         extension = os.path.splitext(video_url)[1][1:]
3866         title_el = metadata.find('title')
3867         if title_el is None:
3868             raise ExtractorError(u'Unable to extract title')
3869         title = title_el.text
3870         format_id_el = metadata.find('format_id')
3871         if format_id_el is None:
3872             format = ext
3873         else:
3874             format = format_id_el.text
3875         description_el = metadata.find('description')
3876         if description_el is not None:
3877             description = description_el.text
3878         else:
3879             description = None
3880         imagePreview_el = metadata.find('imagePreview')
3881         if imagePreview_el is not None:
3882             thumbnail = imagePreview_el.text
3883         else:
3884             thumbnail = None
3885         info = {
3886             'id': video_id,
3887             'url': video_url,
3888             'title': title,
3889             'ext': extension,
3890             'format': format,
3891             'thumbnail': thumbnail,
3892             'description': description
3893         }
3894         return [info]
3895
3896 class SpiegelIE(InfoExtractor):
3897     _VALID_URL = r'https?://(?:www\.)?spiegel\.de/video/[^/]*-(?P<videoID>[0-9]+)(?:\.html)?(?:#.*)?$'
3898
3899     def _real_extract(self, url):
3900         m = re.match(self._VALID_URL, url)
3901         video_id = m.group('videoID')
3902
3903         webpage = self._download_webpage(url, video_id)
3904
3905         video_title = self._html_search_regex(r'<div class="module-title">(.*?)</div>',
3906             webpage, u'title')
3907
3908         xml_url = u'http://video2.spiegel.de/flash/' + video_id + u'.xml'
3909         xml_code = self._download_webpage(xml_url, video_id,
3910                     note=u'Downloading XML', errnote=u'Failed to download XML')
3911
3912         idoc = xml.etree.ElementTree.fromstring(xml_code)
3913         last_type = idoc[-1]
3914         filename = last_type.findall('./filename')[0].text
3915         duration = float(last_type.findall('./duration')[0].text)
3916
3917         video_url = 'http://video2.spiegel.de/flash/' + filename
3918         video_ext = filename.rpartition('.')[2]
3919         info = {
3920             'id': video_id,
3921             'url': video_url,
3922             'ext': video_ext,
3923             'title': video_title,
3924             'duration': duration,
3925         }
3926         return [info]
3927
3928 class LiveLeakIE(InfoExtractor):
3929
3930     _VALID_URL = r'^(?:http?://)?(?:\w+\.)?liveleak\.com/view\?(?:.*?)i=(?P<video_id>[\w_]+)(?:.*)'
3931     IE_NAME = u'liveleak'
3932
3933     def _real_extract(self, url):
3934         mobj = re.match(self._VALID_URL, url)
3935         if mobj is None:
3936             raise ExtractorError(u'Invalid URL: %s' % url)
3937
3938         video_id = mobj.group('video_id')
3939
3940         webpage = self._download_webpage(url, video_id)
3941
3942         video_url = self._search_regex(r'file: "(.*?)",',
3943             webpage, u'video URL')
3944
3945         video_title = self._html_search_regex(r'<meta property="og:title" content="(?P<title>.*?)"',
3946             webpage, u'title').replace('LiveLeak.com -', '').strip()
3947
3948         video_description = self._html_search_regex(r'<meta property="og:description" content="(?P<desc>.*?)"',
3949             webpage, u'description', fatal=False)
3950
3951         video_uploader = self._html_search_regex(r'By:.*?(\w+)</a>',
3952             webpage, u'uploader', fatal=False)
3953
3954         info = {
3955             'id':  video_id,
3956             'url': video_url,
3957             'ext': 'mp4',
3958             'title': video_title,
3959             'description': video_description,
3960             'uploader': video_uploader
3961         }
3962
3963         return [info]
3964
3965 class ARDIE(InfoExtractor):
3966     _VALID_URL = r'^(?:https?://)?(?:(?:www\.)?ardmediathek\.de|mediathek\.daserste\.de)/(?:.*/)(?P<video_id>[^/\?]+)(?:\?.*)?'
3967     _TITLE = r'<h1(?: class="boxTopHeadline")?>(?P<title>.*)</h1>'
3968     _MEDIA_STREAM = r'mediaCollection\.addMediaStream\((?P<media_type>\d+), (?P<quality>\d+), "(?P<rtmp_url>[^"]*)", "(?P<video_url>[^"]*)", "[^"]*"\)'
3969
3970     def _real_extract(self, url):
3971         # determine video id from url
3972         m = re.match(self._VALID_URL, url)
3973
3974         numid = re.search(r'documentId=([0-9]+)', url)
3975         if numid:
3976             video_id = numid.group(1)
3977         else:
3978             video_id = m.group('video_id')
3979
3980         # determine title and media streams from webpage
3981         html = self._download_webpage(url, video_id)
3982         title = re.search(self._TITLE, html).group('title')
3983         streams = [m.groupdict() for m in re.finditer(self._MEDIA_STREAM, html)]
3984         if not streams:
3985             assert '"fsk"' in html
3986             raise ExtractorError(u'This video is only available after 8:00 pm')
3987
3988         # choose default media type and highest quality for now
3989         stream = max([s for s in streams if int(s["media_type"]) == 0],
3990                      key=lambda s: int(s["quality"]))
3991
3992         # there's two possibilities: RTMP stream or HTTP download
3993         info = {'id': video_id, 'title': title, 'ext': 'mp4'}
3994         if stream['rtmp_url']:
3995             self.to_screen(u'RTMP download detected')
3996             assert stream['video_url'].startswith('mp4:')
3997             info["url"] = stream["rtmp_url"]
3998             info["play_path"] = stream['video_url']
3999         else:
4000             assert stream["video_url"].endswith('.mp4')
4001             info["url"] = stream["video_url"]
4002         return [info]
4003
4004 class TumblrIE(InfoExtractor):
4005     _VALID_URL = r'http://(?P<blog_name>.*?)\.tumblr\.com/((post)|(video))/(?P<id>\d*)/(.*?)'
4006
4007     def _real_extract(self, url):
4008         m_url = re.match(self._VALID_URL, url)
4009         video_id = m_url.group('id')
4010         blog = m_url.group('blog_name')
4011
4012         url = 'http://%s.tumblr.com/post/%s/' % (blog, video_id)
4013         webpage = self._download_webpage(url, video_id)
4014
4015         re_video = r'src=\\x22(?P<video_url>http://%s\.tumblr\.com/video_file/%s/(.*?))\\x22 type=\\x22video/(?P<ext>.*?)\\x22' % (blog, video_id)
4016         video = re.search(re_video, webpage)
4017         if video is None:
4018            raise ExtractorError(u'Unable to extract video')
4019         video_url = video.group('video_url')
4020         ext = video.group('ext')
4021
4022         video_thumbnail = self._search_regex(r'posters(.*?)\[\\x22(?P<thumb>.*?)\\x22',
4023             webpage, u'thumbnail', fatal=False)  # We pick the first poster
4024         if video_thumbnail: video_thumbnail = video_thumbnail.replace('\\', '')
4025
4026         # The only place where you can get a title, it's not complete,
4027         # but searching in other places doesn't work for all videos
4028         video_title = self._html_search_regex(r'<title>(?P<title>.*?)</title>',
4029             webpage, u'title', flags=re.DOTALL)
4030
4031         return [{'id': video_id,
4032                  'url': video_url,
4033                  'title': video_title,
4034                  'thumbnail': video_thumbnail,
4035                  'ext': ext
4036                  }]
4037
4038 class BandcampIE(InfoExtractor):
4039     _VALID_URL = r'http://.*?\.bandcamp\.com/track/(?P<title>.*)'
4040
4041     def _real_extract(self, url):
4042         mobj = re.match(self._VALID_URL, url)
4043         title = mobj.group('title')
4044         webpage = self._download_webpage(url, title)
4045         # We get the link to the free download page
4046         m_download = re.search(r'freeDownloadPage: "(.*?)"', webpage)
4047         if m_download is None:
4048             raise ExtractorError(u'No free songs found')
4049
4050         download_link = m_download.group(1)
4051         id = re.search(r'var TralbumData = {(.*?)id: (?P<id>\d*?)$', 
4052                        webpage, re.MULTILINE|re.DOTALL).group('id')
4053
4054         download_webpage = self._download_webpage(download_link, id,
4055                                                   'Downloading free downloads page')
4056         # We get the dictionary of the track from some javascrip code
4057         info = re.search(r'items: (.*?),$',
4058                          download_webpage, re.MULTILINE).group(1)
4059         info = json.loads(info)[0]
4060         # We pick mp3-320 for now, until format selection can be easily implemented.
4061         mp3_info = info[u'downloads'][u'mp3-320']
4062         # If we try to use this url it says the link has expired
4063         initial_url = mp3_info[u'url']
4064         re_url = r'(?P<server>http://(.*?)\.bandcamp\.com)/download/track\?enc=mp3-320&fsig=(?P<fsig>.*?)&id=(?P<id>.*?)&ts=(?P<ts>.*)$'
4065         m_url = re.match(re_url, initial_url)
4066         #We build the url we will use to get the final track url
4067         # This url is build in Bandcamp in the script download_bunde_*.js
4068         request_url = '%s/statdownload/track?enc=mp3-320&fsig=%s&id=%s&ts=%s&.rand=665028774616&.vrs=1' % (m_url.group('server'), m_url.group('fsig'), id, m_url.group('ts'))
4069         final_url_webpage = self._download_webpage(request_url, id, 'Requesting download url')
4070         # If we could correctly generate the .rand field the url would be
4071         #in the "download_url" key
4072         final_url = re.search(r'"retry_url":"(.*?)"', final_url_webpage).group(1)
4073
4074         track_info = {'id':id,
4075                       'title' : info[u'title'],
4076                       'ext' :   'mp3',
4077                       'url' :   final_url,
4078                       'thumbnail' : info[u'thumb_url'],
4079                       'uploader' :  info[u'artist']
4080                       }
4081
4082         return [track_info]
4083
4084 class RedTubeIE(InfoExtractor):
4085     """Information Extractor for redtube"""
4086     _VALID_URL = r'(?:http://)?(?:www\.)?redtube\.com/(?P<id>[0-9]+)'
4087
4088     def _real_extract(self,url):
4089         mobj = re.match(self._VALID_URL, url)
4090         if mobj is None:
4091             raise ExtractorError(u'Invalid URL: %s' % url)
4092
4093         video_id = mobj.group('id')
4094         video_extension = 'mp4'        
4095         webpage = self._download_webpage(url, video_id)
4096
4097         self.report_extraction(video_id)
4098
4099         video_url = self._html_search_regex(r'<source src="(.+?)" type="video/mp4">',
4100             webpage, u'video URL')
4101
4102         video_title = self._html_search_regex('<h1 class="videoTitle slidePanelMovable">(.+?)</h1>',
4103             webpage, u'title')
4104
4105         return [{
4106             'id':       video_id,
4107             'url':      video_url,
4108             'ext':      video_extension,
4109             'title':    video_title,
4110         }]
4111         
4112 class InaIE(InfoExtractor):
4113     """Information Extractor for Ina.fr"""
4114     _VALID_URL = r'(?:http://)?(?:www\.)?ina\.fr/video/(?P<id>I[0-9]+)/.*'
4115
4116     def _real_extract(self,url):
4117         mobj = re.match(self._VALID_URL, url)
4118
4119         video_id = mobj.group('id')
4120         mrss_url='http://player.ina.fr/notices/%s.mrss' % video_id
4121         video_extension = 'mp4'
4122         webpage = self._download_webpage(mrss_url, video_id)
4123
4124         self.report_extraction(video_id)
4125
4126         video_url = self._html_search_regex(r'<media:player url="(?P<mp4url>http://mp4.ina.fr/[^"]+\.mp4)',
4127             webpage, u'video URL')
4128
4129         video_title = self._search_regex(r'<title><!\[CDATA\[(?P<titre>.*?)]]></title>',
4130             webpage, u'title')
4131
4132         return [{
4133             'id':       video_id,
4134             'url':      video_url,
4135             'ext':      video_extension,
4136             'title':    video_title,
4137         }]
4138
4139 class HowcastIE(InfoExtractor):
4140     """Information Extractor for Howcast.com"""
4141     _VALID_URL = r'(?:https?://)?(?:www\.)?howcast\.com/videos/(?P<id>\d+)'
4142
4143     def _real_extract(self, url):
4144         mobj = re.match(self._VALID_URL, url)
4145
4146         video_id = mobj.group('id')
4147         webpage_url = 'http://www.howcast.com/videos/' + video_id
4148         webpage = self._download_webpage(webpage_url, video_id)
4149
4150         self.report_extraction(video_id)
4151
4152         video_url = self._search_regex(r'\'?file\'?: "(http://mobile-media\.howcast\.com/[0-9]+\.mp4)',
4153             webpage, u'video URL')
4154
4155         video_title = self._html_search_regex(r'<meta content=(?:"([^"]+)"|\'([^\']+)\') property=\'og:title\'',
4156             webpage, u'title')
4157
4158         video_description = self._html_search_regex(r'<meta content=(?:"([^"]+)"|\'([^\']+)\') name=\'description\'',
4159             webpage, u'description', fatal=False)
4160
4161         thumbnail = self._html_search_regex(r'<meta content=\'(.+?)\' property=\'og:image\'',
4162             webpage, u'thumbnail', fatal=False)
4163
4164         return [{
4165             'id':       video_id,
4166             'url':      video_url,
4167             'ext':      'mp4',
4168             'title':    video_title,
4169             'description': video_description,
4170             'thumbnail': thumbnail,
4171         }]
4172
4173 class VineIE(InfoExtractor):
4174     """Information Extractor for Vine.co"""
4175     _VALID_URL = r'(?:https?://)?(?:www\.)?vine\.co/v/(?P<id>\w+)'
4176
4177     def _real_extract(self, url):
4178         mobj = re.match(self._VALID_URL, url)
4179
4180         video_id = mobj.group('id')
4181         webpage_url = 'https://vine.co/v/' + video_id
4182         webpage = self._download_webpage(webpage_url, video_id)
4183
4184         self.report_extraction(video_id)
4185
4186         video_url = self._html_search_regex(r'<meta property="twitter:player:stream" content="(.+?)"',
4187             webpage, u'video URL')
4188
4189         video_title = self._html_search_regex(r'<meta property="og:title" content="(.+?)"',
4190             webpage, u'title')
4191
4192         thumbnail = self._html_search_regex(r'<meta property="og:image" content="(.+?)(\?.*?)?"',
4193             webpage, u'thumbnail', fatal=False)
4194
4195         uploader = self._html_search_regex(r'<div class="user">.*?<h2>(.+?)</h2>',
4196             webpage, u'uploader', fatal=False, flags=re.DOTALL)
4197
4198         return [{
4199             'id':        video_id,
4200             'url':       video_url,
4201             'ext':       'mp4',
4202             'title':     video_title,
4203             'thumbnail': thumbnail,
4204             'uploader':  uploader,
4205         }]
4206
4207 class FlickrIE(InfoExtractor):
4208     """Information Extractor for Flickr videos"""
4209     _VALID_URL = r'(?:https?://)?(?:www\.)?flickr\.com/photos/(?P<uploader_id>[\w\-_@]+)/(?P<id>\d+).*'
4210
4211     def _real_extract(self, url):
4212         mobj = re.match(self._VALID_URL, url)
4213
4214         video_id = mobj.group('id')
4215         video_uploader_id = mobj.group('uploader_id')
4216         webpage_url = 'http://www.flickr.com/photos/' + video_uploader_id + '/' + video_id
4217         webpage = self._download_webpage(webpage_url, video_id)
4218
4219         secret = self._search_regex(r"photo_secret: '(\w+)'", webpage, u'secret')
4220
4221         first_url = 'https://secure.flickr.com/apps/video/video_mtl_xml.gne?v=x&photo_id=' + video_id + '&secret=' + secret + '&bitrate=700&target=_self'
4222         first_xml = self._download_webpage(first_url, video_id, 'Downloading first data webpage')
4223
4224         node_id = self._html_search_regex(r'<Item id="id">(\d+-\d+)</Item>',
4225             first_xml, u'node_id')
4226
4227         second_url = 'https://secure.flickr.com/video_playlist.gne?node_id=' + node_id + '&tech=flash&mode=playlist&bitrate=700&secret=' + secret + '&rd=video.yahoo.com&noad=1'
4228         second_xml = self._download_webpage(second_url, video_id, 'Downloading second data webpage')
4229
4230         self.report_extraction(video_id)
4231
4232         mobj = re.search(r'<STREAM APP="(.+?)" FULLPATH="(.+?)"', second_xml)
4233         if mobj is None:
4234             raise ExtractorError(u'Unable to extract video url')
4235         video_url = mobj.group(1) + unescapeHTML(mobj.group(2))
4236
4237         video_title = self._html_search_regex(r'<meta property="og:title" content=(?:"([^"]+)"|\'([^\']+)\')',
4238             webpage, u'video title')
4239
4240         video_description = self._html_search_regex(r'<meta property="og:description" content=(?:"([^"]+)"|\'([^\']+)\')',
4241             webpage, u'description', fatal=False)
4242
4243         thumbnail = self._html_search_regex(r'<meta property="og:image" content=(?:"([^"]+)"|\'([^\']+)\')',
4244             webpage, u'thumbnail', fatal=False)
4245
4246         return [{
4247             'id':          video_id,
4248             'url':         video_url,
4249             'ext':         'mp4',
4250             'title':       video_title,
4251             'description': video_description,
4252             'thumbnail':   thumbnail,
4253             'uploader_id': video_uploader_id,
4254         }]
4255
4256 class TeamcocoIE(InfoExtractor):
4257     _VALID_URL = r'http://teamcoco\.com/video/(?P<url_title>.*)'
4258
4259     def _real_extract(self, url):
4260         mobj = re.match(self._VALID_URL, url)
4261         if mobj is None:
4262             raise ExtractorError(u'Invalid URL: %s' % url)
4263         url_title = mobj.group('url_title')
4264         webpage = self._download_webpage(url, url_title)
4265
4266         video_id = self._html_search_regex(r'<article class="video" data-id="(\d+?)"',
4267             webpage, u'video id')
4268
4269         self.report_extraction(video_id)
4270
4271         video_title = self._html_search_regex(r'<meta property="og:title" content="(.+?)"',
4272             webpage, u'title')
4273
4274         thumbnail = self._html_search_regex(r'<meta property="og:image" content="(.+?)"',
4275             webpage, u'thumbnail', fatal=False)
4276
4277         video_description = self._html_search_regex(r'<meta property="og:description" content="(.*?)"',
4278             webpage, u'description', fatal=False)
4279
4280         data_url = 'http://teamcoco.com/cvp/2.0/%s.xml' % video_id
4281         data = self._download_webpage(data_url, video_id, 'Downloading data webpage')
4282
4283         video_url = self._html_search_regex(r'<file type="high".*?>(.*?)</file>',
4284             data, u'video URL')
4285
4286         return [{
4287             'id':          video_id,
4288             'url':         video_url,
4289             'ext':         'mp4',
4290             'title':       video_title,
4291             'thumbnail':   thumbnail,
4292             'description': video_description,
4293         }]
4294
4295 class XHamsterIE(InfoExtractor):
4296     """Information Extractor for xHamster"""
4297     _VALID_URL = r'(?:http://)?(?:www.)?xhamster\.com/movies/(?P<id>[0-9]+)/.*\.html'
4298
4299     def _real_extract(self,url):
4300         mobj = re.match(self._VALID_URL, url)
4301
4302         video_id = mobj.group('id')
4303         mrss_url = 'http://xhamster.com/movies/%s/.html' % video_id
4304         webpage = self._download_webpage(mrss_url, video_id)
4305
4306         mobj = re.search(r'\'srv\': \'(?P<server>[^\']*)\',\s*\'file\': \'(?P<file>[^\']+)\',', webpage)
4307         if mobj is None:
4308             raise ExtractorError(u'Unable to extract media URL')
4309         if len(mobj.group('server')) == 0:
4310             video_url = compat_urllib_parse.unquote(mobj.group('file'))
4311         else:
4312             video_url = mobj.group('server')+'/key='+mobj.group('file')
4313         video_extension = video_url.split('.')[-1]
4314
4315         video_title = self._html_search_regex(r'<title>(?P<title>.+?) - xHamster\.com</title>',
4316             webpage, u'title')
4317
4318         # Can't see the description anywhere in the UI
4319         # video_description = self._html_search_regex(r'<span>Description: </span>(?P<description>[^<]+)',
4320         #     webpage, u'description', fatal=False)
4321         # if video_description: video_description = unescapeHTML(video_description)
4322
4323         mobj = re.search(r'hint=\'(?P<upload_date_Y>[0-9]{4})-(?P<upload_date_m>[0-9]{2})-(?P<upload_date_d>[0-9]{2}) [0-9]{2}:[0-9]{2}:[0-9]{2} [A-Z]{3,4}\'', webpage)
4324         if mobj:
4325             video_upload_date = mobj.group('upload_date_Y')+mobj.group('upload_date_m')+mobj.group('upload_date_d')
4326         else:
4327             video_upload_date = None
4328             self._downloader.report_warning(u'Unable to extract upload date')
4329
4330         video_uploader_id = self._html_search_regex(r'<a href=\'/user/[^>]+>(?P<uploader_id>[^<]+)',
4331             webpage, u'uploader id', default=u'anonymous')
4332
4333         video_thumbnail = self._search_regex(r'\'image\':\'(?P<thumbnail>[^\']+)\'',
4334             webpage, u'thumbnail', fatal=False)
4335
4336         return [{
4337             'id':       video_id,
4338             'url':      video_url,
4339             'ext':      video_extension,
4340             'title':    video_title,
4341             # 'description': video_description,
4342             'upload_date': video_upload_date,
4343             'uploader_id': video_uploader_id,
4344             'thumbnail': video_thumbnail
4345         }]
4346
4347 class HypemIE(InfoExtractor):
4348     """Information Extractor for hypem"""
4349     _VALID_URL = r'(?:http://)?(?:www\.)?hypem\.com/track/([^/]+)/([^/]+)'
4350
4351     def _real_extract(self, url):
4352         mobj = re.match(self._VALID_URL, url)
4353         if mobj is None:
4354             raise ExtractorError(u'Invalid URL: %s' % url)
4355         track_id = mobj.group(1)
4356
4357         data = { 'ax': 1, 'ts': time.time() }
4358         data_encoded = compat_urllib_parse.urlencode(data)
4359         complete_url = url + "?" + data_encoded
4360         request = compat_urllib_request.Request(complete_url)
4361         response, urlh = self._download_webpage_handle(request, track_id, u'Downloading webpage with the url')
4362         cookie = urlh.headers.get('Set-Cookie', '')
4363
4364         self.report_extraction(track_id)
4365
4366         html_tracks = self._html_search_regex(r'<script type="application/json" id="displayList-data">(.*?)</script>',
4367             response, u'tracks', flags=re.MULTILINE|re.DOTALL).strip()
4368         try:
4369             track_list = json.loads(html_tracks)
4370             track = track_list[u'tracks'][0]
4371         except ValueError:
4372             raise ExtractorError(u'Hypemachine contained invalid JSON.')
4373
4374         key = track[u"key"]
4375         track_id = track[u"id"]
4376         artist = track[u"artist"]
4377         title = track[u"song"]
4378
4379         serve_url = "http://hypem.com/serve/source/%s/%s" % (compat_str(track_id), compat_str(key))
4380         request = compat_urllib_request.Request(serve_url, "" , {'Content-Type': 'application/json'})
4381         request.add_header('cookie', cookie)
4382         song_data_json = self._download_webpage(request, track_id, u'Downloading metadata')
4383         try:
4384             song_data = json.loads(song_data_json)
4385         except ValueError:
4386             raise ExtractorError(u'Hypemachine contained invalid JSON.')
4387         final_url = song_data[u"url"]
4388
4389         return [{
4390             'id':       track_id,
4391             'url':      final_url,
4392             'ext':      "mp3",
4393             'title':    title,
4394             'artist':   artist,
4395         }]
4396
4397
4398 def gen_extractors():
4399     """ Return a list of an instance of every supported extractor.
4400     The order does matter; the first extractor matched is the one handling the URL.
4401     """
4402     return [
4403         YoutubePlaylistIE(),
4404         YoutubeChannelIE(),
4405         YoutubeUserIE(),
4406         YoutubeSearchIE(),
4407         YoutubeIE(),
4408         MetacafeIE(),
4409         DailymotionIE(),
4410         GoogleSearchIE(),
4411         PhotobucketIE(),
4412         YahooIE(),
4413         YahooSearchIE(),
4414         DepositFilesIE(),
4415         FacebookIE(),
4416         BlipTVIE(),
4417         BlipTVUserIE(),
4418         VimeoIE(),
4419         MyVideoIE(),
4420         ComedyCentralIE(),
4421         EscapistIE(),
4422         CollegeHumorIE(),
4423         XVideosIE(),
4424         SoundcloudSetIE(),
4425         SoundcloudIE(),
4426         InfoQIE(),
4427         MixcloudIE(),
4428         StanfordOpenClassroomIE(),
4429         MTVIE(),
4430         YoukuIE(),
4431         XNXXIE(),
4432         YouJizzIE(),
4433         PornotubeIE(),
4434         YouPornIE(),
4435         GooglePlusIE(),
4436         ArteTvIE(),
4437         NBAIE(),
4438         WorldStarHipHopIE(),
4439         JustinTVIE(),
4440         FunnyOrDieIE(),
4441         SteamIE(),
4442         UstreamIE(),
4443         RBMARadioIE(),
4444         EightTracksIE(),
4445         KeekIE(),
4446         TEDIE(),
4447         MySpassIE(),
4448         SpiegelIE(),
4449         LiveLeakIE(),
4450         ARDIE(),
4451         TumblrIE(),
4452         BandcampIE(),
4453         RedTubeIE(),
4454         InaIE(),
4455         HowcastIE(),
4456         VineIE(),
4457         FlickrIE(),
4458         TeamcocoIE(),
4459         XHamsterIE(),
4460         HypemIE(),
4461         GenericIE()
4462     ]
4463
4464 def get_info_extractor(ie_name):
4465     """Returns the info extractor class with the given ie_name"""
4466     return globals()[ie_name+'IE']