use url instead of single formats entry
[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     xpath_text,
13 )
14
15
16 class AfreecaTVIE(InfoExtractor):
17     IE_DESC = 'afreecatv.com'
18     _VALID_URL = r'''(?x)^
19         https?://(?:(live|afbbs|www)\.)?afreeca(?:tv)?\.com(?::\d+)?
20         (?:
21             /app/(?:index|read_ucc_bbs)\.cgi|
22             /player/[Pp]layer\.(?:swf|html))
23         \?.*?\bnTitleNo=(?P<id>\d+)'''
24     _TEST = {
25         'url': 'http://live.afreecatv.com:8079/app/index.cgi?szType=read_ucc_bbs&szBjId=dailyapril&nStationNo=16711924&nBbsNo=18605867&nTitleNo=36164052&szSkin=',
26         'md5': 'f72c89fe7ecc14c1b5ce506c4996046e',
27         'info_dict': {
28             'id': '36164052',
29             'ext': 'mp4',
30             'title': '데일리 에이프릴 요정들의 시상식!',
31             'thumbnail': 're:^https?://videoimg.afreecatv.com/.*$',
32             'uploader': 'dailyapril',
33             'uploader_id': 'dailyapril',
34         }
35     }
36
37     def _real_extract(self, url):
38         video_id = self._match_id(url)
39         parsed_url = compat_urllib_parse_urlparse(url)
40         info_url = compat_urlparse.urlunparse(parsed_url._replace(
41             netloc='afbbs.afreecatv.com:8080',
42             path='/api/video/get_video_info.php'))
43         video_xml = self._download_xml(info_url, video_id)
44
45         if xpath_text(video_xml, './track/flag', default='FAIL') != 'SUCCEED':
46             raise ExtractorError('Specified AfreecaTV video does not exist',
47                                  expected=True)
48         title = xpath_text(video_xml, './track/title', 'title')
49         uploader = xpath_text(video_xml, './track/nickname', 'uploader')
50         uploader_id = xpath_text(video_xml, './track/bj_id', 'uploader id')
51         duration = int_or_none(xpath_text(video_xml, './track/duration',
52                                           'duration'))
53         thumbnail = xpath_text(video_xml, './track/titleImage', 'thumbnail')
54
55         entries = []
56         for video_file in video_xml.findall('./track/video/file'):
57             entries.append({
58                 'id': video_file.get('key'),
59                 'title': title,
60                 'duration': int_or_none(video_file.get('duration')),
61                 '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['url'] = entries[0]['url']
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