Switch codebase to use sanitized_Request instead of
[youtube-dl] / youtube_dl / extractor / moevideo.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5 import re
6
7 from .common import InfoExtractor
8 from ..compat import compat_urllib_parse
9 from ..utils import (
10     ExtractorError,
11     int_or_none,
12     sanitized_Request,
13 )
14
15
16 class MoeVideoIE(InfoExtractor):
17     IE_DESC = 'LetitBit video services: moevideo.net, playreplay.net and videochart.net'
18     _VALID_URL = r'''(?x)
19         https?://(?P<host>(?:www\.)?
20         (?:(?:moevideo|playreplay|videochart)\.net))/
21         (?:video|framevideo)/(?P<id>[0-9]+\.[0-9A-Za-z]+)'''
22     _API_URL = 'http://api.letitbit.net/'
23     _API_KEY = 'tVL0gjqo5'
24     _TESTS = [
25         {
26             'url': 'http://moevideo.net/video/00297.0036103fe3d513ef27915216fd29',
27             'md5': '129f5ae1f6585d0e9bb4f38e774ffb3a',
28             'info_dict': {
29                 'id': '00297.0036103fe3d513ef27915216fd29',
30                 'ext': 'flv',
31                 'title': 'Sink cut out machine',
32                 'description': 'md5:f29ff97b663aefa760bf7ca63c8ca8a8',
33                 'thumbnail': 're:^https?://.*\.jpg$',
34                 'width': 540,
35                 'height': 360,
36                 'duration': 179,
37                 'filesize': 17822500,
38             }
39         },
40         {
41             'url': 'http://playreplay.net/video/77107.7f325710a627383d40540d8e991a',
42             'md5': '74f0a014d5b661f0f0e2361300d1620e',
43             'info_dict': {
44                 'id': '77107.7f325710a627383d40540d8e991a',
45                 'ext': 'flv',
46                 'title': 'Operacion Condor.',
47                 'description': 'md5:7e68cb2fcda66833d5081c542491a9a3',
48                 'thumbnail': 're:^https?://.*\.jpg$',
49                 'width': 480,
50                 'height': 296,
51                 'duration': 6027,
52                 'filesize': 588257923,
53             },
54             'skip': 'Video has been removed',
55         },
56     ]
57
58     def _real_extract(self, url):
59         mobj = re.match(self._VALID_URL, url)
60         video_id = mobj.group('id')
61
62         webpage = self._download_webpage(
63             'http://%s/video/%s' % (mobj.group('host'), video_id),
64             video_id, 'Downloading webpage')
65
66         title = self._og_search_title(webpage)
67         thumbnail = self._og_search_thumbnail(webpage)
68         description = self._og_search_description(webpage)
69
70         r = [
71             self._API_KEY,
72             [
73                 'preview/flv_link',
74                 {
75                     'uid': video_id,
76                 },
77             ],
78         ]
79         r_json = json.dumps(r)
80         post = compat_urllib_parse.urlencode({'r': r_json})
81         req = sanitized_Request(self._API_URL, post)
82         req.add_header('Content-type', 'application/x-www-form-urlencoded')
83
84         response = self._download_json(req, video_id)
85         if response['status'] != 'OK':
86             raise ExtractorError(
87                 '%s returned error: %s' % (self.IE_NAME, response['data']),
88                 expected=True
89             )
90         item = response['data'][0]
91         video_url = item['link']
92         duration = int_or_none(item['length'])
93         width = int_or_none(item['width'])
94         height = int_or_none(item['height'])
95         filesize = int_or_none(item['convert_size'])
96
97         formats = [{
98             'format_id': 'sd',
99             'http_headers': {'Range': 'bytes=0-'},  # Required to download
100             'url': video_url,
101             'width': width,
102             'height': height,
103             'filesize': filesize,
104         }]
105
106         return {
107             'id': video_id,
108             'title': title,
109             'thumbnail': thumbnail,
110             'description': description,
111             'duration': duration,
112             'formats': formats,
113         }