[youtube] Fix extraction.
[youtube-dl] / youtube_dl / extractor / mgoon.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     ExtractorError,
9     qualities,
10     unified_strdate,
11 )
12
13
14 class MgoonIE(InfoExtractor):
15     _VALID_URL = r'''(?x)https?://(?:www\.)?
16     (?:(:?m\.)?mgoon\.com/(?:ch/(?:.+)/v|play/view)|
17         video\.mgoon\.com)/(?P<id>[0-9]+)'''
18     _API_URL = 'http://mpos.mgoon.com/player/video?id={0:}'
19     _TESTS = [
20         {
21             'url': 'http://m.mgoon.com/ch/hi6618/v/5582148',
22             'md5': 'dd46bb66ab35cf6d51cc812fd82da79d',
23             'info_dict': {
24                 'id': '5582148',
25                 'uploader_id': 'hi6618',
26                 'duration': 240.419,
27                 'upload_date': '20131220',
28                 'ext': 'mp4',
29                 'title': 'md5:543aa4c27a4931d371c3f433e8cebebc',
30                 'thumbnail': r're:^https?://.*\.jpg$',
31             }
32         },
33         {
34             'url': 'http://www.mgoon.com/play/view/5582148',
35             'only_matching': True,
36         },
37         {
38             'url': 'http://video.mgoon.com/5582148',
39             'only_matching': True,
40         },
41     ]
42
43     def _real_extract(self, url):
44         mobj = re.match(self._VALID_URL, url)
45         video_id = mobj.group('id')
46
47         data = self._download_json(self._API_URL.format(video_id), video_id)
48
49         if data.get('errorInfo', {}).get('code') != 'NONE':
50             raise ExtractorError('%s encountered an error: %s' % (
51                 self.IE_NAME, data['errorInfo']['message']), expected=True)
52
53         v_info = data['videoInfo']
54         title = v_info.get('v_title')
55         thumbnail = v_info.get('v_thumbnail')
56         duration = v_info.get('v_duration')
57         upload_date = unified_strdate(v_info.get('v_reg_date'))
58         uploader_id = data.get('userInfo', {}).get('u_alias')
59         if duration:
60             duration /= 1000.0
61
62         age_limit = None
63         if data.get('accessInfo', {}).get('code') == 'VIDEO_STATUS_ADULT':
64             age_limit = 18
65
66         formats = []
67         get_quality = qualities(['360p', '480p', '720p', '1080p'])
68         for fmt in data['videoFiles']:
69             formats.append({
70                 'format_id': fmt['label'],
71                 'quality': get_quality(fmt['label']),
72                 'url': fmt['url'],
73                 'ext': fmt['format'],
74
75             })
76         self._sort_formats(formats)
77
78         return {
79             'id': video_id,
80             'title': title,
81             'formats': formats,
82             'thumbnail': thumbnail,
83             'duration': duration,
84             'upload_date': upload_date,
85             'uploader_id': uploader_id,
86             'age_limit': age_limit,
87         }