[ellentv] Fix extraction
[youtube-dl] / youtube_dl / extractor / ellentv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5
6 from .common import InfoExtractor
7 from ..utils import (
8     ExtractorError,
9     parse_iso8601,
10 )
11
12
13 class EllenTVIE(InfoExtractor):
14     _VALID_URL = r'https?://(?:www\.)?(?:ellentv|ellentube)\.com/videos/(?P<id>[a-z0-9_-]+)'
15     _TESTS = [{
16         'url': 'http://www.ellentv.com/videos/0-ipq1gsai/',
17         'md5': '8e3c576bf2e9bfff4d76565f56f94c9c',
18         'info_dict': {
19             'id': '0-ipq1gsai',
20             'ext': 'mp4',
21             'title': 'Fast Fingers of Fate',
22             'description': 'md5:686114ced0a032926935e9015ee794ac',
23             'timestamp': 1428033600,
24             'upload_date': '20150403',
25         }
26     }, {
27         'url': 'http://ellentube.com/videos/0-dvzmabd5/',
28         'md5': '98238118eaa2bbdf6ad7f708e3e4f4eb',
29         'info_dict': {
30             'id': '0-dvzmabd5',
31             'ext': 'mp4',
32             'title': '1 year old twin sister makes her brother laugh',
33             'description': '1 year old twin sister makes her brother laugh',
34             'timestamp': 1419542075,
35             'upload_date': '20141225',
36         }
37     }]
38
39     def _real_extract(self, url):
40         video_id = self._match_id(url)
41
42         webpage = self._download_webpage(
43             'http://widgets.ellentube.com/videos/%s' % video_id,
44             video_id)
45
46         partner_id = self._search_regex(
47             r"var\s+partnerId\s*=\s*'([^']+)", webpage, 'partner id')
48
49         kaltura_id = self._search_regex(
50             [r'id="kaltura_player_([^"]+)"',
51              r"_wb_entry_id\s*:\s*'([^']+)",
52              r'data-kaltura-entry-id="([^"]+)'],
53             webpage, 'kaltura id')
54
55         return self.url_result('kaltura:%s:%s' % (partner_id, kaltura_id), 'Kaltura')
56
57
58 class EllenTVClipsIE(InfoExtractor):
59     IE_NAME = 'EllenTV:clips'
60     _VALID_URL = r'https?://(?:www\.)?ellentv\.com/episodes/(?P<id>[a-z0-9_-]+)'
61     _TEST = {
62         'url': 'http://www.ellentv.com/episodes/meryl-streep-vanessa-hudgens/',
63         'info_dict': {
64             'id': 'meryl-streep-vanessa-hudgens',
65             'title': 'Meryl Streep, Vanessa Hudgens',
66         },
67         'playlist_mincount': 9,
68     }
69
70     def _real_extract(self, url):
71         playlist_id = self._match_id(url)
72
73         webpage = self._download_webpage(url, playlist_id)
74         playlist = self._extract_playlist(webpage)
75
76         return {
77             '_type': 'playlist',
78             'id': playlist_id,
79             'title': self._og_search_title(webpage),
80             'entries': self._extract_entries(playlist)
81         }
82
83     def _extract_playlist(self, webpage):
84         json_string = self._search_regex(r'playerView.addClips\(\[\{(.*?)\}\]\);', webpage, 'json')
85         try:
86             return json.loads("[{" + json_string + "}]")
87         except ValueError as ve:
88             raise ExtractorError('Failed to download JSON', cause=ve)
89
90     def _extract_entries(self, playlist):
91         return [self.url_result(item['url'], 'EllenTV') for item in playlist]