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/lastfm.lua
topkecleon 9f760114bd otouto 3.14
All help messages and many other things moved from markdown to html.
Eventually, I'd like only things made from user input to use markdown.

cats.lua, rmspic.lua, and dilbert.lua moved to sendPhoto with URL.
xkcd.lua and apod.lua not moved to retain formatting.

Probably a lot of other stuff that I forget about. I should commit more often.
2016-10-04 10:07:15 -04:00

134 lines
5.1 KiB
Lua

-- TODO: Add support for librefm API.
-- Just kidding, nobody actually uses that.
local lastfm = {}
local HTTP = require('socket.http')
local URL = require('socket.url')
local JSON = require('dkjson')
local utilities = require('otouto.utilities')
function lastfm:init(config)
assert(
config.lastfm_api_key,
'lastfm.lua requires a last.fm API key from http://last.fm/api.'
)
lastfm.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('lastfm', true):t('np', true):t('npfull', true):t('fmset', true).table
lastfm.doc = config.cmd_pat .. [[np [username]
Returns what you are or were last listening to. If you specify a username, info will be returned for that username.
]] .. config.cmd_pat .. [[npfull [username]
Works like ]] .. config.cmd_pat .. [[np, but returns more info, differently formatted and including album art, if available.
]] .. config.cmd_pat .. [[fmset <username>
Sets your last.fm username. Otherwise, ]] .. config.cmd_pat .. [[np will use your Telegram username. Use "]] .. config.cmd_pat .. [[fmset --" to delete it.]]
lastfm.command = 'lastfm'
lastfm.base_url = 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&format=json&limit=1&api_key=' .. config.lastfm_api_key .. '&user='
end
function lastfm:action(msg, config)
local input = utilities.input(msg.text)
local from_id_str = tostring(msg.from.id)
self.database.userdata[from_id_str] = self.database.userdata[from_id_str] or {}
if string.match(msg.text_lower, '^'..config.cmd_pat..'lastfm') then
utilities.send_message(msg.chat.id, lastfm.doc, true, msg.message_id, 'html')
return
elseif string.match(msg.text_lower, '^'..config.cmd_pat..'fmset') then
if not input then
utilities.send_message(msg.chat.id, lastfm.doc, true, msg.message_id, 'html')
elseif input == '--' or input == utilities.char.em_dash then
self.database.userdata[from_id_str].lastfm = nil
utilities.send_reply(msg, 'Your last.fm username has been forgotten.')
else
self.database.userdata[from_id_str].lastfm = input
utilities.send_reply(msg, 'Your last.fm username has been set to "' .. input .. '".')
end
return
end
local username
local alert = ''
if input then
username = input
elseif self.database.userdata[from_id_str].lastfm then
username = self.database.userdata[from_id_str].lastfm
elseif msg.from.username then
username = msg.from.username
alert = '\n\nYour username has been set to ' .. username .. '.\nTo change it, use '..config.cmd_pat..'fmset <username>.'
self.database.userdata[from_id_str].lastfm = username
else
utilities.send_reply(msg, 'Please specify your last.fm username or set it with '..config.cmd_pat..'fmset.')
return
end
local orig = HTTP.TIMEOUT
HTTP.TIMEOUT = 1
local jstr, res = HTTP.request(lastfm.base_url .. URL.escape(username))
HTTP.TIMEOUT = orig
if res ~= 200 then
utilities.send_reply(msg, config.errors.connection)
return
end
local jdat = JSON.decode(jstr)
if jdat.error then
utilities.send_reply(msg, 'Please specify your last.fm username or set it with '..config.cmd_pat..'fmset.')
return
end
local track = jdat.recenttracks.track[1] or jdat.recenttracks.track
if not track then
utilities.send_reply(msg, 'No history for this user.' .. alert)
return
end
local output = utilities.html_escape(input or msg.from.first_name)
if track['@attr'] and track['@attr'].nowplaying then
output = output .. ' is currently listening to:'
else
output = output .. ' last listened to:'
end
if msg.text_lower:match('^' .. config.cmd_pat .. 'npfull') then
output = '<b>' .. utilities.html_escape(output) .. '</b>'
if track.name and #track.name > 0 then
output = output .. '\n🎵 ' .. utilities.html_escape(track.name)
else
output = output .. '\n🎵 Unknown'
end
if track.artist and track.artist['#text'] and #track.artist['#text'] > 0 then
output = output .. '\n👤 ' .. utilities.html_escape(track.artist['#text'])
end
if track.album and track.album['#text'] and #track.album['#text'] > 0 then
output = output .. '\n💿 ' .. utilities.html_escape(track.album['#text'])
end
-- album art
if track.image and track.image[3] and #track.image[3]['#text'] > 0 then
output = '<a href="' .. utilities.html_escape(track.image[3]['#text']) .. '">' .. utilities.char.zwnj .. '</a>' .. output
end
else
output = output .. '\n'
if track.artist and track.artist['#text'] and #track.artist['#text'] > 0 then
output = output .. utilities.html_escape(track.artist['#text']) .. ' - '
end
output = output .. utilities.html_escape((track.name or 'Unknown'))
end
output = output .. alert
utilities.send_message(msg.chat.id, output, nil, nil, 'html')
end
return lastfm