minor improvements
This commit is contained in:
@ -1,6 +1,10 @@
|
||||
-- utilities.lua
|
||||
-- Functions shared among plugins.
|
||||
|
||||
HTTP = require('socket.http')
|
||||
HTTPS = require('ssl.https')
|
||||
JSON = require('dkjson')
|
||||
|
||||
-- get the indexed word in a string
|
||||
get_word = function(s, i)
|
||||
|
||||
@ -152,4 +156,42 @@ handle_exception = function(err, message)
|
||||
print(output)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
-- Okay, this one I actually did copy from yagop.
|
||||
-- https://github.com/yagop/telegram-bot/blob/master/bot/utils.lua
|
||||
download_file = function(url, filename)
|
||||
|
||||
local respbody = {}
|
||||
local options = {
|
||||
url = url,
|
||||
sink = ltn12.sink.table(respbody),
|
||||
redirect = true
|
||||
}
|
||||
|
||||
local response = nil
|
||||
|
||||
if url:match('^https') then
|
||||
options.redirect = false
|
||||
response = { HTTPS.request(options) }
|
||||
else
|
||||
response = { HTTP.request(options) }
|
||||
end
|
||||
|
||||
local code = response[2]
|
||||
local headers = response[3]
|
||||
local status = response[4]
|
||||
|
||||
if code ~= 200 then return false end
|
||||
|
||||
filename = filename or os.time()
|
||||
|
||||
local file_path = '/tmp/'..filename
|
||||
|
||||
file = io.open(file_path, 'w+')
|
||||
file:write(table.concat(respbody))
|
||||
file:close()
|
||||
|
||||
return file_path
|
||||
|
||||
end
|
||||
|
Reference in New Issue
Block a user