aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/extractor/yam.py
blob: b294767c5b6bc5a7ec918e9108cc4cff1a5fac40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# coding: utf-8
from __future__ import unicode_literals

import re

from .common import InfoExtractor
from ..compat import compat_urlparse
from ..utils import (
    float_or_none,
    month_by_abbreviation,
)


class YamIE(InfoExtractor):
    _VALID_URL = r'http://mymedia.yam.com/m/(?P<id>\d+)'

    _TESTS = [{
        # An audio hosted on Yam
        'url': 'http://mymedia.yam.com/m/2283921',
        'md5': 'c011b8e262a52d5473d9c2e3c9963b9c',
        'info_dict': {
            'id': '2283921',
            'ext': 'mp3',
            'title': '發現 - 趙薇 京華煙雲主題曲',
            'uploader_id': 'princekt',
            'upload_date': '20080807',
            'duration': 313.0,
        }
    }, {
        # An external video hosted on YouTube
        'url': 'http://mymedia.yam.com/m/3598173',
        'md5': '0238ceec479c654e8c2f1223755bf3e9',
        'info_dict': {
            'id': 'pJ2Deys283c',
            'ext': 'mp4',
            'upload_date': '20150202',
            'uploader': '新莊社大瑜伽社',
            'description': 'md5:f5cc72f0baf259a70fb731654b0d2eff',
            'uploader_id': '2323agoy',
            'title': '外婆的澎湖灣KTV-潘安邦',
        }
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)
        page = self._download_webpage(url, video_id)

        # Is it hosted externally on YouTube?
        youtube_url = self._html_search_regex(
            r'<embed src="(http://www.youtube.com/[^"]+)"',
            page, 'YouTube url', default=None)
        if youtube_url:
            return self.url_result(youtube_url, 'Youtube')

        api_page = self._download_webpage(
            'http://mymedia.yam.com/api/a/?pID=' + video_id, video_id,
            note='Downloading API page')
        api_result_obj = compat_urlparse.parse_qs(api_page)

        uploader_id = self._html_search_regex(
            r'<!-- 發表作者 -->:[\n ]+<a href="/([a-z]+)"',
            page, 'uploader id', fatal=False)
        mobj = re.search(r'<!-- 發表於 -->(?P<mon>[A-Z][a-z]{2})  ' +
                         r'(?P<day>\d{1,2}), (?P<year>\d{4})', page)
        if mobj:
            upload_date = '%s%02d%02d' % (
                mobj.group('year'),
                month_by_abbreviation(mobj.group('mon')),
                int(mobj.group('day')))
        else:
            upload_date = None
        duration = float_or_none(api_result_obj['totaltime'][0], scale=1000)

        return {
            'id': video_id,
            'url': api_result_obj['mp3file'][0],
            'title': self._html_search_meta('description', page),
            'duration': duration,
            'uploader_id': uploader_id,
            'upload_date': upload_date,
        }