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