[Yam] Add an error detection and update test cases
[youtube-dl] / youtube_dl / extractor / yam.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 compat_urlparse
8 from ..utils import (
9     float_or_none,
10     month_by_abbreviation,
11     ExtractorError,
12 )
13
14
15 class YamIE(InfoExtractor):
16     _VALID_URL = r'http://mymedia.yam.com/m/(?P<id>\d+)'
17
18     _TESTS = [{
19         # An audio hosted on Yam
20         'url': 'http://mymedia.yam.com/m/2283921',
21         'md5': 'c011b8e262a52d5473d9c2e3c9963b9c',
22         'info_dict': {
23             'id': '2283921',
24             'ext': 'mp3',
25             'title': '發現 - 趙薇 京華煙雲主題曲',
26             'uploader_id': 'princekt',
27             'upload_date': '20080807',
28             'duration': 313.0,
29         }
30     }, {
31         # An external video hosted on YouTube
32         'url': 'http://mymedia.yam.com/m/3599430',
33         'md5': '03127cf10d8f35d120a9e8e52e3b17c6',
34         'info_dict': {
35             'id': 'CNpEoQlrIgA',
36             'ext': 'mp4',
37             'upload_date': '20150306',
38             'uploader': '新莊社大瑜伽社',
39             'description': 'md5:11e2e405311633ace874f2e6226c8b17',
40             'uploader_id': '2323agoy',
41             'title': '20090412陽明山二子坪-1',
42         }
43     }, {
44         'url': 'http://mymedia.yam.com/m/3598173',
45         'info_dict': {
46             'id': '3598173',
47             'ext': 'mp4',
48         },
49         'skip': 'cause Yam system error',
50     }, {
51         'url': 'http://mymedia.yam.com/m/3599437',
52         'info_dict': {
53             'id': '3599437',
54             'ext': 'mp4',
55         },
56         'skip': 'invalid YouTube URL',
57     }]
58
59     def _real_extract(self, url):
60         video_id = self._match_id(url)
61         page = self._download_webpage(url, video_id)
62
63         # Check for errors
64         system_msg = self._html_search_regex(
65             r'系統訊息(?:<br>|\n|\r)*([^<>]+)<br>', page, 'system message',
66             default=None)
67         if system_msg:
68             raise ExtractorError(system_msg, expected=True)
69
70         # Is it hosted externally on YouTube?
71         youtube_url = self._html_search_regex(
72             r'<embed src="(http://www.youtube.com/[^"]+)"',
73             page, 'YouTube url', default=None)
74         if youtube_url:
75             return self.url_result(youtube_url, 'Youtube')
76
77         api_page = self._download_webpage(
78             'http://mymedia.yam.com/api/a/?pID=' + video_id, video_id,
79             note='Downloading API page')
80         api_result_obj = compat_urlparse.parse_qs(api_page)
81
82         uploader_id = self._html_search_regex(
83             r'<!-- 發表作者 -->:[\n ]+<a href="/([a-z]+)"',
84             page, 'uploader id', fatal=False)
85         mobj = re.search(r'<!-- 發表於 -->(?P<mon>[A-Z][a-z]{2})  ' +
86                          r'(?P<day>\d{1,2}), (?P<year>\d{4})', page)
87         if mobj:
88             upload_date = '%s%02d%02d' % (
89                 mobj.group('year'),
90                 month_by_abbreviation(mobj.group('mon')),
91                 int(mobj.group('day')))
92         else:
93             upload_date = None
94         duration = float_or_none(api_result_obj['totaltime'][0], scale=1000)
95
96         return {
97             'id': video_id,
98             'url': api_result_obj['mp3file'][0],
99             'title': self._html_search_meta('description', page),
100             'duration': duration,
101             'uploader_id': uploader_id,
102             'upload_date': upload_date,
103         }