Created
January 5, 2015 04:56
-
-
Save mattiasb/9ac0f454c0ca830aba1a to your computer and use it in GitHub Desktop.
Twitch.tv grilo plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
* Copyright (C) 2015 Mattias Bengtsson | |
* | |
* Contact: Mattias Bengtsson <[email protected]> | |
* | |
* This library is free software; you can redistribute it and/or | |
* modify it under the terms of the GNU Lesser General Public License | |
* as published by the Free Software Foundation; version 2.1 of | |
* the License, or (at your option) any later version. | |
* | |
* This library is distributed in the hope that it will be useful, but | |
* WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
* Lesser General Public License for more details. | |
* | |
* You should have received a copy of the GNU Lesser General Public | |
* License along with this library; if not, write to the Free Software | |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
* 02110-1301 USA | |
* | |
--]] | |
TWITCH_API = 'https://api.twitch.tv/kraken' | |
-- Start with just MTG streams | |
TWITCH_STREAMS = TWITCH_API .. '/streams?game=Magic:%20The%20Gathering&hls=true' | |
--------------------------- | |
-- Source initialization -- | |
--------------------------- | |
source = { | |
id = "grl-twitch-lua", | |
name = "Twitch.tv", | |
description = "A source for watching Twitch.tv", | |
supported_keys = { "id", "title", "url" }, | |
supported_media = 'video', | |
tags = { 'games', 'tv', 'net:internet' } | |
} | |
------------------ | |
-- Source utils -- | |
------------------ | |
function grl_source_browse(media_id) | |
if grl.get_options("skip") > 0 then | |
grl.callback() | |
else | |
grl.fetch(TWITCH_STREAMS, "twitch_fetch_streams_cb") | |
end | |
end | |
function twitch_fetch_streams_cb(data) | |
local json = {} | |
json = grl.lua.json.string_to_table(data) | |
if not json or json.stat == "fail" or not json.streams then | |
grl.callback() | |
return | |
end | |
for index, stream in pairs(json.streams) do | |
local media = create_media(stream) | |
if media ~= nil then | |
grl.callback(media, -1) | |
end | |
end | |
grl.callback() | |
end | |
------------- | |
-- Helpers -- | |
------------- | |
function create_media(stream) | |
local media = {} | |
media.type = "video" | |
media.id = stream.channel.name | |
media.title = stream.channel.display_name | |
.. "\n " | |
.. stream.channel.status | |
media.thumbnail = stream.preview.large | |
-- media.url = | |
return media | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment