Skip to content

Instantly share code, notes, and snippets.

@MaZderMind
Created April 9, 2015 12:20
Show Gist options
  • Save MaZderMind/37a84d27591ff918a0f5 to your computer and use it in GitHub Desktop.
Save MaZderMind/37a84d27591ff918a0f5 to your computer and use it in GitHub Desktop.
media_ccc_de livestreamer plugin
"""Plugin for media.ccc.de
Supports:
- http://media.ccc.de
- http://streaming.media.ccc.de (not yet implemented)
"""
import re
import requests
import json
from livestreamer.plugin import Plugin
from livestreamer.stream import HTTPStream
API_URL_MEDIA = "https://api.media.ccc.de/public/events/"
def get_event_id(url):
page = requests.get(url)
match = re.search(r"{event_id:\s(?P<event_id>\d+),.*}", page.text)
try:
event_id = int(match.group('event_id'))
except:
raise exception("Failed to get event id from '{url}'.")
return event_id
def get_api_json(event_id, api_url):
query_url = "%s%i" % (api_url, event_id)
page = requests.get(query_url)
return page.text
def parse_media_json(event_id, json_string):
try:
json_object = json.loads(json_string)
except:
raise exception("Could not parse json from api.")
recordings = {}
for recording in json_object['recordings']:
match = re.search(r".*\/(?P<format>.*)", recording['mime_type'])
file_format = match.group('format')
if recording['mime_type'] == 'vnd.voc/mp4-web':
continue
elif recording['mime_type'] == 'vnd.voc/h264-hd':
name = "hd_mp4"
elif recording['mime_type'] == 'vnd.voc/webm-hd':
name = "hd_webm"
elif recording['mime_type'] == 'vnd.voc/h264-lq':
name = "lq_mp4"
elif re.match(r"audio", recording['display_mime_type']):
name = "audio_%s" % file_format
else:
if recording['hd'] == 'true':
name = "hd_%s" % file_format
else:
name = "sd_%s" % file_format
recordings[name] = recording['recording_url']
return recordings
class media_ccc_de(Plugin):
@classmethod
def can_handle_url(cls, url):
return re.match(r"http(s)?://media.ccc.de/|http(s)?://streaming.media.ccc.de/", url)
def _get_streams(self):
event_id = get_event_id(self.url)
recordings = parse_media_json(event_id, get_api_json(event_id, API_URL_MEDIA))
streams = {}
for name in recordings.keys():
stream_url = recordings[name]
streams[name] = HTTPStream(self.session, stream_url)
if not streams:
raise PluginError("This plugin does not support your "
"selected video.")
return streams
__plugin__ = media_ccc_de
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment