[MLB] Add new extractor
[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 thumbnail URL to find the folder that contains the videos
33         _image_url = r'http://mediadownloads.mlb.com/mlbam/(?P<_date>n?.+)/images/.*$'
34         bobj = re.match(_image_url, thumbnail)
35         datestr = bobj.group('_date')
36         base_url = 'http://mediadownloads.mlb.com/mlbam/' + datestr
37         filespage = self._download_webpage(base_url, video_id)
38         
39         # Try 1800K, 1500K, 1200K, 600K, then 300K videos
40         video = self._html_search_regex(r'<li><a href="(.*?)_'+video_id+'_1800K.mp4"', filespage, '1800K', fatal=False)
41         if video is not None:
42             video_url = base_url+'/'+video+'_'+video_id+'_1800K.mp4'
43         else:
44             video = self._html_search_regex(r'<li><a href="(.*?)_'+video_id+'_1500K.mp4"', filespage, '1500K', fatal=False)
45             if video is not None:
46                 video_url = base_url+'/'+video+'_'+video_id+'_1500K.mp4'
47             else:
48                 video = self._html_search_regex(r'<li><a href="(.*?)_'+video_id+'_600K.mp4"', filespage, '600K', fatal=False)
49                 if video is not None:
50                     video_url = base_url+'/'+video+'_'+video_id+'_600K.mp4'
51                 else:
52                     video = self._html_search_regex(r'<li><a href="(.*?)_'+video_id+'_300K.mp4"', filespage, 'MLB', fatal=False)
53                     if video is not None:
54                         video_url = base_url+'/'+video+'_'+video_id+'_300K.mp4'
55                     else:
56                         # nothing valuable to return
57                         return None
58                 
59         return {
60             'id': video_id,
61             'url': video_url,
62             'title': title,
63             'ext': 'mp4',
64             'format': 'mp4',
65             'description': description,
66             'thumbnail': thumbnail,
67         }