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