[afreecatv] Add new extractor for afreecatv.com VODs
[youtube-dl] / youtube_dl / extractor / afreecatv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import (
6     compat_urllib_parse_urlparse,
7     compat_urlparse,
8 )
9 from ..utils import (
10     ExtractorError,
11     int_or_none,
12 )
13
14
15 class AfreecaTVIE(InfoExtractor):
16     IE_DESC = 'afreecatv.com'
17     _VALID_URL = r'''(?x)^
18         https?://(?:(live|afbbs|www)\.)?afreeca(?:tv)?\.com(?::\d+)?
19         (?:
20             /app/(?:index|read_ucc_bbs)\.cgi|
21             /player/[Pp]layer\.(?:swf|html))
22         \?.*?\bnTitleNo=(?P<id>\d+)'''
23     _TEST = {
24         'url': 'http://live.afreecatv.com:8079/app/index.cgi?szType=read_ucc_bbs&szBjId=dailyapril&nStationNo=16711924&nBbsNo=18605867&nTitleNo=36164052&szSkin=',
25         'md5': 'f72c89fe7ecc14c1b5ce506c4996046e',
26         'info_dict': {
27             'id': '36164052',
28             'ext': 'mp4',
29             'title': '데일리 에이프릴 요정들의 시상식!',
30             'thumbnail': 're:^https?://videoimg.afreecatv.com/.*$',
31             'uploader': 'dailyapril',
32             'uploader_id': 'dailyapril',
33         }
34     }
35
36     def _real_extract(self, url):
37         video_id = self._match_id(url)
38         parsed_url = compat_urllib_parse_urlparse(url)
39         info_url = compat_urlparse.urlunparse(parsed_url._replace(
40             netloc='afbbs.afreecatv.com:8080',
41             path='/api/video/get_video_info.php'))
42         video_xml = self._download_xml(info_url, video_id)
43
44         track = video_xml.find('track')
45         if track.find('flag').text != 'SUCCEED':
46             raise ExtractorError('Specified AfreecaTV video does not exist',
47                                  expected=True)
48         title = track.find('title').text
49         uploader = track.find('nickname').text
50         uploader_id = track.find('bj_id').text
51         duration = int_or_none(track.find('duration').text)
52         thumbnail = track.find('titleImage').text
53
54         entries = []
55         for video in track.findall('video'):
56             for video_file in video.findall('file'):
57                 entries.append({
58                     'id': video_file.get('key'),
59                     'title': title,
60                     'duration': int_or_none(video_file.get('duration')),
61                     'formats': [{'url': video_file.text}]
62                 })
63
64         info = {
65             'id': video_id,
66             'title': title,
67             'uploader': uploader,
68             'uploader_id': uploader_id,
69             'duration': duration,
70             'thumbnail': thumbnail,
71         }
72
73         if len(entries) > 1:
74             info['_type'] = 'multi_video'
75             info['entries'] = entries
76         elif len(entries) == 1:
77             info['formats'] = entries[0]['formats']
78         else:
79             raise ExtractorError(
80                 'No files found for the specified AfreecaTV video, either'
81                 ' the URL is incorrect or the video has been made private.',
82                 expected=True)
83
84         return info