2015-12-05 16:03:43 +01:00
|
|
|
-- You need a Google API key and a Google Custom Search Engine set up to use this, in config.google_api_key and config.google_cse_key, respectively.
|
2016-04-12 05:55:46 +02:00
|
|
|
-- You must also sign up for the CSE in the Google Developer Console, and enable image results.
|
2015-12-05 16:03:43 +01:00
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
local gImages = {}
|
|
|
|
|
|
|
|
local HTTPS = require('ssl.https')
|
|
|
|
local URL = require('socket.url')
|
2016-04-15 21:07:23 +02:00
|
|
|
local JSON = require('dkjson')
|
2016-06-07 06:31:34 +02:00
|
|
|
local utilities = require('otouto.utilities')
|
2016-06-11 14:46:41 +02:00
|
|
|
local bindings = require('otouto.bindings')
|
2016-04-11 06:04:47 +02:00
|
|
|
|
2016-05-27 02:26:30 +02:00
|
|
|
function gImages:init(config)
|
2016-06-11 14:46:41 +02:00
|
|
|
if not cred_data.google_apikey then
|
|
|
|
print('Missing config value: google_apikey.')
|
2016-04-11 06:04:47 +02:00
|
|
|
print('gImages.lua will not be enabled.')
|
|
|
|
return
|
2016-06-11 14:46:41 +02:00
|
|
|
elseif not cred_data.google_cse_id then
|
|
|
|
print('Missing config value: google_cse_id.')
|
2016-04-11 06:04:47 +02:00
|
|
|
print('gImages.lua will not be enabled.')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-06-11 14:46:41 +02:00
|
|
|
gImages.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('img', true):t('i', true):t('insfw', true).table
|
2016-05-27 05:28:44 +02:00
|
|
|
gImages.doc = [[```
|
2016-06-11 14:46:41 +02:00
|
|
|
]]..config.cmd_pat..[[img <Suchbegriff>
|
|
|
|
Sucht Bild mit Google und versendet es (SafeSearch aktiv)
|
2016-05-27 05:28:44 +02:00
|
|
|
Alias: ]]..config.cmd_pat..[[i
|
|
|
|
```]]
|
2015-12-05 16:03:43 +01:00
|
|
|
end
|
|
|
|
|
2016-06-11 14:46:41 +02:00
|
|
|
gImages.command = 'img <Suchbegriff>'
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-05-27 02:26:30 +02:00
|
|
|
function gImages:action(msg, config)
|
2016-06-11 14:46:41 +02:00
|
|
|
local input = utilities.input(msg.text)
|
|
|
|
if not input then
|
|
|
|
if msg.reply_to_message and msg.reply_to_message.text then
|
|
|
|
input = msg.reply_to_message.text
|
|
|
|
else
|
|
|
|
utilities.send_message(self, msg.chat.id, gImages.doc, true, msg.message_id, true)
|
|
|
|
return
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
2016-06-11 14:46:41 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
print ('Checking if search contains blacklisted word: '..input)
|
|
|
|
if is_blacklisted(input) then
|
|
|
|
utilities.send_reply(self, msg, 'Vergiss es! ._.')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local apikey = cred_data.google_apikey
|
|
|
|
local cseid = cred_data.google_cse_id
|
|
|
|
local BASE_URL = 'https://www.googleapis.com/customsearch/v1'
|
|
|
|
local url = BASE_URL..'/?searchType=image&alt=json&num=10&key='..apikey..'&cx='..cseid..'&safe=high'..'&q=' .. URL.escape(input)
|
|
|
|
local jstr, res = HTTPS.request(url)
|
|
|
|
|
|
|
|
if res ~= 200 then
|
|
|
|
utilities.send_reply(self, msg, config.errors.connection)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local jdat = JSON.decode(jstr)
|
|
|
|
if jdat.searchInformation.totalResults == '0' then
|
|
|
|
utilities.send_reply(self, msg, config.errors.results)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local i = math.random(jdat.queries.request[1].count)
|
|
|
|
local img_url = jdat.items[i].link
|
|
|
|
|
|
|
|
local file = download_to_file(img_url)
|
|
|
|
bindings.sendPhoto(self, {chat_id = msg.chat.id, caption = img_url}, {photo = file} )
|
|
|
|
os.remove(file)
|
|
|
|
print("Deleted: "..file)
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
return gImages
|