- App Store: Fixe Pattern

- Portierung folender Plugins:
  - BR
  - Heise
  - Tagesschau
  - Wiimmfi
  - Wikia
This commit is contained in:
Andreas Bielawski 2016-06-15 18:45:56 +02:00
parent 4a6b9b1816
commit c4362b2196
6 changed files with 255 additions and 1 deletions

View File

@ -7,7 +7,7 @@ local redis = (loadfile "./otouto/redis.lua")()
app_store.triggers = { app_store.triggers = {
"itunes.apple.com/(.*)/app/(.*)/id(%d+)", "itunes.apple.com/(.*)/app/(.*)/id(%d+)",
"^!itunes (%d+)$", "^/itunes (%d+)$",
"itunes.apple.com/app/id(%d+)" "itunes.apple.com/app/id(%d+)"
} }

51
otouto/plugins/br.lua Normal file
View File

@ -0,0 +1,51 @@
local br = {}
local https = require('ssl.https')
local URL = require('socket.url')
local json = require('dkjson')
local utilities = require('otouto.utilities')
local bindings = require('otouto.bindings')
br.triggers = {
"br.de/nachrichten/(.*).html$"
}
function br:get_br_article(article)
local url = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url=%22http://www.br.de/nachrichten/'..article..'.html%22%20and%20xpath=%22//div[@id=%27content%27]/div[1]/div[2]/div[1]|//div[@class=%27lead_picture%27]/img%22&format=json'
local res,code = https.request(url)
local data = json.decode(res).query.results
if code ~= 200 then return "HTTP-Fehler" end
if not data then return "HTTP-Fehler" end
local subtitle = data.div.h1.em
local title = string.sub(data.div.h1.content, 3)
local teaser = string.sub(data.div.p[1].content, 2)
if data.div.p[3] then
updated = data.div.p[3].content
else
updated = data.div.p[2].content
end
if data.img then
image_url = 'https://www.br.de'..data.img.src
end
local text = '*'..subtitle..' - '..title..'*'..teaser..'\n_'..updated..'_'
if data.img then
return text, image_url
else
return text
end
end
function br:action(msg, config, matches)
local article = URL.escape(matches[1])
local text, image_url = br:get_br_article(article)
if image_url then
utilities.send_typing(self, msg.chat.id, 'upload_photo')
local file = download_to_file(image_url, 'br_teaser.jpg')
utilities.send_photo(self, msg.chat.id, file, nil, msg.message_id)
end
utilities.send_reply(self, msg, text, true)
end
return br

44
otouto/plugins/heise.lua Normal file
View File

@ -0,0 +1,44 @@
local heise = {}
local https = require('ssl.https')
local URL = require('socket.url')
local json = require('dkjson')
local utilities = require('otouto.utilities')
local bindings = require('otouto.bindings')
heise.triggers = {
"heise.de/newsticker/meldung/(.*).html$"
}
function heise:get_heise_article(article)
local url = 'https://query.yahooapis.com/v1/public/yql?q=select%20content,src,strong%20from%20html%20where%20url=%22http://www.heise.de/newsticker/meldung/'..article..'.html%22%20and%20xpath=%22//div[@id=%27mitte_news%27]/article/header/h2|//div[@id=%27mitte_news%27]/article/div/p[1]/strong|//div[@id=%27mitte_news%27]/article/div/figure/img%22&format=json'
local res,code = https.request(url)
local data = json.decode(res).query.results
if code ~= 200 then return "HTTP-Fehler" end
local title = data.h2
local teaser = data.strong
if data.img then
image_url = 'https:'..data.img.src
end
local text = '*'..title..'*\n'..teaser
if data.img then
return text, image_url
else
return text
end
end
function heise:action(msg, config, matches)
local article = URL.escape(matches[1])
local text, image_url = heise:get_heise_article(article)
if image_url then
utilities.send_typing(self, msg.chat.id, 'upload_photo')
local file = download_to_file(image_url, 'heise_teaser.jpg')
utilities.send_photo(self, msg.chat.id, file, nil, msg.message_id)
end
utilities.send_reply(self, msg, text, true)
end
return heise

View File

