Advertisement
Guest User

VLC /r/hockey script

a guest
Nov 22nd, 2013
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.16 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 get_timezone()
  58.     local time = os.time()
  59.     local utcdate   = os.date("!*t", time)
  60.     local localdate = os.date("*t", time)
  61.     localdate.isdst = false -- this is the trick
  62.     local time_diff = os.difftime(os.time(localdate), os.time(utcdate))
  63.     local h, m = math.modf(time_diff / 3600)
  64.     return string.format("%+.4d", 100 * h + 60 * m)
  65. end
  66.  
  67. local function convert_to_local(date, diff, tz)
  68.     local time = os.time(get_date_parts(date))
  69.     vlc.msg.dbg(time)
  70.     if (diff == nil) then
  71.         return os.date("%H:%M ET", time)
  72.     end
  73.     local local_time = time + diff;
  74.     return os.date("%H:%M", local_time)
  75. end
  76. function main()
  77.     vlc.msg.dbg("main")
  78.     local games = simplexml.parse_url("http://breadwinka.com/get_games.php?client=nhl&playerclient=hop")
  79.     local et_diff = get_et_diff()
  80.     local timezone = get_timezone()
  81.     local test_games = {}
  82.     for _, game in ipairs( games.children ) do              
  83.         if(game.name == "game") then          
  84.             simplexml.add_name_maps( game )
  85.             local game_date, home_team, away_team, title = getInfoForGame(game, et_diff, timezone)
  86.             if string.find(home_team,"^T%d+$") or string.find (away_team,"^T%d+$") then
  87.                 table.insert(test_games, game)
  88.             else
  89.                 local node = vlc.sd.add_node( { path = "", title = title } )
  90.                 addNodeForGame(node, game, home_team, away_team)
  91.             end
  92.         end
  93.     end
  94.  
  95.     if table.getn(test_games) > 0 then
  96.         local test_node = vlc.sd.add_node( { path = "", title = "Test Streams" } )
  97.         for _, game in ipairs(test_games) do
  98.             local game_date, home_team, away_team, title = getInfoForGame(game, et_diff, timezone)
  99.             node = test_node:add_subnode({ title = title })
  100.             addNodeForGame(node, game, home_team, away_team)
  101.         end
  102.     end
  103. end
  104.  
  105. function getInfoForGame(game, et_diff, timezone)
  106.     local game_date = convert_to_local(game.attributes["game_date"], et_diff, timezone)
  107.     local home_team = full_name(game.children_map['home_team'][1].children[1])
  108.     local away_team = full_name(game.children_map['away_team'][1].children[1])
  109.     local title = game_date .. " - " .. away_team .. " @ " .. home_team
  110.     return game_date, home_team, away_team, title
  111. end
  112.  
  113. function addNodeForGame(parentNode, game, home_team, away_team)
  114.     local quality = {400, 800, 1200, 1600, 2400, 3000, 4500}
  115.     for _, ass in ipairs(game.children_map['assignments'][1].children) do
  116.         local feed = ass.attributes["feed_display_name"]
  117.         local feed_title = home_team
  118.         if(feed == "away") then
  119.             feed_title = away_team
  120.         end
  121.         local feed_node = parentNode:add_subnode({ title = feed_title })
  122.         local ipad = ass.children_map['ipad_url'][1].children[1]
  123.         for _, q in ipairs(quality) do
  124.             local url = string.gsub(ipad, "ipad", q)
  125.             feed_node:add_subitem({
  126.                 path = url,
  127.                 title = q .. ' kbps',
  128.                 options = {
  129.                 "http-user-agent=AppleCoreMedia/1.0.0.8C148 (iPad; U; CPU OS 4_2_1 like Mac OS X; en_us)"
  130.             }
  131.             })
  132.         end
  133.     end
  134. end
  135.  
  136. function full_name(abr)
  137.     local all_names = {
  138.         BOS = "Boston Bruins",
  139.         BUF = "Buffalo Sabres",
  140.         CGY = "Calgary Flames",
  141.         CHI = "Chicago Blackhawks",
  142.         DET = "Detroit Red Wings",
  143.         EDM = "Edmonton Oilers",
  144.         CAR = "Carolina Hurricanes",
  145.         LAK = "Los Angeles Kings",
  146.         MTL = "Montreal Canadiens",
  147.         DAL = "Dallas Stars",
  148.         NJD = "New Jersey Devils",
  149.         NYI = "New York Islanders",
  150.         NYR = "New York Rangers",
  151.         PHI = "Philadelphia Flyers",
  152.         PIT = "Pittsburgh Penguins",
  153.         COL = "Colorado Avalanche",
  154.         STL = "St. Louis Blues",
  155.         TOR = "Toronto Maple Leafs",
  156.         VAN = "Vancouver Canucks",
  157.         WSH = "Washington Capitals",
  158.         PHX = "Phoenix Coyotes",
  159.         SJS = "San Jose Sharks",
  160.         OTT = "Ottawa Senators",
  161.         TBL = "Tampa Bay Lightning",
  162.         ANA = "Anaheim Ducks",
  163.         FLA = "Florida Panthers",
  164.         CBJ = "Columbus Blue Jackets",
  165.         MIN = "Minnesota Wild",
  166.         NSH = "Nashville Predators",
  167.         WPG = "Winnipeg Jets"
  168.     }
  169.     local name = all_names[abr]
  170.     if name == nil then
  171.         name = abr
  172.     end
  173.     return(name)
  174. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement