2017-02-18 22:41:23 +01:00
|
|
|
-- YOU NEED THE FOLLOWING FOLDERS IN YOUR SAVE PATH: photo, document, video, voice, audio, sticker
|
2016-07-17 13:22:27 +02:00
|
|
|
|
|
|
|
local media_download = {}
|
|
|
|
|
|
|
|
media_download.triggers = {
|
|
|
|
'/nil'
|
|
|
|
}
|
|
|
|
|
2016-08-16 15:02:31 +02:00
|
|
|
function media_download:download_to_file_permanently(url, save_dir, file_name)
|
2016-07-17 13:22:27 +02:00
|
|
|
local respbody = {}
|
|
|
|
local options = {
|
|
|
|
url = url,
|
|
|
|
sink = ltn12.sink.table(respbody),
|
|
|
|
redirect = false
|
|
|
|
}
|
|
|
|
local response = nil
|
2016-08-01 21:07:27 +02:00
|
|
|
response = {https.request(options)}
|
2016-07-17 13:22:27 +02:00
|
|
|
|
|
|
|
local code = response[2]
|
|
|
|
local headers = response[3]
|
|
|
|
local status = response[4]
|
|
|
|
|
|
|
|
if code ~= 200 then return false end
|
|
|
|
|
2016-08-16 15:02:31 +02:00
|
|
|
local file_path = save_dir..'/'..file_name
|
2017-02-15 18:40:14 +01:00
|
|
|
print(file_path)
|
2016-07-17 13:22:27 +02:00
|
|
|
file = io.open(file_path, "w+")
|
|
|
|
file:write(table.concat(respbody))
|
|
|
|
file:close()
|
|
|
|
print("Downloaded to: "..file_path)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2016-08-24 15:38:29 +02:00
|
|
|
function media_download:pre_process(msg, config)
|
2016-07-17 13:22:27 +02:00
|
|
|
if msg.photo then
|
|
|
|
local lv = #msg.photo -- find biggest photo, always the last value
|
|
|
|
file_id = msg.photo[lv].file_id
|
|
|
|
file_size = msg.photo[lv].file_size
|
|
|
|
elseif msg.video then
|
|
|
|
file_id = msg.video.file_id
|
|
|
|
file_size = msg.video.file_size
|
2017-06-13 22:38:27 +02:00
|
|
|
elseif msg.video_note then
|
|
|
|
file_id = msg.video_note.file_id
|
|
|
|
file_size = msg.video_note.file_size
|
2017-02-15 18:40:14 +01:00
|
|
|
elseif msg.sticker then
|
2016-07-17 13:22:27 +02:00
|
|
|
file_id = msg.sticker.file_id
|
2017-02-15 18:40:14 +01:00
|
|
|
file_size = msg.sticker.file_size
|
2016-07-17 13:22:27 +02:00
|
|
|
elseif msg.voice then
|
|
|
|
file_id = msg.voice.file_id
|
|
|
|
file_size = msg.voice.file_size
|
|
|
|
elseif msg.audio then
|
|
|
|
file_id = msg.audio.file_id
|
|
|
|
file_size = msg.audio.file_size
|
|
|
|
elseif msg.document then
|
|
|
|
file_id = msg.document.file_id
|
|
|
|
file_size = msg.document.file_size
|
|
|
|
else
|
2016-08-16 15:02:31 +02:00
|
|
|
return msg
|
2016-07-17 13:22:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if file_size > 19922944 then
|
2016-08-01 21:07:27 +02:00
|
|
|
print('Datei ist größer als 20 MB - Nicht downloadbar!')
|
2016-08-16 15:05:12 +02:00
|
|
|
return msg
|
2016-06-16 20:56:37 +02:00
|
|
|
end
|
|
|
|
|
2016-08-16 15:02:31 +02:00
|
|
|
local save_dir = config.getfile_path
|
|
|
|
if not save_dir then
|
2016-08-16 15:05:12 +02:00
|
|
|
print('getfile_path wurde nicht in der Config gesetzt, breche ab...')
|
2016-08-16 15:02:31 +02:00
|
|
|
return msg
|
2016-07-17 13:22:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Check if file has already been downloaded
|
|
|
|
local already_downloaded = redis:sismember('telegram:file_id', file_id)
|
|
|
|
if already_downloaded == true then
|
2016-08-01 21:07:27 +02:00
|
|
|
print('Datei wurde bereits gedownloadet')
|
2016-08-16 15:02:31 +02:00
|
|
|
return msg
|
2016-07-17 13:22:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Saving file to the Telegram Cloud
|
2016-08-24 15:38:29 +02:00
|
|
|
local request = bindings.request('getFile', {
|
2016-07-17 13:22:27 +02:00
|
|
|
file_id = file_id
|
|
|
|
} )
|
|
|
|
|
|
|
|
-- Getting file from the Telegram Cloud
|
|
|
|
if not request then
|
2016-08-01 21:07:27 +02:00
|
|
|
print('Download fehlgeschlagen!')
|
2016-08-16 15:02:31 +02:00
|
|
|
return msg
|
2016-07-17 13:22:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Use original filename for documents
|
|
|
|
if msg.document then
|
|
|
|
file_path = 'document/'..file_id..'-'..msg.document.file_name -- to not overwrite a file
|
|
|
|
else
|
|
|
|
file_path = request.result.file_path
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Construct what we want
|
2016-08-15 23:14:28 +02:00
|
|
|
local download_url = 'https://api.telegram.org/file/bot'..config.bot_api_key..'/'..request.result.file_path
|
2016-08-16 15:02:31 +02:00
|
|
|
|
|
|
|
local ok = media_download:download_to_file_permanently(download_url, save_dir, file_path)
|
2016-07-17 13:22:27 +02:00
|
|
|
if not ok then
|
2016-08-01 21:07:27 +02:00
|
|
|
print('Download fehlgeschlagen!')
|
2016-08-16 15:02:31 +02:00
|
|
|
return msg
|
2016-07-17 13:22:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Save file_id to redis to prevent downloading the same file over and over when forwarding
|
|
|
|
redis:sadd('telegram:file_id', file_id)
|
|
|
|
return msg
|
|
|
|
end
|
|
|
|
|
|
|
|
function media_download:action(msg)
|
|
|
|
end
|
|
|
|
|
2016-08-03 19:05:05 +02:00
|
|
|
return media_download
|