Merge branch 'vgtv' of https://github.com/mrkolby/youtube-dl into mrkolby-vgtv
[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             'duration': 1317,
39         }
40     }
41
42     def _real_extract(self, url):
43         mobj = re.match(self._VALID_URL, url)
44         video_id = mobj.group('id')
45
46         webpage = self._download_webpage(url, video_id)
47
48         video_title = self._search_regex(
49             r'mediaTitle = "([^"]+)"', webpage, 'title')
50
51         pl_fiji = self._search_regex(
52             r'pl_fiji = \'([^\']+)\'', webpage, 'video data')
53         pl_c_qual = self._search_regex(
54             r'pl_c_qual = "(.)"', webpage, 'video quality')
55         video_url = _decode(pl_fiji)
56         formats = [{
57             'url': video_url,
58             'format_id': 'default-%s' % pl_c_qual,
59         }]
60
61         qualities_json = self._search_regex(
62             r'var pl_qualities\s*=\s*(.*?);\n', webpage, 'quality info')
63         qualities_data = json.loads(qualities_json)
64
65         for i, t in enumerate(
66                 re.findall(r"pl_fiji_([a-z0-9]+)\s*=\s*'([^']+')", webpage)):
67             quality_id, crypted_url = t
68             video_url = _decode(crypted_url)
69             f = {
70                 'format_id': quality_id,
71                 'url': video_url,
72                 'quality': i,
73             }
74             if quality_id in qualities_data:
75                 qd = qualities_data[quality_id]
76                 m = re.match(
77                     r'''(?x)<b>(?P<width>[0-9]+)x(?P<height>[0-9]+)<\\/b>
78                         \s*\(\s*(?P<tbr>[0-9]+)\s*kb\\/s''', qd)
79                 if m:
80                     f['width'] = int(m.group('width'))
81                     f['height'] = int(m.group('height'))
82                     f['tbr'] = int(m.group('tbr'))
83             formats.append(f)
84         self._sort_formats(formats)
85
86         thumbnail = self._search_regex(
87             r"var\s+mov_thumb\s*=\s*'([^']+)';",
88             webpage, 'thumbnail', fatal=False)
89         duration = int_or_none(self._search_regex(
90             r'pl_dur\s*=\s*([0-9]+)', webpage, 'duration', fatal=False))
91
92         return {
93             'id': video_id,
94             'title': video_title,
95             'formats': formats,
96             'duration': duration,
97             'age_limit': self._rta_search(webpage),
98             'thumbnail': thumbnail,
99         }