@ -0,0 +1,56 @@
local tagesschau = {}
local https = require('ssl.https')
local URL = require('socket.url')
local json = require('dkjson')
local utilities = require('otouto.utilities')
local bindings = require('otouto.bindings')
tagesschau.triggers = {
"tagesschau.de/([A-Za-z0-9-_-_-/]+).html"
}
local BASE_URL = 'https://www.tagesschau.de/api'
local makeOurDate = function(dateString)
local pattern = "(%d+)%-(%d+)%-(%d+)T(%d+)%:(%d+)%:(%d+)"
local year, month, day, hours, minutes, seconds = dateString:match(pattern)
return day..'.'..month..'.'..year..' um '..hours..':'..minutes..':'..seconds
end
function tagesschau:get_tagesschau_article(article)
local url = BASE_URL..'/'..article..'.json'
local res,code = https.request(url)
local data = json.decode(res)
if code == 404 then return "Artikel nicht gefunden!" end
if code ~= 200 then return "HTTP-Fehler" end
if not data then return "HTTP-Fehler" end
if data.type ~= "story" then
print('Typ "'..data.type..'" wird nicht unterstützt')
return nil
end
local title = data.topline..': '..data.headline
local news = data.shorttext
local posted_at = makeOurDate(data.date)..' Uhr'
local text = '*'..title..'*\n_'..posted_at..'_\n'..news
if data.banner[1] then
return text, data.banner[1].variants[1].modPremium
else
return text
end
end
function tagesschau:action(msg, config, matches)
local article = matches[1]
local text, image_url = tagesschau:get_tagesschau_article(article)
if image_url then
utilities.send_typing(self, msg.chat.id, 'upload_photo')
local file = download_to_file(image_url)
utilities.send_photo(self, msg.chat.id, file, nil, msg.message_id)
end
utilities.send_reply(self, msg, text, true)
end
return tagesschau

View File

@ -0,0 +1,58 @@
local wiimmfi = {}
local http = require('socket.http')
local utilities = require('otouto.utilities')
local bindings = require('otouto.bindings')
function wiimmfi:init(config)
wiimmfi.triggers = {
"^/(mkw)$",
"^/wiimmfi$",
"^/wfc$"
}
wiimmfi.doc = [[*
]]..config.cmd_pat..[[wfc*: Zeigt alle Wiimmfi-Spieler an
*]]..config.cmd_pat..[[mkw*: Zeigt alle Mario-Kart-Wii-Spieler an]]
end
wiimmfi.command = 'wfc, /mkw'
function wiimmfi:getplayer(game)
local url = 'http://wiimmfi.de/game'
local res,code = http.request(url)
if code ~= 200 then return "Fehler beim Abrufen von wiimmfi.de" end
if game == 'mkw' then
local players = string.match(res, "<td align%=center><a href%=\"/game/mariokartwii\".->(.-)</a>")
if players == nil then players = 0 end
text = 'Es spielen gerade '..players..' Spieler Mario Kart Wii'
else
local players = string.match(res, "</tr><tr.->(.-)<th colspan%=3")
local players = string.gsub(players, "</a></td><td>.-<a href=\".-\">", ": ")
local players = string.gsub(players, "<td.->", "")
local players = string.gsub(players, "Wii</td>", "")
local players = string.gsub(players, "WiiWare</td>", "")
local players = string.gsub(players, "NDS</td>", "")
local players = string.gsub(players, "<th.->", "")
local players = string.gsub(players, "<tr.->", "")
local players = string.gsub(players, "</tr>", "")
local players = string.gsub(players, "</th>", "")
local players = string.gsub(players, "<a.->", "")
local players = string.gsub(players, "</a>", "")
local players = string.gsub(players, "</td>", "")
if players == nil then players = 'Momentan spielt keiner auf Wiimmfi :(' end
text = players
end
return text
end
function wiimmfi:action(msg, config, matches)
if matches[1] == "mkw" then
utilities.send_reply(self, msg, wiimmfi:getplayer('mkw'))
return
else
utilities.send_reply(self, msg, wiimmfi:getplayer())
return
end
end
return wiimmfi

45
otouto/plugins/wikia.lua Normal file
View File

@ -0,0 +1,45 @@
local wikia = {}
local http = require('socket.http')
local json = require('dkjson')
local utilities = require('otouto.utilities')
local bindings = require('otouto.bindings')
wikia.triggers = {
"https?://(.+).wikia.com/wiki/(.+)"
}
local BASE_URL = '.wikia.com/api/v1/Articles/Details?abstract=400'
function send_wikia_article(wikia, article)
local url = 'http://'..wikia..BASE_URL..'&titles='..article
local res,code = http.request(url)
if code ~= 200 then return "HTTP-FEHLER" end
if string.match(res, "Not a valid Wikia") then return 'Dieses Wikia existiert nicht!' end
local data = json.decode(res)
local keyset={}
local n=0
for id,_ in pairs(data.items) do
n=n+1
keyset[n]=id
end
local id = keyset[1]
if not id then return 'Diese Seite existiert nicht!' end
local title = data.items[id].title
local abstract = data.items[id].abstract
local article_url = data.basepath..data.items[id].url
local text = '*'..title..'*:\n'..abstract..' [Weiterlesen]('..article_url..')'
return text
end
function wikia:action(msg, config, matches)
local wikia = matches[1]
local article = matches[2]
utilities.send_reply(self, msg, send_wikia_article(wikia, article), true)
end
return wikia