2016-06-19 19:25:24 +02:00
|
|
|
local gdrive = {}
|
|
|
|
|
|
|
|
function gdrive:init(config)
|
|
|
|
if not cred_data.google_apikey then
|
|
|
|
print('Missing config value: google_apikey.')
|
|
|
|
print('gdrive.lua will not be enabled.')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
gdrive.triggers = {
|
|
|
|
"docs.google.com/(.*)/d/([A-Za-z0-9-_-]+)",
|
|
|
|
"drive.google.com/(.*)/d/([A-Za-z0-9-_-]+)",
|
|
|
|
"drive.google.com/(open)%?id=([A-Za-z0-9-_-]+)"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
local apikey = cred_data.google_apikey
|
|
|
|
|
2016-07-20 00:49:31 +02:00
|
|
|
local BASE_URL = 'https://www.googleapis.com/drive/v3'
|
|
|
|
local apikey = cred_data.google_apikey
|
2016-06-19 19:25:24 +02:00
|
|
|
|
|
|
|
function gdrive:get_drive_document_data (docid)
|
2016-07-20 00:49:31 +02:00
|
|
|
local url = BASE_URL..'/files/'..docid..'?key='..apikey..'&fields=id,name,mimeType,owners,fullFileExtension'
|
|
|
|
local res, code = https.request(url)
|
|
|
|
local res = string.gsub(res, 'image/', '') -- snip mimetype
|
2016-06-19 19:25:24 +02:00
|
|
|
local res = string.gsub(res, 'application/', '')
|
|
|
|
if code ~= 200 then return nil end
|
|
|
|
local data = json.decode(res)
|
|
|
|
return data
|
|
|
|
end
|
|
|
|
|
|
|
|
function gdrive:send_drive_document_data(data, self, msg)
|
2016-07-20 00:49:31 +02:00
|
|
|
local title = data.name
|
2016-06-19 19:25:24 +02:00
|
|
|
local mimetype = data.mimeType
|
|
|
|
local id = data.id
|
2016-07-20 00:49:31 +02:00
|
|
|
local owner = data.owners[1].displayName
|
2016-06-19 19:25:24 +02:00
|
|
|
local text = '"'..title..'", freigegeben von '..owner
|
2016-07-20 00:49:31 +02:00
|
|
|
if mimetype:match('google') then -- if document is Google document (like a Spreadsheet)
|
|
|
|
if mimetype:match('drawing') then -- Drawing
|
|
|
|
local image_url = BASE_URL..'/files/'..id..'/export?key='..apikey..'&mimeType=image/png'
|
2016-06-19 19:25:24 +02:00
|
|
|
utilities.send_typing(self, msg.chat.id, 'upload_photo')
|
2016-07-20 00:49:31 +02:00
|
|
|
local file = download_to_file(image_url, 'export.png')
|
|
|
|
utilities.send_photo(self, msg.chat.id, file, text, msg.message_id)
|
2016-06-19 19:25:24 +02:00
|
|
|
return
|
|
|
|
else
|
2016-07-20 00:49:31 +02:00
|
|
|
local pdf_url = BASE_URL..'/files/'..id..'/export?key='..apikey..'&mimeType=application/pdf'
|
2016-06-19 19:25:24 +02:00
|
|
|
utilities.send_typing(self, msg.chat.id, 'upload_document')
|
2016-07-19 19:40:23 +02:00
|
|
|
local file = download_to_file(pdf_url, 'document.pdf')
|
2016-07-20 00:49:31 +02:00
|
|
|
utilities.send_document(self, msg.chat.id, file, text, msg.message_id)
|
2016-06-19 19:25:24 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
else
|
2016-07-19 19:40:23 +02:00
|
|
|
local get_file_url = 'https://drive.google.com/uc?id='..id
|
|
|
|
local keyboard = '{"inline_keyboard":[[{"text":"Direktlink","url":"'..get_file_url..'"}]]}'
|
2016-07-20 00:49:31 +02:00
|
|
|
local ext = data.fullFileExtension
|
2016-06-19 19:25:24 +02:00
|
|
|
if mimetype == "png" or mimetype == "jpg" or mimetype == "jpeg" or mimetype == "gif" or mimetype == "webp" then
|
|
|
|
local respbody = {}
|
|
|
|
local options = {
|
|
|
|
url = get_file_url,
|
|
|
|
sink = ltn12.sink.table(respbody),
|
|
|
|
redirect = false
|
|
|
|
}
|
|
|
|
local response = {https.request(options)} -- luasec doesn't support 302 redirects, so we must contact gdrive again
|
|
|
|
local code = response[2]
|
|
|
|
local headers = response[3]
|
|
|
|
local file_url = headers.location
|
|
|
|
if ext == "jpg" or ext == "jpeg" or ext == "png" then
|
|
|
|
utilities.send_typing(self, msg.chat.id, 'upload_photo')
|
2016-07-19 19:40:23 +02:00
|
|
|
local file = download_to_file(file_url, 'photo.'..ext)
|
|
|
|
utilities.send_photo(self, msg.chat.id, file, text, msg.message_id, keyboard)
|
2016-06-19 19:25:24 +02:00
|
|
|
return
|
|
|
|
else
|
|
|
|
utilities.send_typing(self, msg.chat.id, 'upload_document')
|
2016-07-19 19:40:23 +02:00
|
|
|
local file = download_to_file(file_url, 'document.'..ext)
|
|
|
|
utilities.send_document(self, msg.chat.id, file, text, msg.message_id, keyboard)
|
2016-06-19 19:25:24 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
else
|
2016-07-19 19:40:23 +02:00
|
|
|
local text = '*'..title..'*, freigegeben von _'..owner..'_'
|
|
|
|
utilities.send_reply(self, msg, text, true, keyboard)
|
2016-06-19 19:25:24 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function gdrive:action(msg, config, matches)
|
|
|
|
local docid = matches[2]
|
|
|
|
local data = gdrive:get_drive_document_data(docid)
|
|
|
|
if not data then utilities.send_reply(self, msg, config.errors.connection) return end
|
|
|
|
gdrive:send_drive_document_data(data, self, msg)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
return gdrive
|