[vlive] Remove upload_date extraction & cleanup
[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     _VALID_URL = r'https?://(?:(www|m)\.)?vlive\.tv/video/(?P<id>[0-9]+)'
20     _TEST = {
21         'url': 'http://m.vlive.tv/video/1326',
22         'md5': 'cc7314812855ce56de70a06a27314983',
23         'info_dict': {
24             'id': '1326',
25             'ext': 'mp4',
26             'title': '[V] Girl\'s Day\'s Broadcast',
27             'creator': 'Girl\'s Day',
28         },
29     }
30     _SECRET = 'rFkwZet6pqk1vQt6SxxUkAHX7YL3lmqzUMrU4IDusTo4jEBdtOhNfT4BYYAdArwH'
31
32     def _real_extract(self, url):
33         video_id = self._match_id(url)
34
35         webpage = self._download_webpage(
36             'http://m.vlive.tv/video/%s' % video_id,
37             video_id, note='Download video page')
38
39         title = self._og_search_title(webpage)
40         thumbnail = self._og_search_thumbnail(webpage)
41         creator = self._html_search_regex(
42             r'<span class="name">([^<>]+)</span>', webpage, 'creator')
43         
44         url = 'http://global.apis.naver.com/globalV/globalV/vod/%s/playinfo?' % video_id
45         msgpad = '%.0f' % (time() * 1000)
46         md = b64encode(
47             hmac.new(self._SECRET.encode('ascii'),
48                      (url[:255] + msgpad).encode('ascii'), sha1).digest()
49         )
50         url += '&' + compat_urllib_parse.urlencode({'msgpad': msgpad, 'md': md})
51         playinfo = self._download_json(url, video_id, 'Downloading video json')
52
53         if playinfo.get('message', '') != 'success':
54             raise ExtractorError(playinfo['message'])
55
56         if not playinfo.get('result'):
57             raise ExtractorError('No videos found.')
58
59         formats = []
60         for vid in playinfo['result'].get('videos', {}).get('list', []):
61             formats.append({
62                 'url': vid['source'],
63                 'ext': 'mp4',
64                 'abr': vid.get('bitrate', {}).get('audio'),
65                 'vbr': vid.get('bitrate', {}).get('video'),
66                 'format_id': vid['encodingOption']['name'],
67                 'height': vid.get('height'),
68                 'width': vid.get('width'),
69             })
70         self._sort_formats(formats)
71
72         subtitles = {}
73         for caption in playinfo['result'].get('captions', {}).get('list', []):
74             subtitles[caption['language']] = [
75                 {'ext': determine_ext(caption['source'], default_ext='vtt'),
76                  'url': caption['source']}]
77
78         return {
79             'id': video_id,
80             'title': title,
81             'creator': creator,
82             'thumbnail': thumbnail,
83             'formats': formats,
84             'subtitles': subtitles,
85         }