Advertisement
Guest User

Untitled

a guest
Oct 11th, 2014
12,798
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.27 KB | None | 0 0
  1. --[[
  2.   DISCLAIMER: SCRIPT IS PROVIDED AS IS USE AT YOUR OWN RISK!
  3.  
  4.   Save this script as "hockey.lua"
  5.   Place this script in:
  6.     - Windows (all users):   %ProgramFiles%\VideoLAN\VLC\lua\sd\
  7.     - Windows (current user):   %APPDATA%\VLC\lua\sd\
  8.     - Linux (all users):     /usr/share/vlc/lua/sd/
  9.     - Linux (current user):  ~/.local/share/vlc/lua/sd/
  10.     - Mac OS X (all users):  VLC.app/Contents/MacOS/share/lua/sd/
  11.     --]]
  12.  
  13. SCOREBOARD_URL = 'http://live.nhl.com/GameData/Scoreboard.json'
  14. FEED_SOURCE_URL = 'http://smb.cdnak.neulion.com/fs/nhl/mobile/feed_new/data/streams/%s/ipad/%s_%s.json'
  15.  
  16. MILITARY_TIME=true
  17. SHOW_LOCAL_TIME=true
  18.  
  19. SCRIPT_NAME="/r/hockey"
  20. API_USERNAME="rhockeyvlc"
  21. USER_AGENT="PS4Application libhttp/1.000 (PS4) CoreMedia libhttp/1.76 (PlayStation 4)"
  22. --Alternative User-Agents:
  23. -- USER_AGENT="Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0; Xbox; Xbox One)"
  24. -- USER_AGENT="iTunes-AppleTV/4.1"
  25.  
  26. json = nil
  27. function lazy_load()
  28.     if lazy_loaded then return nil end
  29.     json = require "dkjson"
  30.     json["parse_url"] = function(url)
  31.         local string = ""
  32.         local line = ""
  33.         local stream = vlc.stream(url)
  34.         repeat
  35.             line = stream:readline()
  36.             string = string..line
  37.         until line ~= nil
  38.         return json.decode(string)
  39.     end
  40.     lazy_loaded = true
  41. end
  42.  
  43. function log(msg)
  44.     vlc.msg.info("[" .. SCRIPT_NAME .. "] " .. msg)
  45. end
  46.  
  47. function descriptor()
  48.     return { title=SCRIPT_NAME }
  49. end
  50.  
  51. local function get_date_parts(date_str)
  52.   _,_,y,m,d,h,M,s=string.find(date_str, "(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)")
  53.   return {year=tonumber(y),month=tonumber(m),day=tonumber(d),hour=tonumber(h),min=tonumber(M),sec=tonumber(s)}
  54. end
  55.  
  56.  
  57. local function get_et_diff()
  58.     if not SHOW_LOCAL_TIME then
  59.         return nil
  60.     end
  61.  
  62.     local status, et_date = pcall(get_et_date)
  63.     if (status == false or et_date == nil) then
  64.         vlc.msg.warn("Couldn't get ET time, showing default times: " .. et_date)
  65.         return nil
  66.     end
  67.     local local_time = os.time()
  68.     local et_time = os.time(get_date_parts(et_date))
  69.     local diff_seconds = os.difftime(local_time, et_time)
  70.  
  71.     -- Round to closest 5mins
  72.     local excess = diff_seconds % 300
  73.     if (excess < 150) then
  74.         diff_seconds = diff_seconds - excess
  75.     else
  76.         diff_seconds = diff_seconds + (300 - excess)
  77.     end
  78.     return diff_seconds
  79. end
  80.  
  81. local function convert_to_local(datetime, diff)
  82.     local time, local_time, local_date
  83.  
  84.     if diff == nil then
  85.         diff = 0
  86.     end
  87.  
  88.     time = os.time(get_date_parts(datetime))
  89.     adjusted_time = time + diff;
  90.     local_time = os.date(time_display_format, adjusted_time)
  91.     local_date = os.date("%Y/%m/%d", adjusted_time)
  92.  
  93.     -- Strip leading zero from 12 hour format
  94.     if not MILITARY_TIME then
  95.         local_time = local_time:gsub("^0", "")
  96.     end
  97.     return local_time, local_date
  98. end
  99.  
  100. local function set_time_display_format(diff)
  101.     if MILITARY_TIME then
  102.         time_display_format = "%H:%M"
  103.     else
  104.         time_display_format = "%I:%M %p"
  105.     end
  106.     if (diff == nil) then
  107.         time_display_format = time_display_format .. " ET"
  108.     end
  109. end
  110.  
  111. local function convert_game_time_string_to_date(game_time)
  112.  
  113.     _,_,m,d,y,h,M,s=string.find(game_time, "(%d+)/(%d+)/(%d+) (%d+):(%d+):(%d+)")
  114.  
  115.     return string.format("%d-%d-%d %d:%d:%d", y, m, d, h, M, 0)
  116. end
  117.  
  118. local function get_feed_date()
  119.     -- Calculate date for -10:00
  120.     local timestamp = os.time()
  121.     -- ! gives GMT time
  122.     local format = "%Y/%m/%d"
  123.     -- Offset causes date to only switch over at 10am GMT
  124.     -- which is 5am ET
  125.     local tzoffset = -36000
  126.     return os.date(format, timestamp + tzoffset)
  127. end
  128.  
  129.  
  130. function main()
  131.     lazy_load()
  132.     log("main")
  133.     local et_diff = get_et_diff()
  134.     set_time_display_format(et_diff)
  135.  
  136.     local todays_date = get_feed_date()
  137.     local todays_games = {}
  138.     local scoreboard = json.parse_url(SCOREBOARD_URL)
  139.     for _, game in ipairs( scoreboard["games"] ) do
  140.         local game_id, game_time, game_date, home_team, away_team, title = getInfoForGame(game, et_diff)
  141.         if(game_date == todays_date) then
  142.             table.insert(todays_games, game)
  143.         end
  144.     end
  145.  
  146.  
  147.     if #(todays_games) == 0 then
  148.         vlc.sd.add_node({path="", title="No games today."})
  149.         return
  150.     end
  151.  
  152.     for _, game in ipairs( todays_games ) do
  153.         add_node_for_game(game)
  154.     end
  155. end
  156.  
  157. function getInfoForGame(game, et_diff)
  158.  
  159.     local game_id = game["id"]
  160.     local game_date = convert_game_time_string_to_date(""..game["longStartTime"])
  161.     local local_game_time, local_game_date = convert_to_local(game_date, et_diff)
  162.     local home_team = full_name(""..game["homeTeamName"])
  163.     local away_team = full_name(""..game["awayTeamName"])
  164.     local title = game_id .. " " .. local_game_time .. " - " .. away_team .. " @ " .. home_team
  165.  
  166.     return game_id, local_game_time, local_game_date, home_team, away_team, title
  167. end
  168.  
  169. function add_node_for_game_team_type(parentNode, node, prefix)
  170.     local quality = {400, 800, 1200, 1600, 3000, 4500, 5000}
  171.  
  172.     if (node ~= nil) then
  173.         for _, q in ipairs(quality) do
  174.             local url = string.gsub(node, "ipad", q)
  175.             parentNode:add_subitem({
  176.                 path = url,
  177.                 title = prefix .. ' - ' .. q .. ' kbps ',
  178.                 options = {
  179.                     "http-user-agent=" .. USER_AGENT
  180.                 }
  181.             })
  182.         end
  183.     end
  184. end
  185.  
  186. local function add_missing_feed_node(parent_node, game, game_state)
  187.     if game_state == 6 then
  188.         parent_node:add_subnode({title = "Game has finished. No replay or highlights available yet."})
  189.     else
  190.         parent_node:add_subnode({title = "No stream available yet."})
  191.     end
  192. end
  193.  
  194. local function add_node_for_game_team(parentNode, node, game_state)
  195.     local nodeAdded = false
  196.     if (node["live"] ~= nil) then
  197.         add_node_for_game_team_type(parentNode, node["live"]["bitrate0"], "Live")
  198.         nodeAdded = true
  199.     end
  200.  
  201.     if (node["vod-condensed"] ~= nil) then
  202.         add_node_for_game_team_type(parentNode, node["vod-condensed"]["bitrate0"], "Condensed VOD")
  203.         nodeAdded = true
  204.     end
  205.     if (node["vod-continuous"] ~= nil and node["vod-condensed"] ~= nil) then
  206.         local url = string.gsub(node["vod-condensed"]["bitrate0"], "condensed", "continuous")
  207.         add_node_for_game_team_type(parentNode, url, "Continuous VOD")
  208.         nodeAdded = true
  209.     end
  210.  
  211.     if(nodeAdded ~= true) then
  212.         add_missing_feed_node(parentNode, node, game_state)
  213.     end
  214.  
  215. end
  216.  
  217. function add_node_for_game(game)
  218.  
  219.     local game_id, game_time, game_date, home_team, away_team, title = getInfoForGame(game, et_diff)
  220.  
  221.     local parentNode = vlc.sd.add_node( { path = "", title = title } )
  222.     local home_feed_node = parentNode:add_subnode({ title = home_team })
  223.     local away_feed_node = parentNode:add_subnode({ title = away_team })
  224.  
  225.     local id_year = string.sub(game_id, 1, 4)
  226.     local id_season = string.sub(game_id, 5, 6)
  227.     local id_game = string.sub(game_id, 7, 10)
  228.  
  229.     local feed_url = string.format(FEED_SOURCE_URL, id_year, id_season, id_game)
  230.  
  231.     local streams = json.parse_url(feed_url)
  232.     if (streams ~= nil) then
  233.         local ipad = streams['gameStreams']['ipad']
  234.         local game_state = streams["gameState"]
  235.  
  236.         log(game_state .." ".. game_time .." ".. game_date .." ".. home_team .." ".. away_team .." (".. title .. ")")
  237.  
  238.         local home = ipad["home"]
  239.         local away = ipad["away"]
  240.  
  241.         if (home ~= nil) then
  242.             add_node_for_game_team(home_feed_node, home, game_state)
  243.         else
  244.             add_missing_feed_node(home_feed_node, game, game_state)
  245.         end
  246.         if (away ~= nil) then
  247.             add_node_for_game_team(away_feed_node, away, game_state)
  248.         else
  249.             add_missing_feed_node(away_feed_node, game, game_state)
  250.         end
  251.     else
  252.         add_missing_feed_node(home_feed_node, game, game_state)
  253.         add_missing_feed_node(away_feed_node, game, game_state)
  254.     end
  255.  
  256. end
  257.  
  258. function full_name(abr)
  259.     local all_names = {
  260.         BOS = "Boston Bruins",
  261.         BUF = "Buffalo Sabres",
  262.         CGY = "Calgary Flames",
  263.         CHI = "Chicago Blackhawks",
  264.         DET = "Detroit Red Wings",
  265.         EDM = "Edmonton Oilers",
  266.         CAR = "Carolina Hurricanes",
  267.         LAK = "Los Angeles Kings",
  268.         MTL = "Montreal Canadiens",
  269.         DAL = "Dallas Stars",
  270.         NJD = "New Jersey Devils",
  271.         NYI = "New York Islanders",
  272.         NYR = "New York Rangers",
  273.         PHI = "Philadelphia Flyers",
  274.         PIT = "Pittsburgh Penguins",
  275.         COL = "Colorado Avalanche",
  276.         STL = "St. Louis Blues",
  277.         TOR = "Toronto Maple Leafs",
  278.         VAN = "Vancouver Canucks",
  279.         WSH = "Washington Capitals",
  280.         ARI = "Arizona Coyotes",
  281.         SJS = "San Jose Sharks",
  282.         OTT = "Ottawa Senators",
  283.         TBL = "Tampa Bay Lightning",
  284.         ANA = "Anaheim Ducks",
  285.         FLA = "Florida Panthers",
  286.         CBJ = "Columbus Blue Jackets",
  287.         MIN = "Minnesota Wild",
  288.         NSH = "Nashville Predators",
  289.         WPG = "Winnipeg Jets"
  290.     }
  291.     local name = all_names[abr]
  292.     if name == nil then
  293.         name = abr
  294.     end
  295.     return(name)
  296. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement