2016-05-29 19:08:39 +02:00
|
|
|
-- TODO: Add support for librefm API.
|
|
|
|
-- Just kidding, nobody actually uses that.
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
local lastfm = {}
|
2015-07-29 00:13:46 +02:00
|
|
|
|
2016-03-04 19:06:13 +01:00
|
|
|
local HTTP = require('socket.http')
|
2016-04-11 06:04:47 +02:00
|
|
|
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-04-11 06:04:47 +02:00
|
|
|
|
2016-05-27 02:26:30 +02:00
|
|
|
function lastfm:init(config)
|
2016-10-04 16:07:15 +02:00
|
|
|
assert(
|
|
|
|
config.lastfm_api_key,
|
2016-08-14 04:46:18 +02:00
|
|
|
'lastfm.lua requires a last.fm API key from http://last.fm/api.'
|
|
|
|
)
|
2016-03-04 19:06:13 +01:00
|
|
|
|
2016-10-04 16:07:15 +02:00
|
|
|
lastfm.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('lastfm', true):t('np', true):t('npfull', true):t('fmset', true).table
|
2016-08-14 04:46:18 +02:00
|
|
|
lastfm.doc = config.cmd_pat .. [[np [username]
|
2016-01-08 14:44:37 +01:00
|
|
|
Returns what you are or were last listening to. If you specify a username, info will be returned for that username.
|
|
|
|
|
2016-10-04 16:07:15 +02:00
|
|
|
]] .. config.cmd_pat .. [[npfull [username]
|
|
|
|
Works like ]] .. config.cmd_pat .. [[np, but returns more info, differently formatted and including album art, if available.
|
|
|
|
|
2016-07-25 11:03:35 +02:00
|
|
|
]] .. 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.]]
|
2016-05-27 05:28:44 +02:00
|
|
|
|
2016-10-04 16:07:15 +02:00
|
|
|
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
|
2015-07-29 00:13:46 +02:00
|
|
|
|
2016-05-27 02:26:30 +02:00
|
|
|
function lastfm:action(msg, config)
|
2015-07-29 00:13:46 +02:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
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 {}
|
|
|
|
|
2016-10-04 16:07:15 +02:00
|
|
|
if string.match(msg.text_lower, '^'..config.cmd_pat..'lastfm') then
|
|
|
|
utilities.send_message(msg.chat.id, lastfm.doc, true, msg.message_id, 'html')
|
2016-08-14 04:46:18 +02:00
|
|
|
return
|
2016-10-04 16:07:15 +02:00
|
|
|
elseif string.match(msg.text_lower, '^'..config.cmd_pat..'fmset') then
|
2016-08-14 04:46:18 +02:00
|
|
|
if not input then
|
2016-10-04 16:07:15 +02:00
|
|
|
utilities.send_message(msg.chat.id, lastfm.doc, true, msg.message_id, 'html')
|
2016-08-14 04:46:18 +02:00
|
|
|
elseif input == '--' or input == utilities.char.em_dash then
|
|
|
|
self.database.userdata[from_id_str].lastfm = nil
|
2016-08-23 06:16:32 +02:00
|
|
|
utilities.send_reply(msg, 'Your last.fm username has been forgotten.')
|
2016-08-14 04:46:18 +02:00
|
|
|
else
|
|
|
|
self.database.userdata[from_id_str].lastfm = input
|
2016-08-23 06:16:32 +02:00
|
|
|
utilities.send_reply(msg, 'Your last.fm username has been set to "' .. input .. '".')
|
2016-08-14 04:46:18 +02:00
|
|
|
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
|
2016-08-23 06:16:32 +02:00
|
|
|
utilities.send_reply(msg, 'Please specify your last.fm username or set it with '..config.cmd_pat..'fmset.')
|
2016-08-14 04:46:18 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-10-04 16:07:15 +02:00
|
|
|
local orig = HTTP.TIMEOUT
|
|
|
|
HTTP.TIMEOUT = 1
|
|
|
|
local jstr, res = HTTP.request(lastfm.base_url .. URL.escape(username))
|
|
|
|
HTTP.TIMEOUT = orig
|
2016-08-14 04:46:18 +02:00
|
|
|
|
|
|
|
if res ~= 200 then
|
2016-08-23 06:16:32 +02:00
|
|
|
utilities.send_reply(msg, config.errors.connection)
|
2016-08-14 04:46:18 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local jdat = JSON.decode(jstr)
|
|
|
|
if jdat.error then
|
2016-08-23 06:16:32 +02:00
|
|
|
utilities.send_reply(msg, 'Please specify your last.fm username or set it with '..config.cmd_pat..'fmset.')
|
2016-08-14 04:46:18 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-10-04 16:07:15 +02:00
|
|
|
local track = jdat.recenttracks.track[1] or jdat.recenttracks.track
|
|
|
|
if not track then
|
2016-08-23 06:16:32 +02:00
|
|
|
utilities.send_reply(msg, 'No history for this user.' .. alert)
|
2016-08-14 04:46:18 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-10-04 16:07:15 +02:00
|
|
|
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:'
|
2016-08-14 04:46:18 +02:00
|
|
|
else
|
2016-10-04 16:07:15 +02:00
|
|
|
output = output .. ' last listened to:'
|
2016-08-14 04:46:18 +02:00
|
|
|
end
|
|
|
|
|
2016-10-04 16:07:15 +02:00
|
|
|
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'))
|
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
end
|
|
|
|
|
2016-10-04 16:07:15 +02:00
|
|
|
output = output .. alert
|
|
|
|
|
|
|
|
utilities.send_message(msg.chat.id, output, nil, nil, 'html')
|
2015-07-29 00:13:46 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
return lastfm
|