[bleacherreport] Add new Extractor
[youtube-dl] / youtube_dl / extractor / bleacherreport.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from .amp import AMPIE
6 from ..utils import (
7     ExtractorError,
8     int_or_none,
9     parse_iso8601,
10 )
11
12
13 class BleacherReportIE(InfoExtractor):
14     _VALID_URL = r'https?://(?:www\.)?bleacherreport\.com/articles/(?P<id>\d+)'
15     _TESTS = [{
16         'url': 'http://bleacherreport.com/articles/2496438-fsu-stat-projections-is-jalen-ramsey-best-defensive-player-in-college-football',
17         'md5': 'a3ffc3dc73afdbc2010f02d98f990f20',
18         'info_dict': {
19             'id': '2496438',
20             'ext': 'mp4',
21             'title': 'FSU Stat Projections: Is Jalen Ramsey Best Defensive Player in College Football?',
22             'uploader_id': 3992341,
23             'description': 'CFB, ACC, Florida State',
24             'timestamp': 1434380212,
25             'upload_date': '20150615',
26             'uploader': 'Team Stream Now ',
27         },
28         'add_ie': ['Ooyala'],
29     },{
30         'url': 'http://bleacherreport.com/articles/2586817-aussie-golfers-get-fright-of-their-lives-after-being-chased-by-angry-kangaroo',
31         'md5': 'af5f90dc9c7ba1c19d0a3eac806bbf50',
32         'info_dict': {
33             'id': '2586817',
34             'ext': 'mp4',
35             'title': 'Aussie Golfers Get Fright of Their Lives After Being Chased by Angry Kangaroo',
36             'timestamp': 1446839961,
37             'uploader': 'Sean Fay',
38             'description': 'md5:e95afafa43619816552723878b3b0a84',
39             'uploader_id': 6466954,
40             'upload_date': '20151011',
41         },
42         'add_ie': ['Youtube'],
43     },{
44         'url': 'http://bleacherreport.com/articles/2496438-fsu-stat-projections-is-jalen-ramsey-best-defensive-player-in-college-football',
45         'md5': 'a3ffc3dc73afdbc2010f02d98f990f20',
46         'info_dict': {
47             'id': '2496438',
48             'ext': 'mp4',
49             'title': 'FSU Stat Projections: Is Jalen Ramsey Best Defensive Player in College Football?',
50             'upload_date': '20150615',
51             'uploader': 'Team Stream Now ',
52             'timestamp': 1434380212,
53             'description': 'CFB, ACC, Florida State',
54             'uploader_id': 3992341,
55         },
56         'add_ie': ['Vine'],
57     }]
58
59     def _real_extract(self, url):
60         article_id = self._match_id(url)
61
62         article_data = self._download_json('http://api.bleacherreport.com/api/v1/articles/%s' % article_id, article_id)['article']
63
64         thumbnails = []
65         primary_photo = article_data.get('primaryPhoto')
66         if primary_photo:
67             thumbnails = [{
68                 'url': primary_photo['url'],
69                 'width': primary_photo.get('width'),
70                 'height': primary_photo.get('height'),
71             }]
72
73         info = {
74             '_type': 'url_transparent',
75             'id': article_id,
76             'title': article_data['title'],
77             'uploader': article_data.get('author', {}).get('name'),
78             'uploader_id': article_data.get('authorId'),
79             'timestamp': parse_iso8601(article_data.get('createdAt')),
80             'thumbnails': thumbnails,
81             'comment_count': int_or_none(article_data.get('commentsCount')),
82             'view_count': int_or_none(article_data.get('hitCount')),
83         }
84
85         video = article_data.get('video')
86         if video:
87             video_type = video['type']
88             if video_type == 'cms.bleacherreport.com':
89                 info['url'] = 'http://bleacherreport.com/video_embed?id=%s' % video['id']
90             elif video_type == 'ooyala.com':
91                 info['url'] = 'ooyala:%s' % video['id']
92             elif video_type == 'youtube.com':
93                 info['url'] = video['id']
94             elif video_type == 'vine.co':
95                 info['url'] = 'https://vine.co/v/%s' % video['id']
96             else:
97                 info['url'] = video_type + video['id']
98             return info
99         else:
100             raise ExtractorError('no video in the article', expected=True)
101
102
103 class BleacherReportCMSIE(AMPIE):
104     _VALID_URL = r'https?://(?:www\.)?bleacherreport\.com/video_embed\?id=(?P<id>[0-9a-f-]{36})'
105     _TESTS = [{
106         'url': 'http://bleacherreport.com/video_embed?id=8fd44c2f-3dc5-4821-9118-2c825a98c0e1',
107         'md5': 'f0ca220af012d4df857b54f792c586bb',
108         'info_dict': {
109             'id': '8fd44c2f-3dc5-4821-9118-2c825a98c0e1',
110             'ext': 'flv',
111             'title': 'Cena vs. Rollins Would Expose the Heavyweight Division',
112             'description': 'md5:984afb4ade2f9c0db35f3267ed88b36e',
113         },
114     }]
115
116     def _real_extract(self, url):
117         video_id = self._match_id(url)
118
119         info = self._extract_feed_info('http://cms.bleacherreport.com/media/items/%s/akamai.json' % video_id)
120         info['id'] = video_id
121         return info