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