This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot-2/otouto/plugins/br.lua
Andreas Bielawski c4362b2196 - App Store: Fixe Pattern
- Portierung folender Plugins:
  - BR
  - Heise
  - Tagesschau
  - Wiimmfi
  - Wikia
2016-06-15 18:45:56 +02:00

51 lines
1.6 KiB
Lua

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