2015-11-25 03:22:04 +01:00
|
|
|
if not config.lastfm_api_key then
|
|
|
|
print('Missing config value: lastfm_api_key.')
|
|
|
|
print('lastfm.lua will not be enabled.')
|
|
|
|
return
|
|
|
|
end
|
2015-07-29 00:13:46 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local doc = [[
|
|
|
|
/lastfm
|
|
|
|
/np [username]
|
|
|
|
Returns what you are or were last listening to. If you specify a username, info will be returned for that username.
|
|
|
|
/fmset <username>
|
|
|
|
Sets your last.fm username. Otherwise, /np will use your Telegram username. Use "/fmset -" to delete it.
|
2015-07-29 00:13:46 +02:00
|
|
|
]]
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local triggers = {
|
|
|
|
'^/lastfm[@'..bot.username..']*',
|
|
|
|
'^/np[@'..bot.username..']*',
|
|
|
|
'^/fmset[@'..bot.username..']*'
|
2015-07-29 00:13:46 +02:00
|
|
|
}
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local action = function(msg)
|
2015-07-29 00:13:46 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
lastfm = load_data('lastfm.json')
|
|
|
|
local input = msg.text:input()
|
2015-08-04 22:11:55 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
if string.match(msg.text, '^/lastfm') then
|
|
|
|
sendReply(msg, doc:sub(10))
|
|
|
|
return
|
|
|
|
elseif string.match(msg.text, '^/fmset') then
|
2015-08-04 22:11:55 +02:00
|
|
|
if not input then
|
2015-11-25 03:22:04 +01:00
|
|
|
sendReply(msg, doc)
|
|
|
|
elseif input == '-' then
|
|
|
|
lastfm[msg.from.id_str] = nil
|
|
|
|
sendReply(msg, 'Your last.fm username has been forgotten.')
|
|
|
|
else
|
|
|
|
lastfm[msg.from.id_str] = input
|
|
|
|
sendReply(msg, 'Your last.fm username has been set to "' .. input .. '".')
|
2015-08-04 22:11:55 +02:00
|
|
|
end
|
2015-11-25 03:22:04 +01:00
|
|
|
save_data('lastfm.json', lastfm)
|
2015-08-04 22:11:55 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local url = 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&format=json&limit=1&api_key=' .. config.lastfm_api_key .. '&user='
|
2015-07-29 00:13:46 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local username
|
|
|
|
if input then
|
|
|
|
username = input
|
|
|
|
elseif lastfm[msg.from.id_str] then
|
|
|
|
username = lastfm[msg.from.id_str]
|
|
|
|
elseif msg.from.username then
|
|
|
|
username = msg.from.username
|
|
|
|
else
|
|
|
|
sendReply(msg, 'Please specify your last.fm username or set it with /fmset.')
|
|
|
|
return
|
2015-07-29 00:13:46 +02:00
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
url = url .. URL.escape(username)
|
2015-07-29 00:13:46 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
jstr, res = HTTPS.request(url)
|
2015-07-29 00:13:46 +02:00
|
|
|
if res ~= 200 then
|
2015-11-25 03:22:04 +01:00
|
|
|
sendReply(msg, config.errors.connection)
|
|
|
|
return
|
2015-07-29 00:13:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local jdat = JSON.decode(jstr)
|
|
|
|
if jdat.error then
|
2015-11-25 03:22:04 +01:00
|
|
|
sendReply(msg, jdat.error)
|
|
|
|
return
|
2015-07-29 00:13:46 +02:00
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local jdat = jdat.recenttracks.track[1] or jdat.recenttracks.track
|
|
|
|
if not jdat then
|
|
|
|
sendReply(msg, 'No history for this user.')
|
|
|
|
return
|
2015-07-29 00:13:46 +02:00
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local message = input or msg.from.first_name
|
|
|
|
message = '🎵 ' .. message
|
2015-07-29 00:13:46 +02:00
|
|
|
|
|
|
|
if jdat['@attr'] and jdat['@attr'].nowplaying then
|
2015-11-25 03:22:04 +01:00
|
|
|
message = message .. ' is currently listening to:\n'
|
|
|
|
else
|
|
|
|
message = message .. ' last listened to:\n'
|
2015-08-23 08:46:34 +02:00
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local title = jdat.name or 'Unknown'
|
|
|
|
local artist = 'Unknown'
|
2015-08-23 08:46:34 +02:00
|
|
|
if jdat.artist then
|
|
|
|
artist = jdat.artist['#text']
|
2015-07-29 00:13:46 +02:00
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
message = message .. title .. ' - ' .. artist
|
|
|
|
sendReply(msg, message)
|
2015-07-29 00:13:46 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
return {
|
|
|
|
action = action,
|
|
|
|
triggers = triggers,
|
|
|
|
doc = doc
|
|
|
|
}
|