Skip to content

Instantly share code, notes, and snippets.

@tomoe-mami
Created September 28, 2012 01:43
Show Gist options
  • Save tomoe-mami/3797515 to your computer and use it in GitHub Desktop.
Save tomoe-mami/3797515 to your computer and use it in GitHub Desktop.
Yet another imgur uploader
#!/usr/bin/env lua
-- Usage: lemur.lua [filename]
-- If filename is not specified, will output credit information
local os = require "os"
local io = require "io"
local string = require "string"
local table = require "table"
local socket = require "socket"
local ltn12 = require "ltn12"
local http = require "socket.http"
local url = require "socket.url"
local json = require "cjson"
SCRIPT_VERSION = "0.1"
API_HOST = "api.imgur.com"
API_SCHEME = "http"
API_VERSION = 2
API_KEY = "GET_YOUR_OWN_API_KEY"
UA = "LEMURIMGURIMGUPLODR %s <https://github.com/rumia>"
function build_url(action, params)
if not params then params = {} end
if not params.key then params.key = API_KEY end
local queries = {}
for key, value in pairs(params) do
queries[#queries + 1] = string.format("%s=%s", url.escape(key), url.escape(value))
end
return url.build {
scheme = API_SCHEME,
host = API_HOST,
path = string.format("/%s/%s.json", API_VERSION, action),
query = table.concat(queries, "&")
}
end
function show_error(...)
if arg and #arg > 0 then
io.stderr:write("ERROR: " .. string.format(unpack(arg)) .. "\n")
end
end
function get_credit()
local body, status_code, headers, status_line = http.request(build_url("credits"))
if status_code ~= 200 then
show_error(status_line)
else
if body and #body > 0 then
local parsed = json.decode(body)
if parsed and type(parsed) == "table" then
if parsed.error then
show_error(parsed.error.message)
elseif parsed.credits and type(parsed.credits) == "table" then
print(string.format(
"Information for API key %s\n" ..
"\tAvailable credit: %d\n" ..
"\tAllocation limit: %d\n" ..
"\tNext reset: %s\n" ..
"\tRefresh time: %d secs",
API_KEY,
parsed.credits.remaining,
parsed.credits.limit,
os.date("!%Y-%m-%d %H:%M:%S %z", parsed.credits.reset),
parsed.credits.refresh_in_secs))
else
show_error("Unknown response: %s", body)
end
end
else
show_error("Empty response")
end
end
end
function upload(filename, title, caption)
local body = {}
local file_handle = assert(io.open(filename, "r"))
local size = file_handle:seek("end")
file_handle:seek("set")
local _, status_code, headers, status_line =
http.request({
method = "PUT",
url = build_url("upload", { title = title, caption = caption, name = filename }),
source = ltn12.source.file(file_handle),
sink = ltn12.sink.table(body),
headers = {
["content-length"] = size,
["user-agent"] = string.format(UA, SCRIPT_VERSION)
}
})
if status_code ~= 200 then
show_error(status_line)
else
if body and type(body) == "table" then
body = table.concat(body, "")
if #body > 0 then
local parsed = json.decode(body)
if parsed and type(parsed) == "table" then
if parsed.error then
show_error(parsed.error.message)
elseif parsed.upload then
print(string.format(
"%s (%ux%u %s)\n" ..
"\tPage: %s\n" ..
"\tOriginal Size: %s\n" ..
"\tSmall Thumbnail: %s\n" ..
"\tLarge Thumbnail: %s\n" ..
"\tDelete Page: %s",
parsed.upload.image.name,
parsed.upload.image.width,
parsed.upload.image.height,
parsed.upload.image.type,
parsed.upload.links.imgur_page,
parsed.upload.links.original,
parsed.upload.links.small_square,
parsed.upload.links.large_thumbnail,
parsed.upload.links.delete_page))
else
show_error("Unknown response: %s", body)
end
else
show_error("Unable to parse response: %s", body)
end
else
show_error("Empty response")
end
end
end
end
filename = select(1, ...)
if not filename then
get_credit()
else
upload(filename, select(2, ...), select(3, ...))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment