Merge pull request #3865 from diffycat/jpopsuki
[youtube-dl] / youtube_dl / extractor / cliphunter.py
1 from __future__ import unicode_literals
2
3 import json
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import int_or_none
8
9
10 _translation_table = {
11     'a': 'h', 'd': 'e', 'e': 'v', 'f': 'o', 'g': 'f', 'i': 'd', 'l': 'n',
12     'm': 'a', 'n': 'm', 'p': 'u', 'q': 't', 'r': 's', 'v': 'p', 'x': 'r',
13     'y': 'l', 'z': 'i',
14     '$': ':', '&': '.', '(': '=', '^': '&', '=': '/',
15 }
16
17
18 def _decode(s):
19     return ''.join(_translation_table.get(c, c) for c in s)
20
21
22 class CliphunterIE(InfoExtractor):
23     IE_NAME = 'cliphunter'
24
25     _VALID_URL = r'''(?x)http://(?:www\.)?cliphunter\.com/w/
26         (?P<id>[0-9]+)/
27         (?P<seo>.+?)(?:$|[#\?])
28     '''
29     _TEST = {
30         'url': 'http://www.cliphunter.com/w/1012420/Fun_Jynx_Maze_solo',
31         'md5': 'a2ba71eebf523859fe527a61018f723e',
32         'info_dict': {
33             'id': '1012420',
34             'ext': 'mp4',
35             'title': 'Fun Jynx Maze solo',
36             'thumbnail': 're:^https?://.*\.jpg$',
37             'age_limit': 18,
38         }
39     }
40
41     def _real_extract(self, url):
42         mobj = re.match(self._VALID_URL, url)
43         video_id = mobj.group('id')
44
45         webpage = self._download_webpage(url, video_id)
46
47         video_title = self._search_regex(
48             r'mediaTitle = "([^"]+)"', webpage, 'title')
49
50         pl_fiji = self._search_regex(
51             r'pl_fiji = \'([^\']+)\'', webpage, 'video data')
52         pl_c_qual = self._search_regex(
53             r'pl_c_qual = "(.)"', webpage, 'video quality')
54         video_url = _decode(pl_fiji)
55         formats = [{
56             'url': video_url,
57             'format_id': 'default-%s' % pl_c_qual,
58         }]
59
60         qualities_json = self._search_regex(
61             r'var pl_qualities\s*=\s*(.*?);\n', webpage, 'quality info')
62         qualities_data = json.loads(qualities_json)
63
64         for i, t in enumerate(
65                 re.findall(r"pl_fiji_([a-z0-9]+)\s*=\s*'([^']+')", webpage)):
66             quality_id, crypted_url = t
67             video_url = _decode(crypted_url)
68             f = {
69                 'format_id': quality_id,
70                 'url': video_url,
71                 'quality': i,
72             }
73             if quality_id in qualities_data:
74                 qd = qualities_data[quality_id]
75                 m = re.match(
76                     r'''(?x)<b>(?P<width>[0-9]+)x(?P<height>[0-9]+)<\\/b>
77                         \s*\(\s*(?P<tbr>[0-9]+)\s*kb\\/s''', qd)
78                 if m:
79                     f['width'] = int(m.group('width'))
80                     f['height'] = int(m.group('height'))
81                     f['tbr'] = int(m.group('tbr'))
82             formats.append(f)
83         self._sort_formats(formats)
84
85         thumbnail = self._search_regex(
86             r"var\s+mov_thumb\s*=\s*'([^']+)';",
87             webpage, 'thumbnail', fatal=False)
88
89         return {
90             'id': video_id,
91             'title': video_title,
92             'formats': formats,
93             'age_limit': self._rta_search(webpage),
94             'thumbnail': thumbnail,
95         }