local args = { ... } -- Collect command line arguments so you can actually control ts
local configFile = fs.open("disk/config.json", "r") -- read from configuration file (holds user and pass)
local config = textutils.unserialiseJSON(configFile.readAll()) -- parse config to lua table
print(configFile.readAll())
configFile.close()
-- construct http POST body
local body = "template_id=" .. args[1] .. "&username=" .. textutils.urlEncode(config.username) .. "&password=" .. textutils.urlEncode(config.password)
body = body .. "&text0=" .. textutils.urlEncode(args[2]) .. "&text1=" .. textutils.urlEncode(args[3])
local response = http.post("https://api.imgflip.com/caption_image", body) -- send request
local tbl = textutils.unserialiseJSON(response.readAll()) -- parse to lua table
local serialised = textutils.serialise(tbl) -- reserialise to text for pretty print
print(serialised)
response.close()
local responseFile = fs.open("disk/out/caption_results.txt", "w") -- save the response to a file
responseFile.write(serialised)
responseFile.close()
-- save the generated image to files for extra credit on this school project (i'm joking, i made this for fun)
local imgURL = tbl.data.url
local imgID = string.sub(imgURL, -10)
print(imgURL)
local imgFile = fs.open("disk/out/" .. imgID, "w")
local image = http.get(imgURL)
imgFile.write(image.readAll())
image.close()
imgFile.close()