1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-14 20:54:50 +00:00
opus/sys/apis/git.lua
kepler155c@gmail.com c478781cba package management
2018-11-03 18:13:41 -04:00

62 lines
1.3 KiB
Lua

local json = require('json')
local Util = require('util')
local TREE_URL = 'https://api.github.com/repos/%s/%s/git/trees/%s?recursive=1'
local FILE_URL = 'https://raw.githubusercontent.com/%s/%s/%s/%s'
local git = { }
local os = _G.os
if not _G.GIT then
_G.GIT = { }
end
function git.list(repository, cache)
local t = Util.split(repository, '(.-)/')
local user = t[1]
local repo = t[2]
local branch = t[3] or 'master'
local dataUrl = string.format(TREE_URL, user, repo, branch)
if _G.GIT[dataUrl] then
_G.GIT[dataUrl].count = _G.GIT[dataUrl].count + 1
else
_G.GIT[dataUrl] = {
count = 1
}
end
local contents = Util.download(dataUrl)
if not contents then
error('Invalid repository')
end
local data = json.decode(contents)
Util.writeTable('.git/' .. dataUrl, { day = os.day(), data = data })
if data.message and data.message:find("API rate limit exceeded") then
error("Out of API calls, try again later")
end
if data.message and data.message == "Not found" then
error("Invalid repository")
end
local list = { }
for _,v in pairs(data.tree) do
if v.type == "blob" then
v.path = v.path:gsub("%s","%%20")
list[v.path] = {
url = string.format(FILE_URL, user, repo, branch, v.path),
size = v.size,
}
end
end
return list
end
return git