fix multi_video part naming, add upload_date field
[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_text,
15 )
16
17
18 class 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+)'''
26     _TEST = {
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': '데일리 에이프릴 요정들의 시상식!',
33             'thumbnail': 're:^https?://videoimg.afreecatv.com/.*$',
34             'uploader': 'dailyapril',
35             'uploader_id': 'dailyapril',
36         }
37     }
38
39     @staticmethod
40     def parse_video_key(key):
41         video_key = {'upload_date': None, 'part': '0'}
42         m = re.match(r'^(?P<upload_date>\d{8})_\w+_(?P<part>\d+)$', key)
43         if m:
44             video_key['upload_date'] = m.group('upload_date')
45             video_key['part'] = m.group('part')
46         return video_key
47
48     def _real_extract(self, url):
49         video_id = self._match_id(url)
50         parsed_url = compat_urllib_parse_urlparse(url)
51         info_url = compat_urlparse.urlunparse(parsed_url._replace(
52             netloc='afbbs.afreecatv.com:8080',
53             path='/api/video/get_video_info.php'))
54         video_xml = self._download_xml(info_url, video_id)
55
56         if xpath_text(video_xml, './track/flag', default='FAIL') != 'SUCCEED':
57             raise ExtractorError('Specified AfreecaTV video does not exist',
58                                  expected=True)
59         title = xpath_text(video_xml, './track/title', 'title')
60         uploader = xpath_text(video_xml, './track/nickname', 'uploader')
61         uploader_id = xpath_text(video_xml, './track/bj_id', 'uploader id')
62         duration = int_or_none(xpath_text(video_xml, './track/duration',
63                                           'duration'))
64         thumbnail = xpath_text(video_xml, './track/titleImage', 'thumbnail')
65
66         entries = []
67         for video_file in video_xml.findall('./track/video/file'):
68             video_key = self.parse_video_key(video_file.get('key'))
69             entries.append({
70                 'id': '%s_%s' % (video_id, video_key['part']),
71                 'title': title,
72                 'upload_date': video_key['upload_date'],
73                 'duration': int_or_none(video_file.get('duration')),
74                 'url': video_file.text,
75             })
76
77         info = {
78             'id': video_id,
79             'title': title,
80             'uploader': uploader,
81             'uploader_id': uploader_id,
82             'duration': duration,
83             'thumbnail': thumbnail,
84         }
85
86         if len(entries) > 1:
87             info['_type'] = 'multi_video'
88             info['entries'] = entries
89         elif len(entries) == 1:
90             info['url'] = entries[0]['url']
91             info['upload_date'] = entries[0]['upload_date']
92         else:
93             raise ExtractorError(
94                 'No files found for the specified AfreecaTV video, either'
95                 ' the URL is incorrect or the video has been made private.',
96                 expected=True)
97
98         return info