]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/yam.py
Merge tag 'upstream/2015.02.28'
[youtubedl] / 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 )
12
13
14 class YamIE(InfoExtractor):
15 _VALID_URL = r'http://mymedia.yam.com/m/(?P<id>\d+)'
16
17 _TESTS = [{
18 # An audio hosted on Yam
19 'url': 'http://mymedia.yam.com/m/2283921',
20 'md5': 'c011b8e262a52d5473d9c2e3c9963b9c',
21 'info_dict': {
22 'id': '2283921',
23 'ext': 'mp3',
24 'title': '發現 - 趙薇 京華煙雲主題曲',
25 'uploader_id': 'princekt',
26 'upload_date': '20080807',
27 'duration': 313.0,
28 }
29 }, {
30 # An external video hosted on YouTube
31 'url': 'http://mymedia.yam.com/m/3598173',
32 'md5': '0238ceec479c654e8c2f1223755bf3e9',
33 'info_dict': {
34 'id': 'pJ2Deys283c',
35 'ext': 'mp4',
36 'upload_date': '20150202',
37 'uploader': '新莊社大瑜伽社',
38 'description': 'md5:f5cc72f0baf259a70fb731654b0d2eff',
39 'uploader_id': '2323agoy',
40 'title': '外婆的澎湖灣KTV-潘安邦',
41 }
42 }]
43
44 def _real_extract(self, url):
45 video_id = self._match_id(url)
46 page = self._download_webpage(url, video_id)
47
48 # Is it hosted externally on YouTube?
49 youtube_url = self._html_search_regex(
50 r'<embed src="(http://www.youtube.com/[^"]+)"',
51 page, 'YouTube url', default=None)
52 if youtube_url:
53 return self.url_result(youtube_url, 'Youtube')
54
55 api_page = self._download_webpage(
56 'http://mymedia.yam.com/api/a/?pID=' + video_id, video_id,
57 note='Downloading API page')
58 api_result_obj = compat_urlparse.parse_qs(api_page)
59
60 uploader_id = self._html_search_regex(
61 r'<!-- 發表作者 -->:[\n ]+<a href="/([a-z]+)"',
62 page, 'uploader id', fatal=False)
63 mobj = re.search(r'<!-- 發表於 -->(?P<mon>[A-Z][a-z]{2}) ' +
64 r'(?P<day>\d{1,2}), (?P<year>\d{4})', page)
65 if mobj:
66 upload_date = '%s%02d%02d' % (
67 mobj.group('year'),
68 month_by_abbreviation(mobj.group('mon')),
69 int(mobj.group('day')))
70 else:
71 upload_date = None
72 duration = float_or_none(api_result_obj['totaltime'][0], scale=1000)
73
74 return {
75 'id': video_id,
76 'url': api_result_obj['mp3file'][0],
77 'title': self._html_search_meta('description', page),
78 'duration': duration,
79 'uploader_id': uploader_id,
80 'upload_date': upload_date,
81 }