use /track/video/file to determine if video exists
[youtube-dl] / youtube_dl / extractor / afreecatv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import (
8     compat_urllib_parse_urlparse,
9     compat_urlparse,
10 )
11 from ..utils import (
12     ExtractorError,
13     int_or_none,
14     xpath_element,
15     xpath_text,
16 )
17
18
19 class AfreecaTVIE(InfoExtractor):
20     IE_DESC = 'afreecatv.com'
21     _VALID_URL = r'''(?x)^
22         https?://(?:(live|afbbs|www)\.)?afreeca(?:tv)?\.com(?::\d+)?
23         (?:
24             /app/(?:index|read_ucc_bbs)\.cgi|
25             /player/[Pp]layer\.(?:swf|html))
26         \?.*?\bnTitleNo=(?P<id>\d+)'''
27     _TESTS = [{
28         'url': 'http://live.afreecatv.com:8079/app/index.cgi?szType=read_ucc_bbs&szBjId=dailyapril&nStationNo=16711924&nBbsNo=18605867&nTitleNo=36164052&szSkin=',
29         'md5': 'f72c89fe7ecc14c1b5ce506c4996046e',
30         'info_dict': {
31             'id': '36164052',
32             'ext': 'mp4',
33             'title': '데일리 에이프릴 요정들의 시상식!',
34             'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
35             'uploader': 'dailyapril',
36             'uploader_id': 'dailyapril',
37             'upload_date': '20160503',
38         }
39     }, {
40         'url': 'http://afbbs.afreecatv.com:8080/app/read_ucc_bbs.cgi?nStationNo=16711924&nTitleNo=36153164&szBjId=dailyapril&nBbsNo=18605867',
41         'info_dict': {
42             'id': '36153164',
43             'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
44             'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
45             'uploader': 'dailyapril',
46             'uploader_id': 'dailyapril',
47         },
48         'playlist_count': 2,
49         'playlist': [{
50             'md5': 'd8b7c174568da61d774ef0203159bf97',
51             'info_dict': {
52                 'id': '36153164_1',
53                 'ext': 'mp4',
54                 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
55                 'upload_date': '20160502',
56             },
57         }, {
58             'md5': '58f2ce7f6044e34439ab2d50612ab02b',
59             'info_dict': {
60                 'id': '36153164_2',
61                 'ext': 'mp4',
62                 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
63                 'upload_date': '20160502',
64             },
65         }],
66     }, {
67         'url': 'http://www.afreecatv.com/player/Player.swf?szType=szBjId=djleegoon&nStationNo=11273158&nBbsNo=13161095&nTitleNo=36327652',
68         'only_matching': True,
69     }]
70
71     @staticmethod
72     def parse_video_key(key):
73         video_key = {}
74         m = re.match(r'^(?P<upload_date>\d{8})_\w+_(?P<part>\d+)$', key)
75         if m:
76             video_key['upload_date'] = m.group('upload_date')
77             video_key['part'] = m.group('part')
78         return video_key
79
80     def _real_extract(self, url):
81         video_id = self._match_id(url)
82         parsed_url = compat_urllib_parse_urlparse(url)
83         info_url = compat_urlparse.urlunparse(parsed_url._replace(
84             netloc='afbbs.afreecatv.com:8080',
85             path='/api/video/get_video_info.php'))
86         video_xml = self._download_xml(info_url, video_id)
87
88         if xpath_element(video_xml, './track/video/file') is None:
89             raise ExtractorError('Specified AfreecaTV video does not exist',
90                                  expected=True)
91
92         title = xpath_text(video_xml, './track/title', 'title')
93         uploader = xpath_text(video_xml, './track/nickname', 'uploader')
94         uploader_id = xpath_text(video_xml, './track/bj_id', 'uploader id')
95         duration = int_or_none(xpath_text(video_xml, './track/duration',
96                                           'duration'))
97         thumbnail = xpath_text(video_xml, './track/titleImage', 'thumbnail')
98
99         entries = []
100         for i, video_file in enumerate(video_xml.findall('./track/video/file')):
101             video_key = self.parse_video_key(video_file.get('key', ''))
102             if not video_key:
103                 continue
104             entries.append({
105                 'id': '%s_%s' % (video_id, video_key.get('part', i + 1)),
106                 'title': title,
107                 'upload_date': video_key.get('upload_date'),
108                 'duration': int_or_none(video_file.get('duration')),
109                 'url': video_file.text,
110             })
111
112         info = {
113             'id': video_id,
114             'title': title,
115             'uploader': uploader,
116             'uploader_id': uploader_id,
117             'duration': duration,
118             'thumbnail': thumbnail,
119         }
120
121         if len(entries) > 1:
122             info['_type'] = 'multi_video'
123             info['entries'] = entries
124         elif len(entries) == 1:
125             info['url'] = entries[0]['url']
126             info['upload_date'] = entries[0].get('upload_date')
127         else:
128             raise ExtractorError(
129                 'No files found for the specified AfreecaTV video, either'
130                 ' the URL is incorrect or the video has been made private.',
131                 expected=True)
132
133         return info