[ellentv] Clean up and simplify
[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-7jqrsr18/',
17         'md5': 'e4af06f3bf0d5f471921a18db5764642',
18         'info_dict': {
19             'id': '0-7jqrsr18',
20             'ext': 'mp4',
21             'title': 'What\'s Wrong with These Photos? A Whole Lot',
22             'timestamp': 1406876400,
23             'upload_date': '20140801',
24         }
25     }, {
26         'url': 'http://ellentube.com/videos/0-dvzmabd5/',
27         'md5': '98238118eaa2bbdf6ad7f708e3e4f4eb',
28         'info_dict': {
29             'id': '0-dvzmabd5',
30             'ext': 'mp4',
31             'title': '1 year old twin sister makes her brother laugh',
32             'timestamp': 1419542075,
33             'upload_date': '20141225',
34         }
35     }]
36
37     def _real_extract(self, url):
38         video_id = self._match_id(url)
39
40         webpage = self._download_webpage(url, video_id)
41         timestamp = parse_iso8601(self._search_regex(
42             r'<span class="publish-date"><time datetime="([^"]+)">',
43             webpage, 'timestamp'))
44
45         return {
46             'id': video_id,
47             'title': self._og_search_title(webpage),
48             'url': self._html_search_meta('VideoURL', webpage, 'url'),
49             'timestamp': timestamp,
50         }
51
52
53 class EllenTVClipsIE(InfoExtractor):
54     IE_NAME = 'EllenTV:clips'
55     _VALID_URL = r'https?://(?:www\.)?ellentv\.com/episodes/(?P<id>[a-z0-9_-]+)'
56     _TEST = {
57         'url': 'http://www.ellentv.com/episodes/meryl-streep-vanessa-hudgens/',
58         'info_dict': {
59             'id': 'meryl-streep-vanessa-hudgens',
60             'title': 'Meryl Streep, Vanessa Hudgens',
61         },
62         'playlist_mincount': 9,
63     }
64
65     def _real_extract(self, url):
66         playlist_id = self._match_id(url)
67
68         webpage = self._download_webpage(url, playlist_id)
69         playlist = self._extract_playlist(webpage)
70
71         return {
72             '_type': 'playlist',
73             'id': playlist_id,
74             'title': self._og_search_title(webpage),
75             'entries': self._extract_entries(playlist)
76         }
77
78     def _extract_playlist(self, webpage):
79         json_string = self._search_regex(r'playerView.addClips\(\[\{(.*?)\}\]\);', webpage, 'json')
80         try:
81             return json.loads("[{" + json_string + "}]")
82         except ValueError as ve:
83             raise ExtractorError('Failed to download JSON', cause=ve)
84
85     def _extract_entries(self, playlist):
86         return [self.url_result(item['url'], 'EllenTV') for item in playlist]