[cracked] Update tests
[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     _TESTS = [{
15         'url': 'http://www.cracked.com/video_19070_if-animal-actors-got-e21-true-hollywood-stories.html',
16         'md5': '89b90b9824e3806ca95072c4d78f13f7',
17         'info_dict': {
18             'id': '19070',
19             'ext': 'mp4',
20             'title': 'If Animal Actors Got E! True Hollywood Stories',
21             'timestamp': 1404954000,
22             'upload_date': '20140710',
23         }
24     }, {
25         # youtube embed
26         'url': 'http://www.cracked.com/video_19006_4-plot-holes-you-didnt-notice-in-your-favorite-movies.html',
27         'md5': 'ccd52866b50bde63a6ef3b35016ba8c7',
28         'info_dict': {
29             'id': 'EjI00A3rZD0',
30             'ext': 'mp4',
31             'title': "4 Plot Holes You Didn't Notice in Your Favorite Movies - The Spit Take",
32             'description': 'md5:c603708c718b796fe6079e2b3351ffc7',
33             'upload_date': '20140725',
34             'uploader_id': 'Cracked',
35             'uploader': 'Cracked',
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
45         youtube_url = self._search_regex(
46             r'<iframe[^>]+src="((?:https?:)?//www\.youtube\.com/embed/[^"]+)"',
47             webpage, 'youtube url', default=None)
48         if youtube_url:
49             return self.url_result(youtube_url, 'Youtube')
50
51         video_url = self._html_search_regex(
52             [r'var\s+CK_vidSrc\s*=\s*"([^"]+)"', r'<video\s+src="([^"]+)"'],
53             webpage, 'video URL')
54
55         title = self._search_regex(
56             [r'property="?og:title"?\s+content="([^"]+)"', r'class="?title"?>([^<]+)'],
57             webpage, 'title')
58
59         description = self._search_regex(
60             r'name="?(?:og:)?description"?\s+content="([^"]+)"',
61             webpage, 'description', default=None)
62
63         timestamp = self._html_search_regex(
64             r'"date"\s*:\s*"([^"]+)"', webpage, 'upload date', fatal=False)
65         if timestamp:
66             timestamp = parse_iso8601(timestamp[:-6])
67
68         view_count = str_to_int(self._html_search_regex(
69             r'<span\s+class="?views"? id="?viewCounts"?>([\d,\.]+) Views</span>',
70             webpage, 'view count', fatal=False))
71         comment_count = str_to_int(self._html_search_regex(
72             r'<span\s+id="?commentCounts"?>([\d,\.]+)</span>',
73             webpage, 'comment count', fatal=False))
74
75         m = re.search(r'_(?P<width>\d+)X(?P<height>\d+)\.mp4$', video_url)
76         if m:
77             width = int(m.group('width'))
78             height = int(m.group('height'))
79         else:
80             width = height = None
81
82         return {
83             'id': video_id,
84             'url': video_url,
85             'title': title,
86             'description': description,
87             'timestamp': timestamp,
88             'view_count': view_count,
89             'comment_count': comment_count,
90             'height': height,
91             'width': width,
92         }