2016-02-21 20:28:40 +01:00
|
|
|
if not database.librefm then
|
|
|
|
database.librefm = {}
|
|
|
|
end
|
|
|
|
|
2016-01-15 04:30:53 +01:00
|
|
|
local command = 'librefm'
|
|
|
|
local doc = [[```
|
|
|
|
/lnp [username]
|
|
|
|
Returns what you are or were last listening to. If you specify a username, info will be returned for that username.
|
|
|
|
|
|
|
|
/lfmset <username>
|
2016-01-15 04:34:25 +01:00
|
|
|
Sets your libre.fm username. Otherwise, /np will use your Telegram username. Use "/fmset -" to delete it.
|
2016-01-15 04:30:53 +01:00
|
|
|
```]]
|
|
|
|
|
|
|
|
local triggers = {
|
2016-02-14 09:47:02 +01:00
|
|
|
'^/librefm[@'..bot.username..']*',
|
2016-01-15 04:30:53 +01:00
|
|
|
'^/lnp[@'..bot.username..']*',
|
|
|
|
'^/lfmset[@'..bot.username..']*'
|
|
|
|
}
|
|
|
|
|
|
|
|
local action = function(msg)
|
|
|
|
|
|
|
|
local input = msg.text:input()
|
|
|
|
|
|
|
|
if string.match(msg.text, '^/librefm') then
|
|
|
|
sendMessage(msg.chat.id, doc, true, msg.message_id, true)
|
|
|
|
return
|
|
|
|
elseif string.match(msg.text, '^/lfmset') then
|
|
|
|
if not input then
|
|
|
|
sendMessage(msg.chat.id, doc, true, msg.message_id, true)
|
|
|
|
elseif input == '-' then
|
2016-04-27 01:37:55 +02:00
|
|
|
database.librefm[msg.from.id_str] = nil
|
2016-01-15 04:34:25 +01:00
|
|
|
sendReply(msg, 'Your libre.fm username has been forgotten.')
|
2016-01-15 04:30:53 +01:00
|
|
|
else
|
2016-04-27 01:37:55 +02:00
|
|
|
database.librefm[msg.from.id_str] = input
|
2016-01-15 04:34:25 +01:00
|
|
|
sendReply(msg, 'Your libre.fm username has been set to "' .. input .. '".')
|
2016-01-15 04:30:53 +01:00
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local url = 'http://alpha.libre.fm/2.0/?method=user.getrecenttracks&format=json&limit=1&api_key=0&user='
|
|
|
|
|
|
|
|
local username
|
2016-04-27 01:37:55 +02:00
|
|
|
local alert = ''
|
2016-01-15 04:30:53 +01:00
|
|
|
if input then
|
|
|
|
username = input
|
2016-04-27 01:37:55 +02:00
|
|
|
elseif database.librefm[msg.from.id_str] then
|
|
|
|
username = database.librefm[msg.from.id_str]
|
2016-01-15 04:30:53 +01:00
|
|
|
elseif msg.from.username then
|
|
|
|
username = msg.from.username
|
2016-04-27 01:37:55 +02:00
|
|
|
alert = '\n\nYour username has been set to ' .. username .. '.\nTo change it, use /lfmset <username>.'
|
|
|
|
database.librefm[msg.from.id_str] = username
|
2016-01-15 04:30:53 +01:00
|
|
|
else
|
2016-01-15 04:34:25 +01:00
|
|
|
sendReply(msg, 'Please specify your libre.fm username or set it with /lfmset.')
|
2016-01-15 04:30:53 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
url = url .. URL.escape(username)
|
|
|
|
|
|
|
|
jstr, res = HTTPS.request(url)
|
|
|
|
if res ~= 200 then
|
|
|
|
sendReply(msg, config.errors.connection)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local jdat = JSON.decode(jstr)
|
|
|
|
if jdat.error then
|
2016-01-15 04:34:25 +01:00
|
|
|
sendReply(msg, 'Please specify your libre.fm username or set it with /lfmset.')
|
2016-01-15 04:30:53 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local jdat = jdat.recenttracks.track[1] or jdat.recenttracks.track
|
|
|
|
if not jdat then
|
2016-04-27 01:37:55 +02:00
|
|
|
sendReply(msg, 'No history for this user.' .. alert)
|
2016-01-15 04:30:53 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-04-27 01:37:55 +02:00
|
|
|
local output = input or msg.from.first_name
|
|
|
|
output = '🎵 ' .. output
|
2016-01-15 04:30:53 +01:00
|
|
|
|
|
|
|
if jdat['@attr'] and jdat['@attr'].nowplaying then
|
2016-04-27 01:37:55 +02:00
|
|
|
output = output .. ' is currently listening to:\n'
|
2016-01-15 04:30:53 +01:00
|
|
|
else
|
2016-04-27 01:37:55 +02:00
|
|
|
output = output .. ' last listened to:\n'
|
2016-01-15 04:30:53 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local title = jdat.name or 'Unknown'
|
|
|
|
local artist = 'Unknown'
|
|
|
|
if jdat.artist then
|
|
|
|
artist = jdat.artist['#text']
|
|
|
|
end
|
|
|
|
|
2016-04-27 01:37:55 +02:00
|
|
|
output = output .. title .. ' - ' .. artist .. alert
|
|
|
|
sendMessage(msg.chat.id, output)
|
2016-01-15 04:30:53 +01:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
|
|
|
action = action,
|
|
|
|
triggers = triggers,
|
|
|
|
doc = doc,
|
|
|
|
command = command
|
|
|
|
}
|