Merge pull request #4626 from gauravb7090/ellentube
[youtube-dl] / youtube_dl / extractor / ellentv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import json
6
7 from .common import InfoExtractor
8 from ..utils import (
9     ExtractorError,
10     parse_iso8601,
11 )
12
13
14 class EllenTVIE(InfoExtractor):
15     _VALID_URL = r'https?://(?:www\.)?(ellentv|ellentube)\.com/videos/(?P<id>[a-z0-9_-]+)'
16     _TESTS = [{
17         'url': 'http://www.ellentv.com/videos/0-7jqrsr18/',
18         'md5': 'e4af06f3bf0d5f471921a18db5764642',
19         'info_dict': {
20             'id': '0-7jqrsr18',
21             'ext': 'mp4',
22             'title': 'What\'s Wrong with These Photos? A Whole Lot',
23             'timestamp': 1406876400,
24             'upload_date': '20140801',
25         }
26     },
27     {
28         'url': 'http://ellentube.com/videos/0-dvzmabd5/',
29         'md5': '98238118eaa2bbdf6ad7f708e3e4f4eb',
30         'info_dict': {
31             'id': '0-dvzmabd5',
32             'ext': 'mp4',
33             'title': '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         mobj = re.match(self._VALID_URL, url)
41         video_id = mobj.group('id')
42
43         webpage = self._download_webpage(url, video_id)
44         timestamp = parse_iso8601(self._search_regex(
45             r'<span class="publish-date"><time datetime="([^"]+)">',
46             webpage, 'timestamp'))
47         print "\n"
48         print timestamp
49         print "\n"
50
51         return {
52             'id': video_id,
53             'title': self._og_search_title(webpage),
54             'url': self._html_search_meta('VideoURL', webpage, 'url'),
55             'timestamp': timestamp,
56         }
57
58
59 class EllenTVClipsIE(InfoExtractor):
60     IE_NAME = 'EllenTV:clips'
61     _VALID_URL = r'https?://(?:www\.)?ellentv\.com/episodes/(?P<id>[a-z0-9_-]+)'
62     _TEST = {
63         'url': 'http://www.ellentv.com/episodes/meryl-streep-vanessa-hudgens/',
64         'info_dict': {
65             'id': 'meryl-streep-vanessa-hudgens',
66             'title': 'Meryl Streep, Vanessa Hudgens',
67         },
68         'playlist_mincount': 9,
69     }
70
71     def _real_extract(self, url):
72         mobj = re.match(self._VALID_URL, url)
73         playlist_id = mobj.group('id')
74
75         webpage = self._download_webpage(url, playlist_id)
76         playlist = self._extract_playlist(webpage)
77
78         return {
79             '_type': 'playlist',
80             'id': playlist_id,
81             'title': self._og_search_title(webpage),
82             'entries': self._extract_entries(playlist)
83         }
84
85     def _extract_playlist(self, webpage):
86         json_string = self._search_regex(r'playerView.addClips\(\[\{(.*?)\}\]\);', webpage, 'json')
87         try:
88             return json.loads("[{" + json_string + "}]")
89         except ValueError as ve:
90             raise ExtractorError('Failed to download JSON', cause=ve)
91
92     def _extract_entries(self, playlist):
93         return [self.url_result(item['url'], 'EllenTV') for item in playlist]