Skip to content

Instantly share code, notes, and snippets.

Created July 21, 2014 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/60058db98c5a010cb0f3 to your computer and use it in GitHub Desktop.
Save anonymous/60058db98c5a010cb0f3 to your computer and use it in GitHub Desktop.
Gists Codea Upload
--# Main
-- Gist uploader/downloader
local function _setup()
assert(loadstring(readGlobalData('Dkjson')))() -- load json
status, msg = "", ""
textWrapWidth(WIDTH*.9)
fontSize(35)
parameter.action("Paste gist id", function()
parameter.clear()
status, msg = "", pasteboard.text
parameter.action("Download", function()
gist_get(pasteboard.text, function(data)
status, msg = "", "Downloaded. Wait..."
for filename,file in pairs(data.files) do
saveProjectTab((filename:gsub('%..*', '')):gsub('Main', 'Main2'), file.content)
end
status, msg = "Success", ""
end)
end)
end)
parameter.action("Upload new gist", function()
parameter.clear()
status, msg = "", "Starting upload..."
gist_create({ ["Main.lua"] = { content = pasteboard.text } }, function(data)
pasteboard.copy(data.html_url)
saveProjectTab("myGists", readProjectTab("myGists") .. "-- " .. data.html_url .. " -- project: \n")
status, msg = "Success", " :\n\n" .. data.html_url .. "\n\n" .. "Link copied in the pasteboard and in tab myGists"
end)
end)
end
function draw()
background(18)
fill(status == "Success" and color(96, 181, 47, 255) or (status == "Error" and color(177, 49, 49, 255) or color(203, 209, 60, 255)))
text(status .. msg, WIDTH*.5, HEIGHT*.5)
end
function setup()
if readGlobalData('Dkjson') then
_setup()
else -- first load
saveProjectTab("myGists", "--# myGists\n")
status, msg = "", "Loading dkjson..."
http.request("https://gist.githubusercontent.com/HyroVitalyProtago/5965767/raw/73facb82eda4c92393c51535f8dd08728e25555d/Dkjson.lua",
function(data)
saveGlobalData('Dkjson', data)
_setup()
end)
end
end
-- GithubAPI v3 - tiny port for Gists utility by @HyroVitalyProtago
local function request(url, callback, opts)
local quit = function() parameter.action("Quit", function() close() end) end
http.request("https://api.github.com/" .. url, function(data, status, headers)
callback(json.decode(data), status, headers)
quit()
end, function (e)
status, msg = "Error", " : " .. e
quit()
end, opts)
end
function gist_get(id, callback)
request("gists/"..id, callback)
end
function gist_create(files, callback)
request("gists", callback, {
method = "POST",
data = json.encode({
files = files,
public = true,
description = 'Gists Codea Upload'
})
})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment