Functions for downloading and removing images async. The magic of cackbacks 🌟

This commit is contained in:
yago 2015-02-15 14:06:01 +01:00
parent 8ee6e8fd4f
commit 3df9d9c3f3
3 changed files with 99 additions and 40 deletions

View File

@ -1,3 +1,5 @@
local ltn12 = require "ltn12"
function get_receiver(msg) function get_receiver(msg)
if msg.to.type == 'user' then if msg.to.type == 'user' then
return 'user#id'..msg.from.id return 'user#id'..msg.from.id
@ -15,7 +17,6 @@ function is_chat_msg( msg )
end end
function string.random(length) function string.random(length)
math.randomseed(os.time())
local str = ""; local str = "";
for i = 1, length do for i = 1, length do
math.random(97, 122) math.random(97, 122)
@ -36,46 +37,31 @@ function string.trim(s)
return s:gsub("^%s*(.-)%s*$", "%1") return s:gsub("^%s*(.-)%s*$", "%1")
end end
function download_to_file( url , noremove ) -- Saves file to /tmp/. If file_name isn't provided,
-- will get the text after the last "/" for filename.
function download_to_file(url, file_name)
print("url to download: "..url) print("url to download: "..url)
local ltn12 = require "ltn12"
local respbody = {}
one, c, h = http.request{url=url, sink=ltn12.sink.table(respbody), redirect=true}
htype = h["content-type"]
if htype == "image/jpeg" then local respbody = {}
file_name = string.random(5)..".jpg" local options = {
file_path = "/tmp/"..file_name url = url,
else sink = ltn12.sink.table(respbody),
if htype == "image/gif" then redirect = true
file_name = string.random(5)..".gif" }
file_path = "/tmp/"..file_name
else local one, c, h = http.request(options)
if htype == "image/png" then
file_name = string.random(5)..".png" -- Everything after the last /
file_path = "/tmp/"..file_name local file_name = url:match("([^/]+)$")
else local file_path = "/tmp/"..file_name
file_name = url:match("([^/]+)$")
file_path = "/tmp/"..file_name
end
end
end
file = io.open(file_path, "w+") file = io.open(file_path, "w+")
file:write(table.concat(respbody)) file:write(table.concat(respbody))
file:close() file:close()
if noremove == nil then
print(file_path.."will be removed in 20 seconds")
postpone(rmtmp_cb, file_path, 20)
end
return file_path return file_path
end end
-- Callback to remove a file
function rmtmp_cb(file_path, success, result)
os.remove(file_path)
end
function vardump(value, depth, key) function vardump(value, depth, key)
local linePrefix = "" local linePrefix = ""
@ -191,7 +177,84 @@ function string:isempty()
return self == nil or self == '' return self == nil or self == ''
end end
-- Returns true if String starts with Start
function string.starts(String, Start) function string.starts(String, Start)
return Start == string.sub(String,1,string.len(Start)) return Start == string.sub(String,1,string.len(Start))
end end
-- Send image to user and delete it when finished.
-- cb_function and cb_extra are optionals callback
function _send_photo(receiver, file_path, cb_function, cb_extra)
local cb_extra = {
file_path = file_path,
cb_function = cb_function,
cb_extra = cb_extra
}
-- Call to remove with optional callback
send_photo(receiver, file_path, rmtmp_cb, cb_extra)
end
-- Download the image and send to receiver, it will be deleted.
function send_photo_from_url(receiver, url)
local file_path = download_to_file(url, false)
print("File path: "..file_path)
_send_photo(receiver, file_path)
end
-- Send multimple images asynchronous.
-- param urls must be a table.
function send_photos_from_url(receiver, urls)
local cb_extra = {
receiver = receiver,
urls = urls,
remove_path = nil
}
send_photos_from_url_callback(cb_extra)
end
-- Use send_photos_from_url.
-- This fuction might be difficult to understand.
function send_photos_from_url_callback(cb_extra, success, result)
-- cb_extra is a table containing receiver, urls and remove_path
local receiver = cb_extra.receiver
local urls = cb_extra.urls
local remove_path = cb_extra.remove_path
-- The previously image to remove
if remove_path ~= nil then
os.remove(remove_path)
print("Deleted: "..remove_path)
end
-- Nil or empty, exit case (no more urls)
if urls == nil or #urls == 0 then
return false
end
-- Take the head and remove from urls table
local head = table.remove(urls, 1)
local file_path = download_to_file(head, false)
local cb_extra = {
receiver = receiver,
urls = urls,
remove_path = file_path
}
-- Send first and postpone the others as callback
send_photo(receiver, file_path, send_photos_from_url_callback, cb_extra)
end
-- Callback to remove a file
function rmtmp_cb(cb_extra, success, result)
local file_path = cb_extra.file_path
local cb_function = cb_extra.cb_function
local cb_extra = cb_extra.cb_extra
if file_path ~= nil then
os.remove(file_path)
print("Deleted: "..file_path)
end
-- Finaly call the callback
cb_function(cb_extra, success, result)
end

View File

@ -22,7 +22,7 @@ function run(msg, matches)
local receiver = get_receiver(msg) local receiver = get_receiver(msg)
url, title = get_9GAG() url, title = get_9GAG()
file_path = download_to_file(url) file_path = download_to_file(url)
send_photo(receiver, file_path, send_title, {receiver, title}) _send_photo(receiver, file_path, send_title, {receiver, title})
return false return false
end end

View File

@ -59,15 +59,11 @@ function run(msg, matches)
-- send the parts -- send the parts
local receiver = get_receiver(msg) local receiver = get_receiver(msg)
send_msg(receiver, header .. "\n" .. text, ok_cb, false) send_msg(receiver, header .. "\n" .. text, ok_cb, false)
for k, v in pairs(images) do send_photos_from_url(receiver, images)
local file = download_to_file(v)
send_photo(receiver, file, ok_cb, false)
delay_s(1)
end
return nil return nil
end end
return { return {
description = "When user sends twitter URL, send text and images to origin. Requieres OAuth Key.", description = "When user sends twitter URL, send text and images to origin. Requieres OAuth Key.",
usage = "", usage = "",