[4tube] Style and make more robust
[youtube-dl] / youtube_dl / extractor / fourtube.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import (
7     compat_urllib_request,
8 )
9 from ..utils import (
10     parse_duration,
11     parse_iso8601,
12     str_to_int,
13 )
14
15
16 class FourTubeIE(InfoExtractor):
17     IE_NAME = '4tube'
18     _VALID_URL = r'https?://(?:www\.)?4tube\.com/videos/(?P<id>\d+)'
19
20     _TEST = {
21         'url': 'http://www.4tube.com/videos/209733/hot-babe-holly-michaels-gets-her-ass-stuffed-by-black',
22         'md5': '6516c8ac63b03de06bc8eac14362db4f',
23         'info_dict': {
24             'id': '209733',
25             'ext': 'mp4',
26             'title': 'Hot Babe Holly Michaels gets her ass stuffed by black',
27             'uploader': 'WCP Club',
28             'uploader_id': 'wcp-club',
29             'upload_date': '20131031',
30             'timestamp': 1383263892,
31             'duration': 583,
32             'view_count': int,
33             'like_count': int,
34             'categories': list,
35             'age_limit': 18,
36         }
37     }
38
39     def _real_extract(self, url):
40         video_id = self._match_id(url)
41         webpage = self._download_webpage(url, video_id)
42
43         title = self._html_search_meta('name', webpage)
44         timestamp = parse_iso8601(self._html_search_meta(
45             'uploadDate', webpage))
46         thumbnail = self._html_search_meta('thumbnailUrl', webpage)
47         uploader_id = self._html_search_regex(
48             r'<a class="img-avatar" href="[^"]+/users/([^/"]+)" title="Go to [^"]+ page">', webpage, 'uploader id')
49         uploader = self._html_search_regex(
50             r'<a class="img-avatar" href="[^"]+/users/[^/"]+" title="Go to ([^"]+) page">', webpage, 'uploader')
51
52         categories_html = self._search_regex(
53             r'(?s)><i class="icon icon-tag"></i>\s*Categories / Tags\s*.*?<ul class="list">(.*?)</ul>',
54             webpage, 'categories', fatal=False)
55         categories = None
56         if categories_html:
57             categories = [
58                 c.strip() for c in re.findall(
59                     r'(?s)<li><a.*?>(.*?)</a>', categories_html)]
60
61         view_count = str_to_int(self._search_regex(
62             r'<meta itemprop="interactionCount" content="UserPlays:([0-9,]+)">',
63             webpage, 'view count', fatal=False))
64         like_count = str_to_int(self._search_regex(
65             r'<meta itemprop="interactionCount" content="UserLikes:([0-9,]+)">',
66             webpage, 'like count', fatal=False))
67         duration = parse_duration(self._html_search_meta('duration', webpage))
68
69         player_js = self._download_webpage(
70             self._search_regex(
71                 r'<script[^>]id=(["\'])playerembed\1[^>]+src=(["\'])(?P<url>.+?)\2',
72                 webpage, 'player JS', group='url'),
73             video_id, 'Downloading player JS')
74
75         params_js = self._search_regex(
76             r'\$\.ajax\(url,\ opts\);\s*\}\s*\}\)\(([0-9,\[\] ]+)\)',
77             player_js, 'initialization parameters'
78         )
79         params = self._parse_json('[%s]' % params_js, video_id)
80         media_id = params[0]
81         sources = ['%s' % p for p in params[2]]
82
83         token_url = 'http://tkn.4tube.com/{0}/desktop/{1}'.format(
84             media_id, '+'.join(sources))
85         headers = {
86             b'Content-Type': b'application/x-www-form-urlencoded',
87             b'Origin': b'http://www.4tube.com',
88         }
89         token_req = compat_urllib_request.Request(token_url, b'{}', headers)
90         tokens = self._download_json(token_req, video_id)
91         formats = [{
92             'url': tokens[format]['token'],
93             'format_id': format + 'p',
94             'resolution': format + 'p',
95             'quality': int(format),
96         } for format in sources]
97         self._sort_formats(formats)
98
99         return {
100             'id': video_id,
101             'title': title,
102             'formats': formats,
103             'categories': categories,
104             'thumbnail': thumbnail,
105             'uploader': uploader,
106             'uploader_id': uploader_id,
107             'timestamp': timestamp,
108             'like_count': like_count,
109             'view_count': view_count,
110             'duration': duration,
111             'age_limit': 18,
112         }