[vlive] Updated to new V App/VLive api.
[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     int_or_none
14 )
15 from ..compat import compat_urllib_parse
16
17
18 class VLiveIE(InfoExtractor):
19     IE_NAME = 'vlive'
20     # vlive.tv/video/ links redirect to www.vlive.tv/video/ 
21     _VALID_URL = r'https?://(?:(www|m)\.)?vlive\.tv/video/(?P<id>[0-9]+)'
22     _TEST = {
23         'url': 'http://www.vlive.tv/video/1326',
24         'md5': 'cc7314812855ce56de70a06a27314983',
25         'info_dict': {
26             'id': '1326',
27             'ext': 'mp4',
28             'title': '[V] Girl\'s Day\'s Broadcast',
29             'creator': 'Girl\'s Day',
30         },
31     }
32
33     def _real_extract(self, url):
34         video_id = self._match_id(url)
35
36         webpage = self._download_webpage(
37             'http://www.vlive.tv/video/%s' % video_id,
38             video_id, note='Download video page')
39
40         long_video_id = self._search_regex(
41             r'vlive\.tv\.video\.ajax\.request\.handler\.init\("[0-9]+",\s?"[^"]*",\s?"([^"]+)",\s?"[^"]+",\s?"[^"]*",\s?"[^"]*"\)', webpage, 'long_video_id')
42
43         key = self._search_regex(
44             r'vlive\.tv\.video\.ajax\.request\.handler\.init\("[0-9]+",\s?"[^"]*",\s?"[^"]+",\s?"([^"]+)",\s?"[^"]*",\s?"[^"]*"\)', webpage, 'key')
45
46         title = self._og_search_title(webpage)
47         thumbnail = self._og_search_thumbnail(webpage)
48         creator = self._html_search_regex(
49             r'<div class="info_area">\s*<strong[^>]+class="name">([^<>]+)</strong>', webpage, 'creator',fatal=False)
50
51         # doct = document type (xml or json), cpt = caption type (vtt or ttml)
52         url = "http://global.apis.naver.com/rmcnmv/rmcnmv/vod_play_videoInfo.json?videoId=%s&key=%s&ptc=http&doct=json&cpt=vtt" % (long_video_id, key)
53         
54         playinfo = self._download_json(url, video_id, 'Downloading video json')
55
56         formats = []
57         for vid in playinfo.get('videos', {}).get('list', []):
58             formats.append({
59                 'url': vid['source'],
60                 'ext': 'mp4',
61                 'abr': vid.get('bitrate', {}).get('audio'),
62                 'vbr': vid.get('bitrate', {}).get('video'),
63                 'format_id': vid.get('encodingOption', {}).get('name'),
64                 'height': int_or_none(vid.get('encodingOption', {}).get('height')),
65                 'width': int_or_none(vid.get('encodingOption', {}).get('width')),
66             })
67         self._sort_formats(formats)
68
69         subtitles = {}
70         for caption in playinfo.get('captions', {}).get('list', []):
71             subtitles[caption['language']] = [
72                 {'ext': determine_ext(caption['source'], default_ext='vtt'),
73                  'url': caption['source']}]
74
75         return {
76             'id': video_id,
77             'title': title,
78             'creator': creator,
79             'thumbnail': thumbnail,
80             'formats': formats,
81             'subtitles': subtitles,
82         }