-- This is a proprietary plugin, property of Andreas Bielawski, (c) 2015 <andi (dot) b (at) outlook (dot) de>
-- DO NOT USE WITHOUT PERMISSION

do

local BASE_URL = 'https://api.twitch.tv'

function get_twitch_data (twitch_name)
  local url = BASE_URL..'/kraken/channels/'..twitch_name
  local res,code  = https.request(url)
  if code ~= 200 then return "HTTP-FEHLER" end
  local data = json:decode(res)
  return data
end

function send_twitch_data(data, receiver)
  local display_name = data.display_name
  local name = data.name
  if data.game == nil then
    game = 'nichts'
  else
    game = data.game
  end
  local status = data.status
  local views = comma_value(data.views)
  local followers = comma_value(data.followers)
  local text = display_name..' ('..name..') streamt '..game..'\n'..status..'\n'..views..' Zuschauer insgesamt und '..followers..' Follower'
  send_msg(receiver, text, ok_cb, false)
end

function run(msg, matches)
  local twitch_name = matches[1]
  local data = get_twitch_data(twitch_name)
  local receiver = get_receiver(msg)
  send_twitch_data(data, receiver)
end

return {
  description = "Sendet Twitch-Info.", 
  usage = {"Twitch.tv URL"},
  patterns = {"twitch.tv/([A-Za-z0-9-_-]+)"},
  run = run 
}

end
