merge upstream

Colors on Non valids. Check msg.to.id and msg.from.id
function load_from_file(file)
vardump uses serpent
tabs on scandir
Failed on Free games (no price info)
Google with unescaped URLs
This commit is contained in:
Akamaru 2015-04-26 19:16:44 +02:00
parent 94fea4e9aa
commit fe78a49367
5 changed files with 73 additions and 65 deletions

4
.gitignore vendored
View File

@ -24,4 +24,6 @@ plugins/twitch.lua
plugins/create_sticker.lua plugins/create_sticker.lua
plugins/derpibooru_nsfw.lua plugins/derpibooru_nsfw.lua
plugins/derpibooru.lua plugins/derpibooru.lua
plugins/rss.lua plugins/rss.lua
plugins/url_info.lua
plugins/meme.lua

View File

@ -38,23 +38,33 @@ end
function msg_valid(msg) function msg_valid(msg)
-- Dont process outgoing messages -- Dont process outgoing messages
if msg.out then if msg.out then
print("Not valid: msg from us") print('\27[36mNot valid: msg from us\27[39m')
return false return false
end end
-- Before bot was started -- Before bot was started
if msg.date < now then if msg.date < now then
print("Not valid: old msg") print('\27[36mNot valid: old msg\27[39m')
return false return false
end end
if msg.unread == 0 then if msg.unread == 0 then
print("Not valid: readed") print('\27[36mNot valid: readed\27[39m')
return false return false
end end
if msg.service then if msg.service then
print("Not valid: service") print('\27[36mNot valid: service\27[39m')
return false
end
if not msg.to.id then
print('\27[36mNot valid: To id not provided\27[39m')
return false
end
if not msg.from.id then
print('\27[36mNot valid: From id not provided\27[39m')
return false return false
end end

View File

@ -116,49 +116,16 @@ function download_to_file(url, file_name)
end end
function vardump(value, depth, key) function vardump(value)
local linePrefix = "" print(serpent.block(value, {comment=false}))
local spaces = ""
if key ~= nil then
linePrefix = "["..key.."] = "
end
if depth == nil then
depth = 0
else
depth = depth + 1
for i=1, depth do spaces = spaces .. " " end
end
if type(value) == 'table' then
mTable = getmetatable(value)
if mTable == nil then
print(spaces ..linePrefix.."(table) ")
else
print(spaces .."(metatable) ")
value = mTable
end
for tableKey, tableValue in pairs(value) do
vardump(tableValue, depth, tableKey)
end
elseif type(value) == 'function' or
type(value) == 'thread' or
type(value) == 'userdata' or
value == nil
then
print(spaces..tostring(value))
else
print(spaces..linePrefix.."("..type(value)..") "..tostring(value))
end
end end
-- taken from http://stackoverflow.com/a/11130774/3163199 -- taken from http://stackoverflow.com/a/11130774/3163199
function scandir(directory) function scandir(directory)
local i, t, popen = 0, {}, io.popen local i, t, popen = 0, {}, io.popen
for filename in popen('ls -a "'..directory..'"'):lines() do for filename in popen('ls -a "'..directory..'"'):lines() do
i = i + 1 i = i + 1
t[i] = filename t[i] = filename
end end
return t return t
end end
@ -464,4 +431,19 @@ end
function sleep(n) function sleep(n)
os.execute("sleep " .. tonumber(n)) os.execute("sleep " .. tonumber(n))
end end
-- Function to read data from files
function load_from_file(file)
local f = io.open(file, "r+")
-- If file doesn't exists
if f == nil then
-- Create a new empty table
serialize_to_file({}, file)
print ('Created file', file)
else
print ('Data loaded from file', file)
f:close()
end
return loadfile (file)()
end

View File

@ -1,14 +1,18 @@
function googlethat(query) function googlethat(query)
local api = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&" local api = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&safe=active&hl=de&"
local parameters = "q=".. (URL.escape(query) or "") local parameters = "q=".. (URL.escape(query) or "")
-- Do the request -- Do the request
local res, code = https.request(api..parameters) local res, code = https.request(api..parameters)
if code ~=200 then return nil end if code ~=200 then return nil end
local data = json:decode(res) local data = json:decode(res)
local results={} local results={}
for key,result in ipairs(data.responseData.results) do for key,result in ipairs(data.responseData.results) do
table.insert(results,{result.titleNoFormatting, result.url}) table.insert(results, {
result.titleNoFormatting,
result.unescapedUrl or result.url
})
end end
return results return results
end end
@ -22,9 +26,8 @@ function stringlinks(results)
end end
function run(msg, matches) function run(msg, matches)
vardump(matches) local results = googlethat(matches[1])
local results = googlethat(matches[1]) return stringlinks(results)
return stringlinks(results)
end end
return { return {

View File

@ -1,12 +1,11 @@
-- See https://wiki.teamfortress.com/wiki/User:RJackson/StorefrontAPI -- See https://wiki.teamfortress.com/wiki/User:RJackson/StorefrontAPI
do do
local BASE_URL = 'http://store.steampowered.com/api/appdetails/?appids=' local BASE_URL = 'http://store.steampowered.com/api/appdetails/'
local DESC_LENTH = 200 local DESC_LENTH = 200
function unescape(str) local function unescape(str)
str = string.gsub( str, '&lt;', '<' ) str = string.gsub( str, '&lt;', '<' )
str = string.gsub( str, '&gt;', '>' ) str = string.gsub( str, '&gt;', '>' )
str = string.gsub( str, '&quot;', '"' ) str = string.gsub( str, '&quot;', '"' )
@ -17,28 +16,40 @@ function unescape(str)
return str return str
end end
function get_steam_data (appid) local function get_steam_data (appid)
local url = BASE_URL..appid local url = BASE_URL
url = url..'?appids='..appid
url = url..'&cc=DE'
local res,code = http.request(url) local res,code = http.request(url)
if code ~= 200 then return nil end if code ~= 200 then return nil end
local data = json:decode(res)[appid].data local data = json:decode(res)[appid].data
return data return data
end end
local function price_info (data)
local price = '' -- If no data is empty
function send_steam_data(data, receiver) if data then
local description = string.sub(unescape(data.about_the_game:gsub("%b<>", "")), 1, DESC_LENTH) .. '...' local initial = data.initial
local title = data.name local final = data.final or data.initial
local price = ""..(data.price_overview.initial/100) local min = math.min(data.initial, data.final)
local sale_price = ""..(data.price_overview.final/100) price = tostring(min/100)
local percent_savings = data.price_overview.discount_percent if data.discount_percent and initial ~= final then
local price_display = price price = price..data.currency..' ('..data.discount_percent..'% OFF)'
end
if percent_savings ~= 0 then price = price..' (US)'
price_display = price.." -> "..sale_price.." ("..percent_savings.."%)"
end end
local text = title..' '..price_display..'\n'..description return price
end
local function send_steam_data(data, receiver)
local description = string.sub(unescape(data.about_the_game:gsub("%b<>", "")), 1, DESC_LENTH) .. '...'
local title = data.name
local price = price_info(data.price_overview)
local text = title..' '..price..'\n'..description
local image_url = data.header_image local image_url = data.header_image
local cb_extra = { local cb_extra = {
receiver = receiver, receiver = receiver,
@ -48,7 +59,7 @@ function send_steam_data(data, receiver)
end end
function run(msg, matches) local function run(msg, matches)
local appid = matches[1] local appid = matches[1]
local data = get_steam_data(appid) local data = get_steam_data(appid)
local receiver = get_receiver(msg) local receiver = get_receiver(msg)