[gamersyde] Simplify

This commit is contained in:
Sergey M․ 2015-04-04 17:53:22 +06:00
parent ba9e68f402
commit 5c29dbd0c7

View File

@ -1,14 +1,18 @@
# coding: utf-8
from __future__ import unicode_literals from __future__ import unicode_literals
import re import re
import time
from .common import InfoExtractor from .common import InfoExtractor
from ..utils import (
js_to_json,
parse_duration,
remove_start,
)
class GamersydeIE(InfoExtractor): class GamersydeIE(InfoExtractor):
_VALID_URL = r'https?://(?:www\.)?gamersyde\.com/hqstream_' _VALID_URL = r'https?://(?:www\.)?gamersyde\.com/hqstream_(?P<display_id>[\da-z_]+)-(?P<id>\d+)_[a-z]{2}\.html'
_TESTS = [{ _TEST = {
'url': 'http://www.gamersyde.com/hqstream_bloodborne_birth_of_a_hero-34371_en.html', 'url': 'http://www.gamersyde.com/hqstream_bloodborne_birth_of_a_hero-34371_en.html',
'md5': 'f38d400d32f19724570040d5ce3a505f', 'md5': 'f38d400d32f19724570040d5ce3a505f',
'info_dict': { 'info_dict': {
@ -18,76 +22,49 @@ class GamersydeIE(InfoExtractor):
'title': 'Bloodborne - Birth of a hero', 'title': 'Bloodborne - Birth of a hero',
'thumbnail': 're:^https?://.*\.jpg$', 'thumbnail': 're:^https?://.*\.jpg$',
} }
}, {
'url': 'http://www.gamersyde.com/hqstream_dark_souls_ii_scholar_of_the_first_sin_gameplay_part_1-34417_en.html',
'md5': '94bd7c3feff3275576cf5cb6c8a3a720',
'info_dict': {
'id': '34417',
'ext': 'mp4',
'duration': 270,
'title': 'Dark Souls II: Scholar of the First Sin - Gameplay - Part 1',
'thumbnail': 're:^https?://.*\.jpg$',
}
}, {
'url': 'http://www.gamersyde.com/hqstream_grand_theft_auto_v_heists_trailer-33786_en.html',
'md5': '65e442f5f340d571ece8c80d50700369',
'info_dict': {
'id': '33786',
'ext': 'mp4',
'duration': 59,
'title': 'Grand Theft Auto V - Heists Trailer',
'thumbnail': 're:^https?://.*\.jpg$',
}
} }
]
def _calculateDuration(self, durationString):
if (durationString.find("minutes") > -1):
duration = time.strptime(durationString, "%M minutes %S seconds")
else:
duration = time.strptime(durationString, "%S seconds")
return duration.tm_min * 60 + duration.tm_sec
def _fixJsonSyntax(self, json):
json = re.sub(r",\s*}", "}", json, flags=re.DOTALL)
json = re.sub(r",\s*]", "]", json, flags=re.DOTALL)
json = json.replace('file: "', '"file": "')
json = json.replace('title: "', '"title": "')
json = json.replace('label: "', '"label": "')
json = json.replace('image: "', '"image": "')
json = json.replace('sources: [', '"sources": [')
return json
def _real_extract(self, url): def _real_extract(self, url):
mobj = re.match(self._VALID_URL, url)
video_id = mobj.group('id')
display_id = mobj.group('display_id')
video_id = self._search_regex(r'-(.*?)_[a-z]{2}.html$', url, 'video_id') webpage = self._download_webpage(url, display_id)
webpage = self._download_webpage(url, video_id)
filesJson = self._search_regex(r'playlist: (.*?)\}\);', webpage, 'files', flags=re.DOTALL) playlist = self._parse_json(
data = self._parse_json(filesJson,video_id, transform_source=self._fixJsonSyntax) self._search_regex(
r'(?s)playlist: \[({.+?})\]\s*}\);', webpage, 'files'),
playlist = data[0] display_id, transform_source=js_to_json)
formats = [] formats = []
for source in playlist['sources']:
title = re.sub(r"[0-9]+ - ", "", playlist['title']) video_url = source.get('file')
if not video_url:
length = self._search_regex(r'(([0-9]{1,2} minutes ){0,1}[0-9]{1,2} seconds)', webpage, 'length') continue
duration = self._calculateDuration(length) format_id = source.get('label')
f = {
for playlistEntry in playlist['sources']: 'url': video_url,
format = { 'format_id': format_id,
'url': playlistEntry['file'],
'format_id': playlistEntry['label']
} }
m = re.search(r'^(?P<height>\d+)[pP](?P<fps>\d+)fps', format_id)
if m:
f.update({
'height': int(m.group('height')),
'fps': int(m.group('fps')),
})
formats.append(f)
self._sort_formats(formats)
formats.append(format) title = remove_start(playlist['title'], '%s - ' % video_id)
thumbnail = playlist.get('image')
duration = parse_duration(self._search_regex(
r'Length:</label>([^<]+)<', webpage, 'duration', fatal=False))
return { return {
'id': video_id, 'id': video_id,
'display_id': display_id,
'title': title, 'title': title,
'formats': formats, 'thumbnail': thumbnail,
'duration': duration, 'duration': duration,
'thumbnail': playlist['image'] 'formats': formats,
} }