[cracked] Add support for youtube embeds
[youtube-dl] / youtube_dl / extractor / cracked.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     parse_iso8601,
8     str_to_int,
9 )
10
11
12 class CrackedIE(InfoExtractor):
13     _VALID_URL = r'https?://(?:www\.)?cracked\.com/video_(?P<id>\d+)_[\da-z-]+\.html'
14     _TEST = {
15         'url': 'http://www.cracked.com/video_19006_4-plot-holes-you-didnt-notice-in-your-favorite-movies.html',
16         'md5': '4b29a5eeec292cd5eca6388c7558db9e',
17         'info_dict': {
18             'id': '19006',
19             'ext': 'mp4',
20             'title': '4 Plot Holes You Didn\'t Notice in Your Favorite Movies',
21             'description': 'md5:3b909e752661db86007d10e5ec2df769',
22             'timestamp': 1405659600,
23             'upload_date': '20140718',
24         }
25     }
26
27     def _real_extract(self, url):
28         mobj = re.match(self._VALID_URL, url)
29         video_id = mobj.group('id')
30
31         webpage = self._download_webpage(url, video_id)
32
33         youtube_url = self._search_regex(
34             r'<iframe[^>]+src="((?:https?:)?//www\.youtube\.com/embed/[^"]+)"',
35             webpage, 'youtube url', default=None)
36         if youtube_url:
37             return self.url_result(youtube_url)
38
39         video_url = self._html_search_regex(
40             [r'var\s+CK_vidSrc\s*=\s*"([^"]+)"', r'<video\s+src="([^"]+)"'],
41             webpage, 'video URL')
42
43         title = self._search_regex(
44             [r'property="?og:title"?\s+content="([^"]+)"', r'class="?title"?>([^<]+)'],
45             webpage, 'title')
46
47         description = self._search_regex(
48             r'name="?(?:og:)?description"?\s+content="([^"]+)"',
49             webpage, 'description', default=None)
50
51         timestamp = self._html_search_regex(
52             r'"date"\s*:\s*"([^"]+)"', webpage, 'upload date', fatal=False)
53         if timestamp:
54             timestamp = parse_iso8601(timestamp[:-6])
55
56         view_count = str_to_int(self._html_search_regex(
57             r'<span\s+class="?views"? id="?viewCounts"?>([\d,\.]+) Views</span>',
58             webpage, 'view count', fatal=False))
59         comment_count = str_to_int(self._html_search_regex(
60             r'<span\s+id="?commentCounts"?>([\d,\.]+)</span>',
61             webpage, 'comment count', fatal=False))
62
63         m = re.search(r'_(?P<width>\d+)X(?P<height>\d+)\.mp4$', video_url)
64         if m:
65             width = int(m.group('width'))
66             height = int(m.group('height'))
67         else:
68             width = height = None
69
70         return {
71             'id': video_id,
72             'url': video_url,
73             'title': title,
74             'description': description,
75             'timestamp': timestamp,
76             'view_count': view_count,
77             'comment_count': comment_count,
78             'height': height,
79             'width': width,
80         }