Switch codebase to use sanitized_Request instead of
[youtube-dl] / youtube_dl / extractor / pornotube.py
1 from __future__ import unicode_literals
2
3 import json
4
5 from .common import InfoExtractor
6 from ..utils import (
7     int_or_none,
8     sanitized_Request,
9 )
10
11
12 class PornotubeIE(InfoExtractor):
13     _VALID_URL = r'https?://(?:\w+\.)?pornotube\.com/(?:[^?#]*?)/video/(?P<id>[0-9]+)'
14     _TEST = {
15         'url': 'http://www.pornotube.com/orientation/straight/video/4964/title/weird-hot-and-wet-science',
16         'md5': '60fc5a4f0d93a97968fc7999d98260c9',
17         'info_dict': {
18             'id': '4964',
19             'ext': 'mp4',
20             'upload_date': '20141203',
21             'title': 'Weird Hot and Wet Science',
22             'description': 'md5:a8304bef7ef06cb4ab476ca6029b01b0',
23             'categories': ['Adult Humor', 'Blondes'],
24             'uploader': 'Alpha Blue Archives',
25             'thumbnail': 're:^https?://.*\\.jpg$',
26             'timestamp': 1417582800,
27             'age_limit': 18,
28         }
29     }
30
31     def _real_extract(self, url):
32         video_id = self._match_id(url)
33
34         # Fetch origin token
35         js_config = self._download_webpage(
36             'http://www.pornotube.com/assets/src/app/config.js', video_id,
37             note='Download JS config')
38         originAuthenticationSpaceKey = self._search_regex(
39             r"constant\('originAuthenticationSpaceKey',\s*'([^']+)'",
40             js_config, 'originAuthenticationSpaceKey')
41
42         # Fetch actual token
43         token_req_data = {
44             'authenticationSpaceKey': originAuthenticationSpaceKey,
45             'credentials': 'Clip Application',
46         }
47         token_req = sanitized_Request(
48             'https://api.aebn.net/auth/v1/token/primal',
49             data=json.dumps(token_req_data).encode('utf-8'))
50         token_req.add_header('Content-Type', 'application/json')
51         token_req.add_header('Origin', 'http://www.pornotube.com')
52         token_answer = self._download_json(
53             token_req, video_id, note='Requesting primal token')
54         token = token_answer['tokenKey']
55
56         # Get video URL
57         delivery_req = sanitized_Request(
58             'https://api.aebn.net/delivery/v1/clips/%s/MP4' % video_id)
59         delivery_req.add_header('Authorization', token)
60         delivery_info = self._download_json(
61             delivery_req, video_id, note='Downloading delivery information')
62         video_url = delivery_info['mediaUrl']
63
64         # Get additional info (title etc.)
65         info_req = sanitized_Request(
66             'https://api.aebn.net/content/v1/clips/%s?expand='
67             'title,description,primaryImageNumber,startSecond,endSecond,'
68             'movie.title,movie.MovieId,movie.boxCoverFront,movie.stars,'
69             'movie.studios,stars.name,studios.name,categories.name,'
70             'clipActive,movieActive,publishDate,orientations' % video_id)
71         info_req.add_header('Authorization', token)
72         info = self._download_json(
73             info_req, video_id, note='Downloading metadata')
74
75         timestamp = int_or_none(info.get('publishDate'), scale=1000)
76         uploader = info.get('studios', [{}])[0].get('name')
77         movie_id = info['movie']['movieId']
78         thumbnail = 'http://pic.aebn.net/dis/t/%s/%s_%08d.jpg' % (
79             movie_id, movie_id, info['primaryImageNumber'])
80         categories = [c['name'] for c in info.get('categories')]
81
82         return {
83             'id': video_id,
84             'url': video_url,
85             'title': info['title'],
86             'description': info.get('description'),
87             'timestamp': timestamp,
88             'uploader': uploader,
89             'thumbnail': thumbnail,
90             'categories': categories,
91             'age_limit': 18,
92         }