Skip to content

Instantly share code, notes, and snippets.

@adalessa
Last active November 14, 2022 22:05
Show Gist options
  • Save adalessa/d126475c56ceb315eb50198fb3b2dd34 to your computer and use it in GitHub Desktop.
Save adalessa/d126475c56ceb315eb50198fb3b2dd34 to your computer and use it in GitHub Desktop.
Script de lua para tener los resultados en vivo de los partidos del mundial dentro de neovim. Ideal para Lualine
local Job = require'plenary.job'
local qatar = {}
qatar.matches = {}
local run = false
local function get_data(type)
local url = string.format("https://world-cup-json-2022.fly.dev/matches/%s", type)
local data = ""
Job:new({
command = 'curl',
args = { url },
on_exit = function(j, return_val)
print(return_val)
data = j:result()[1]
end,
}):sync()
return vim.fn.json_decode(data)
end
Qatar_watch = function (type)
if not run then
return
end
vim.defer_fn(function ()
qatar.matches = get_data(type)
Qatar_watch(type)
end, 60 * 1000)
end
local function get_goals(goals)
if goals == vim.NIL or goals == nil then
return 0
end
return goals
end
qatar.current_matches = function()
local matches_str = {}
for _, match in ipairs(qatar.matches) do
table.insert(
matches_str,
string.format(
"[%s] %d - [%s] %d",
match.home_team.country,
get_goals(match.home_team.goals),
match.away_team.country,
get_goals(match.away_team.goals)
)
)
end
return vim.fn.join(matches_str, " || ")
end
qatar.start_watch = function (type)
type = type or "current"
if type ~= "current" and type ~= "today" then
return "can not start invalid type"
end
run = true
qatar.matches = get_data(type)
Qatar_watch(type)
end
qatar.stop_watch = function ()
run = false
end
return qatar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment