Merge branch 'gamersyde' of https://github.com/snipem/youtube-dl into snipem-gamersyde
[youtube-dl] / youtube_dl / extractor / gamersyde.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3 import re
4 import time
5
6 from .common import InfoExtractor
7
8
9 class GamersydeIE(InfoExtractor):
10     _VALID_URL = r'https?://(?:www\.)?gamersyde\.com/hqstream_'
11     _TESTS = [{
12         'url': 'http://www.gamersyde.com/hqstream_bloodborne_birth_of_a_hero-34371_en.html',
13         'md5': 'f38d400d32f19724570040d5ce3a505f',
14         'info_dict': {
15             'id': '34371',
16             'ext': 'mp4',
17             'duration': 372,
18             'title': 'Bloodborne - Birth of a hero',
19             'thumbnail': 're:^https?://.*\.jpg$',
20         }
21     }, {
22         'url': 'http://www.gamersyde.com/hqstream_dark_souls_ii_scholar_of_the_first_sin_gameplay_part_1-34417_en.html',
23         'md5': '94bd7c3feff3275576cf5cb6c8a3a720',
24         'info_dict': {
25             'id': '34417',
26             'ext': 'mp4',
27             'duration': 270,
28             'title': 'Dark Souls II: Scholar of the First Sin - Gameplay - Part 1',
29             'thumbnail': 're:^https?://.*\.jpg$',
30         }
31     }, {
32         'url': 'http://www.gamersyde.com/hqstream_grand_theft_auto_v_heists_trailer-33786_en.html',
33         'md5': '65e442f5f340d571ece8c80d50700369',
34         'info_dict': {
35             'id': '33786',
36             'ext': 'mp4',
37             'duration': 59,
38             'title': 'Grand Theft Auto V - Heists Trailer',
39             'thumbnail': 're:^https?://.*\.jpg$',
40         }
41     }
42     ]
43
44     def _calculateDuration(self, durationString):
45         if (durationString.find("minutes") > -1):
46             duration = time.strptime(durationString, "%M minutes %S seconds")
47         else:
48             duration = time.strptime(durationString, "%S seconds")
49         return duration.tm_min * 60 + duration.tm_sec
50
51     def _fixJsonSyntax(self, json):
52
53         json = re.sub(r",\s*}", "}", json, flags=re.DOTALL)
54         json = re.sub(r",\s*]", "]", json, flags=re.DOTALL)
55         json = json.replace('file: "', '"file": "')
56         json = json.replace('title: "', '"title": "')
57         json = json.replace('label: "', '"label": "')
58         json = json.replace('image: "', '"image": "')
59         json = json.replace('sources: [', '"sources": [')
60         return json
61
62     def _real_extract(self, url):
63
64         video_id = self._search_regex(r'-(.*?)_[a-z]{2}.html$', url, 'video_id')
65         webpage = self._download_webpage(url, video_id)
66
67         filesJson = self._search_regex(r'playlist: (.*?)\}\);', webpage, 'files', flags=re.DOTALL)
68         data = self._parse_json(filesJson,video_id, transform_source=self._fixJsonSyntax)
69         
70         playlist = data[0]
71
72         formats = []
73
74         title = re.sub(r"[0-9]+ - ", "", playlist['title'])
75         
76         length = self._search_regex(r'(([0-9]{1,2} minutes ){0,1}[0-9]{1,2} seconds)', webpage, 'length')
77         duration = self._calculateDuration(length)
78
79         for playlistEntry in playlist['sources']:
80             format = {
81                 'url': playlistEntry['file'],
82                 'format_id': playlistEntry['label']
83             }
84
85             formats.append(format)
86
87         return {
88             'id': video_id,
89             'title': title,
90             'formats': formats,
91             'duration': duration,
92             'thumbnail': playlist['image']
93             }