[vk] Add login feature (Closes #2206)
[youtube-dl] / youtube_dl / extractor / vk.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import json
6
7 from .common import InfoExtractor
8 from ..utils import (
9     ExtractorError,
10     compat_urllib_request,
11     compat_urllib_parse,
12     compat_str,
13     unescapeHTML,
14 )
15
16
17 class VKIE(InfoExtractor):
18     IE_NAME = 'vk.com'
19     _VALID_URL = r'https?://vk\.com/(?:videos.*?\?.*?z=)?video(?P<id>.*?)(?:\?|%2F|$)'
20
21     _TESTS = [
22         {
23             'url': 'http://vk.com/videos-77521?z=video-77521_162222515%2Fclub77521',
24             'md5': '0deae91935c54e00003c2a00646315f0',
25             'info_dict': {
26                 'id': '162222515',
27                 'ext': 'flv',
28                 'title': 'ProtivoGunz - Хуёвая песня',
29                 'uploader': 'Noize MC',
30                 'duration': 195,
31             },
32         },
33         {
34             'url': 'http://vk.com/video4643923_163339118',
35             'md5': 'f79bccb5cd182b1f43502ca5685b2b36',
36             'info_dict': {
37                 'id': '163339118',
38                 'ext': 'mp4',
39                 'uploader': 'Elvira Dzhonik',
40                 'title': 'Dream Theater - Hollow Years Live at Budokan 720*',
41                 'duration': 558,
42             }
43         },
44         {
45             'url': 'http://vk.com/video-8871596_164049491',
46             'md5': 'a590bcaf3d543576c9bd162812387666',
47             'note': 'Only available for registered users',
48             'info_dict': {
49                 'id': '164049491',
50                 'ext': 'mp4',
51                 'uploader': 'Триллеры',
52                 'title': '► Бойцовский клуб / Fight Club 1999 [HD 720]\u00a0',
53                 'duration': 8352,
54             },
55             'skip': 'Requires vk account credentials',
56         }
57     ]
58
59     def _login(self):
60         (username, password) = self._get_login_info()
61         if username is None:
62             return
63
64         login_form = {
65             'act': 'login',
66             'role': 'al_frame',
67             'expire': '1',
68             'email': username,
69             'pass': password,
70         }
71
72         request = compat_urllib_request.Request('https://login.vk.com/?act=login',
73             compat_urllib_parse.urlencode(login_form).encode('utf-8'))
74         login_page = self._download_webpage(request, None, note='Logging in as %s' % username)
75
76         if re.search(r'onLoginFailed', login_page):
77             raise ExtractorError('Unable to login, incorrect username and/or password', expected=True)
78
79     def _real_initialize(self):
80         self._login()
81
82     def _real_extract(self, url):
83         mobj = re.match(self._VALID_URL, url)
84         video_id = mobj.group('id')
85
86         info_url = 'http://vk.com/al_video.php?act=show&al=1&video=%s' % video_id
87         info_page = self._download_webpage(info_url, video_id)
88
89         if re.search(r'<!>Please log in or <', info_page):
90             raise ExtractorError('This video is only available for registered users, '
91                 'use --username and --password options to provide account credentials.', expected=True)
92
93         m_yt = re.search(r'src="(http://www.youtube.com/.*?)"', info_page)
94         if m_yt is not None:
95             self.to_screen(u'Youtube video detected')
96             return self.url_result(m_yt.group(1), 'Youtube')
97         data_json = self._search_regex(r'var vars = ({.*?});', info_page, 'vars')
98         data = json.loads(data_json)
99
100         formats = [{
101             'format_id': k,
102             'url': v,
103             'width': int(k[len('url'):]),
104         } for k, v in data.items()
105             if k.startswith('url')]
106         self._sort_formats(formats)
107
108         return {
109             'id': compat_str(data['vid']),
110             'formats': formats,
111             'title': unescapeHTML(data['md_title']),
112             'thumbnail': data.get('jpg'),
113             'uploader': data.get('md_author'),
114             'duration': data.get('duration')
115         }