[vlive] Add info about www. to m. redirects for mobile
[youtube-dl] / youtube_dl / extractor / vlive.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import hmac
5 from hashlib import sha1
6 from base64 import b64encode
7 from time import time
8
9 from .common import InfoExtractor
10 from ..utils import (
11     ExtractorError,
12     determine_ext
13 )
14 from ..compat import compat_urllib_parse
15
16
17 class VLiveIE(InfoExtractor):
18     IE_NAME = 'vlive'
19     # www.vlive.tv/video/ links redirect to m.vlive.tv/video/ for mobile devices
20     _VALID_URL = r'https?://(?:(www|m)\.)?vlive\.tv/video/(?P<id>[0-9]+)'
21     _TEST = {
22         'url': 'http://m.vlive.tv/video/1326',
23         'md5': 'cc7314812855ce56de70a06a27314983',
24         'info_dict': {
25             'id': '1326',
26             'ext': 'mp4',
27             'title': '[V] Girl\'s Day\'s Broadcast',
28             'creator': 'Girl\'s Day',
29         },
30     }
31     _SECRET = 'rFkwZet6pqk1vQt6SxxUkAHX7YL3lmqzUMrU4IDusTo4jEBdtOhNfT4BYYAdArwH'
32
33     def _real_extract(self, url):
34         video_id = self._match_id(url)
35
36         webpage = self._download_webpage(
37             'http://m.vlive.tv/video/%s' % video_id,
38             video_id, note='Download video page')
39
40         title = self._og_search_title(webpage)
41         thumbnail = self._og_search_thumbnail(webpage)
42         creator = self._html_search_regex(
43             r'<span[^>]+class="name">([^<>]+)</span>', webpage, 'creator')
44         
45         url = 'http://global.apis.naver.com/globalV/globalV/vod/%s/playinfo?' % video_id
46         msgpad = '%.0f' % (time() * 1000)
47         md = b64encode(
48             hmac.new(self._SECRET.encode('ascii'),
49                      (url[:255] + msgpad).encode('ascii'), sha1).digest()
50         )
51         url += '&' + compat_urllib_parse.urlencode({'msgpad': msgpad, 'md': md})
52         playinfo = self._download_json(url, video_id, 'Downloading video json')
53
54         if playinfo.get('message', '') != 'success':
55             raise ExtractorError(playinfo.get('message', 'JSON request unsuccessful'))
56
57         if not playinfo.get('result'):
58             raise ExtractorError('No videos found.')
59
60         formats = []
61         for vid in playinfo['result'].get('videos', {}).get('list', []):
62             formats.append({
63                 'url': vid['source'],
64                 'ext': 'mp4',
65                 'abr': vid.get('bitrate', {}).get('audio'),
66                 'vbr': vid.get('bitrate', {}).get('video'),
67                 'format_id': vid['encodingOption']['name'],
68                 'height': vid.get('height'),
69                 'width': vid.get('width'),
70             })
71         self._sort_formats(formats)
72
73         subtitles = {}
74         for caption in playinfo['result'].get('captions', {}).get('list', []):
75             subtitles[caption['language']] = [
76                 {'ext': determine_ext(caption['source'], default_ext='vtt'),
77                  'url': caption['source']}]
78
79         return {
80             'id': video_id,
81             'title': title,
82             'creator': creator,
83             'thumbnail': thumbnail,
84             'formats': formats,
85             'subtitles': subtitles,
86         }