Advertisement
Guest User

VLC /r/hockey Script (12HourClock)

a guest
Nov 29th, 2013
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.75 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. require "simplexml"
  13.  
  14. API_USERNAME="rhockeyvlc"
  15.  
  16. function descriptor()
  17.     return { title="/r/hockey" }
  18. end
  19.  
  20. local function get_date_parts(date_str)
  21.   i1,i2,y,m,d,h,M,t=string.find(date_str, "(%d+)-(%d+)-(%d+) (%d+):(%d+)(.*)")
  22.   vlc.msg.dbg(date_str)
  23.   vlc.msg.dbg(i1 .. ", " .. i2)
  24.   h = tonumber(h)
  25.   if (t==" pm") then
  26.     h = h + 12
  27.   end
  28.   return {year=tonumber(y),month=tonumber(m),day=tonumber(d),hour=h,min=tonumber(M),sec=0}
  29. end
  30.  
  31. local function get_et_date()
  32.     local et_time_xml = simplexml.parse_url("http://api.geonames.org/timezone?lat=40.67&lng=-73.94&username=" .. API_USERNAME)
  33.     local et_date = nil
  34.     for _, child in ipairs( et_time_xml.children ) do
  35.         if (child.name == "timezone") then
  36.             for _, subchild in ipairs( child.children ) do
  37.                 if (subchild.name == "time") then
  38.                     et_date = subchild.children[1] .. ":00"
  39.                 end
  40.             end
  41.         end
  42.     end
  43.     return et_date
  44. end
  45.  
  46. local function get_et_diff()
  47.     local status, et_date = pcall(get_et_date)
  48.     if (status == false or et_date == nil) then
  49.         vlc.msg.warn("Couldn't get ET time, showing default times: " .. et_date)
  50.         return nil
  51.     end
  52.     local local_time = os.time()
  53.     local et_time = os.time(get_date_parts(et_date))
  54.     return os.difftime(local_time, et_time)
  55. end
  56.  
  57. local function convert_to_local(date, diff)
  58.     local time = os.time(get_date_parts(date))
  59.     vlc.msg.dbg(time)
  60.     if (diff == nil) then
  61.         return string.upper(os.date("%I:%M %p ET", time))
  62.     end
  63.     local local_time = time + diff;
  64.     return string.upper(os.date("%I:%M %p", local_time))
  65. end
  66. function main()
  67.     vlc.msg.dbg("main")
  68.     local games = simplexml.parse_url("http://breadwinka.com/get_games.php?client=nhl&playerclient=hop")
  69.     local et_diff = get_et_diff()
  70.     local test_games = {}
  71.     for _, game in ipairs( games.children ) do              
  72.         if(game.name == "game") then          
  73.             simplexml.add_name_maps( game )
  74.             local game_date, home_team, away_team, title = getInfoForGame(game, et_diff)
  75.             if string.find(home_team,"^T%d+$") or string.find (away_team,"^T%d+$") then
  76.                 table.insert(test_games, game)
  77.             else
  78.                 local node = vlc.sd.add_node( { path = "", title = title } )
  79.                 addNodeForGame(node, game, home_team, away_team)
  80.             end
  81.         end
  82.     end
  83.  
  84.     if table.getn(test_games) > 0 then
  85.         local test_node = vlc.sd.add_node( { path = "", title = "Test Streams" } )
  86.         for _, game in ipairs(test_games) do
  87.             local game_date, home_team, away_team, title = getInfoForGame(game, et_diff)
  88.             node = test_node:add_subnode({ title = title })
  89.             addNodeForGame(node, game, home_team, away_team)
  90.         end
  91.     end
  92. end
  93.  
  94. function getInfoForGame(game, et_diff)
  95.     local game_date = convert_to_local(game.attributes["game_date"], et_diff)
  96.     local home_team = full_name(game.children_map['home_team'][1].children[1])
  97.     local away_team = full_name(game.children_map['away_team'][1].children[1])
  98.     local title = game_date .. " - " .. away_team .. " @ " .. home_team
  99.     return game_date, home_team, away_team, title
  100. end
  101.  
  102. function addNodeForGame(parentNode, game, home_team, away_team)
  103.     local quality = {400, 800, 1200, 1600, 2400, 3000, 4500}
  104.     for _, ass in ipairs(game.children_map['assignments'][1].children) do
  105.         local feed = ass.attributes["feed_display_name"]
  106.         local feed_title = home_team
  107.         if(feed == "away") then
  108.             feed_title = away_team
  109.         end
  110.         local feed_node = parentNode:add_subnode({ title = feed_title })
  111.         local ipad = ass.children_map['ipad_url'][1].children[1]
  112.         for _, q in ipairs(quality) do
  113.             local url = string.gsub(ipad, "ipad", q)
  114.             feed_node:add_subitem({
  115.                 path = url,
  116.                 title = q .. ' kbps',
  117.                 options = {
  118.                 "http-user-agent=AppleCoreMedia/1.0.0.8C148 (iPad; U; CPU OS 4_2_1 like Mac OS X; en_us)"
  119.             }
  120.             })
  121.         end
  122.     end
  123. end
  124.  
  125. function full_name(abr)
  126.     local all_names = {
  127.         BOS = "Boston Bruins",
  128.         BUF = "Buffalo Sabres",
  129.         CGY = "Calgary Flames",
  130.         CHI = "Chicago Blackhawks",
  131.         DET = "Detroit Red Wings",
  132.         EDM = "Edmonton Oilers",
  133.         CAR = "Carolina Hurricanes",
  134.         LAK = "Los Angeles Kings",
  135.         MTL = "Montreal Canadiens",
  136.         DAL = "Dallas Stars",
  137.         NJD = "New Jersey Devils",
  138.         NYI = "New York Islanders",
  139.         NYR = "New York Rangers",
  140.         PHI = "Philadelphia Flyers",
  141.         PIT = "Pittsburgh Penguins",
  142.         COL = "Colorado Avalanche",
  143.         STL = "St. Louis Blues",
  144.         TOR = "Toronto Maple Leafs",
  145.         VAN = "Vancouver Canucks",
  146.         WSH = "Washington Capitals",
  147.         PHX = "Phoenix Coyotes",
  148.         SJS = "San Jose Sharks",
  149.         OTT = "Ottawa Senators",
  150.         TBL = "Tampa Bay Lightning",
  151.         ANA = "Anaheim Ducks",
  152.         FLA = "Florida Panthers",
  153.         CBJ = "Columbus Blue Jackets",
  154.         MIN = "Minnesota Wild",
  155.         NSH = "Nashville Predators",
  156.         WPG = "Winnipeg Jets"
  157.     }
  158.     local name = all_names[abr]
  159.     if name == nil then
  160.         name = abr
  161.     end
  162.     return(name)
  163. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement