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
|
|
|
|
2016-02-21 20:28:40 +01:00
|
|
|
if not database.lastfm then
|
|
|
|
database.lastfm = {}
|
|
|
|
end
|
|
|
|
|
2016-03-04 19:06:13 +01:00
|
|
|
local HTTP = require('socket.http')
|
|
|
|
HTTP.TIMEOUT = 1
|
|
|
|
|
2016-01-08 14:44:37 +01:00
|
|
|
local command = 'lastfm'
|
|
|
|
local doc = [[```
|
|
|
|
/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
|
|
|
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
|
2016-01-09 04:42:31 +01:00
|
|
|
sendMessage(msg.chat.id, doc, true, msg.message_id, true)
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
|
|
|
elseif string.match(msg.text, '^/fmset') then
|
2015-08-04 22:11:55 +02:00
|
|
|
if not input then
|
2016-01-08 14:44:37 +01:00
|
|
|
sendMessage(msg.chat.id, doc, true, msg.message_id, true)
|
2015-11-25 03:22:04 +01:00
|
|
|
elseif input == '-' then
|
2016-02-21 20:28:40 +01:00
|
|
|
database.lastfm[msg.from.id_str] = nil
|
2015-11-25 03:22:04 +01:00
|
|
|
sendReply(msg, 'Your last.fm username has been forgotten.')
|
|
|
|
else
|
2016-02-21 20:28:40 +01:00
|
|
|
database.lastfm[msg.from.id_str] = input
|
2015-11-25 03:22:04 +01:00
|
|
|
sendReply(msg, 'Your last.fm username has been set to "' .. input .. '".')
|
2015-08-04 22:11:55 +02:00
|
|
|
end
|
|
|
|
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
|
2016-01-08 04:30:12 +01:00
|
|
|
local output = ''
|
2015-11-25 03:22:04 +01:00
|
|
|
if input then
|
|
|
|
username = input
|
2016-02-21 20:28:40 +01:00
|
|
|
elseif database.lastfm[msg.from.id_str] then
|
|
|
|
username = database.lastfm[msg.from.id_str]
|
2015-11-25 03:22:04 +01:00
|
|
|
elseif msg.from.username then
|
|
|
|
username = msg.from.username
|
2016-01-08 04:30:12 +01:00
|
|
|
output = '\n\nYour username has been set to ' .. username .. '.\nTo change it, use /fmset <username>.'
|
2016-02-21 20:28:40 +01:00
|
|
|
database.lastfm[msg.from.id_str] = username
|
2015-11-25 03:22:04 +01:00
|
|
|
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
|
|
|
|
2016-03-04 19:06:13 +01:00
|
|
|
jstr, res = HTTP.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
|
2016-01-13 19:00:17 +01:00
|
|
|
sendReply(msg, 'Please specify your last.fm username or set it with /fmset.')
|
2015-11-25 03:22:04 +01:00
|
|
|
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
|
2016-01-08 04:30:12 +01:00
|
|
|
sendReply(msg, 'No history for this user.' .. output)
|
2015-11-25 03:22:04 +01:00
|
|
|
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
|
|
|
|
|
2016-01-08 04:30:12 +01:00
|
|
|
message = message .. title .. ' - ' .. artist .. output
|
2015-12-05 15:30:52 +01:00
|
|
|
sendMessage(msg.chat.id, message)
|
2015-07-29 00:13:46 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
return {
|
|
|
|
action = action,
|
|
|
|
triggers = triggers,
|
2016-01-08 14:44:37 +01:00
|
|
|
doc = doc,
|
|
|
|
command = command
|
2015-11-25 03:22:04 +01:00
|
|
|
}
|