Switched to use media detail XML to extract video URL
[youtube-dl] / youtube_dl / extractor / mlb.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6
7
8 class MlbIE(InfoExtractor):
9     _VALID_URL = r'http?://m\.mlb\.com/video/topic/[0-9]+/v(?P<id>n?\d+)/.*$'
10     _TEST = {
11         'url': 'http://m.mlb.com/video/topic/81536970/v34496663/mianym-stanton-practices-for-the-home-run-derby',
12         'md5': u'd9c022c10d21f849f49c05ae12a8a7e9',
13         'info_dict': {
14             'id': '34496663',
15             'ext': 'mp4',
16             'format': 'mp4',
17             'description': "7/11/14: Giancarlo Stanton practices for the Home Run Derby prior to the game against the Mets",
18             'title': "Stanton prepares for Derby",
19         },
20     }
21
22     def _real_extract(self, url):
23         mobj = re.match(self._VALID_URL, url)
24         video_id = mobj.group('id')
25
26         webpage = self._download_webpage(url, video_id)
27
28         title = self._og_search_title(webpage, default=video_id)
29         description = self._html_search_regex(r'<meta name="description" (?:content|value)="(.*?)"/>', webpage, 'description', fatal=False)
30         thumbnail = self._html_search_regex(r'<meta itemprop="image" (?:content|value)="(.*?)" />', webpage, 'image', fatal=False)
31
32         # use the video_id to find the Media detail XML
33         id_len = len(video_id)
34         _mediadetail_url = 'http://m.mlb.com/gen/multimedia/detail/'+video_id[id_len-3]+'/'+video_id[id_len-2]+'/'+video_id[id_len-1]+'/'+video_id+'.xml'
35         
36         mediadetails = self._download_xml(_mediadetail_url, video_id, "Downloading media detail...")
37         has1500K = 0
38         has1200K = 0
39         has600K = 0
40         # loop through the list of url's and only get the highest quality MP4 content
41         for element in mediadetails.findall('url'):
42             scenario = element.attrib['playback_scenario']
43             if scenario.startswith(u'FLASH'):
44                 if scenario.startswith(u'FLASH_1800K'):
45                     video_url = element.text
46                     # 1800K is the current highest quality video on MLB.com
47                     break
48                 else:
49                     if scenario.startswith(u'FLASH_1500K'):
50                         video_url = element.text
51                         has1500K = 1
52                     else:
53                         if (scenario.startswith(u'FLASH_1200K') and not has1500K):
54                             video_url = element.text
55                             has1200K = 1
56                         else:
57                             if (scenario.startswith(u'FLASH_600K') and not has1200K):
58                                 video_url = element.text
59                                 has600K = 1
60                             else:
61                                 if (scenario.startswith(u'FLASH_300K') and not has600K):
62                                     video_url = element.text
63
64         return {
65             'id': video_id,
66             'url': video_url,
67             'extractor': 'mlb',
68             'webpage_url': url,
69             'title': title,
70             'ext': 'mp4',
71             'format': 'mp4',
72             'description': description,
73             'thumbnail': thumbnail,
74         }