crooksandliars.com extractor
[youtube-dl] / youtube_dl / extractor / crooksandliars.py
1 from __future__ import unicode_literals
2
3 import json
4
5 from .common import InfoExtractor
6 from ..utils import (
7     mimetype2ext,
8 )
9
10
11 class CrooksAndLiarsIE(InfoExtractor):
12     _VALID_URL = r'(?:https?:)?//embed.crooksandliars.com/embed/(?P<id>[A-Za-z0-9]+)(?:$|[?#])'
13
14     _TESTS = [{
15         'url': 'https://embed.crooksandliars.com/embed/8RUoRhRi',
16         'info_dict': {
17             'id': 'https://embed.crooksandliars.com/embed/8RUoRhRi',
18             'title': "Fox & Friends Says Protecting Atheists From Discrimination Is Anti-Christian!",
19             'description': "Fox News, Fox & Friends Weekend, April 4, 2015. Read more... http://crooksandliars.com/2015/04/fox-friends-says-protecting-atheists",
20             'timestamp': 1428207000,
21             'thumbnail': '//crooksandliars.com/files/mediaposters/2015/04/31235.jpg?ts=1428207050',
22             'uploader': "Heather",
23         }
24     }]
25
26     def _real_extract(self, url):
27         video_id = self._match_id(url)
28         webpage = self._download_webpage(url, video_id)
29
30         manifest = json.loads(self._html_search_regex(r'var manifest = ({.*?})\n', webpage, 'manifest JSON'))
31
32         formats = []
33         for item in manifest['flavors']:
34             if not item['mime'].startswith('video/'): # XXX: or item['exclude']?
35                 continue
36             formats.append({
37                 'format_id': item['type'],
38                 'ext': mimetype2ext(item['mime']),
39                 'url': item['url'],
40             })
41
42         # XXX: manifest['url']?
43         return {
44             'url': url,
45             'id': video_id,
46             'uploader': manifest['author'],
47             'title': manifest['title'],
48             'description': manifest['description'],
49             'thumbnail': manifest['poster'],
50             'duration': manifest['duration'],
51             'timestamp': int(manifest['created']),
52             'formats': formats,
53         }
54
55 class CrooksAndLiarsArticleIE(InfoExtractor):
56     _VALID_URL = r'(?:https?:)?//crooksandliars.com/\d+/\d+/(?P<id>[a-z\-]+)(?:/|$)'
57
58     _TESTS = [{
59         'url': 'http://crooksandliars.com/2015/04/fox-friends-says-protecting-atheists',
60         'only_matching': True,
61     }]
62
63     def _real_extract(self, url):
64         video_id = self._match_id(url)
65         webpage = self._download_webpage(url, video_id)
66         player_url = self._proto_relative_url(self._html_search_regex(r'<iframe src="(//embed.crooksandliars.com/.*)"', webpage, 'embedded player'))
67
68         return {
69             '_type': 'url',
70             'url': player_url
71         }