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-04-11 06:04:47 +02:00
|
|
|
local utilities = require('utilities')
|
|
|
|
|
2016-05-27 02:26:30 +02:00
|
|
|
function lastfm:init(config)
|
|
|
|
if not config.lastfm_api_key then
|
2016-04-11 06:04:47 +02:00
|
|
|
print('Missing config value: lastfm_api_key.')
|
|
|
|
print('lastfm.lua will not be enabled.')
|
|
|
|
return
|
|
|
|
end
|
2016-03-04 19:06:13 +01:00
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
lastfm.triggers = utilities.triggers(self.info.username):t('lastfm', true):t('np', true):t('fmset', true).table
|
|
|
|
end
|
|
|
|
|
2016-05-10 21:49:05 +02:00
|
|
|
lastfm.command = 'lastfm'
|
|
|
|
lastfm.doc = [[```
|
2016-05-27 02:59:45 +02:00
|
|
|
]]..utilities.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-05-27 02:59:45 +02:00
|
|
|
]]..utilities.CMD_PAT..[[fmset <username>
|
|
|
|
Sets your last.fm username. Otherwise, ]]..utilities.CMD_PAT..[[np will use your Telegram username. Use "]]..utilities.CMD_PAT..[[fmset --" to delete it.
|
2016-01-08 14:44:37 +01:00
|
|
|
```]]
|
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-04-08 23:12:02 +02:00
|
|
|
local input = utilities.input(msg.text)
|
2015-08-04 22:11:55 +02:00
|
|
|
|
2016-05-27 02:59:45 +02:00
|
|
|
if string.match(msg.text, '^'..utilities.CMD_PAT..'lastfm') then
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_message(self, msg.chat.id, lastfm.doc, true, msg.message_id, true)
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
2016-05-27 02:59:45 +02:00
|
|
|
elseif string.match(msg.text, '^'..utilities.CMD_PAT..'fmset') then
|
2015-08-04 22:11:55 +02:00
|
|
|
if not input then
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_message(self, msg.chat.id, lastfm.doc, true, msg.message_id, true)
|
2016-05-25 15:01:54 +02:00
|
|
|
elseif input == '--' or input == utilities.char.em_dash then
|
2016-04-11 06:04:47 +02:00
|
|
|
self.database.users[msg.from.id_str].lastfm = nil
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_reply(self, msg, 'Your last.fm username has been forgotten.')
|
2015-11-25 03:22:04 +01:00
|
|
|
else
|
2016-04-11 06:04:47 +02:00
|
|
|
self.database.users[msg.from.id_str].lastfm = input
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_reply(self, msg, 'Your last.fm username has been set to "' .. input .. '".')
|
2015-08-04 22:11:55 +02:00
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-05-27 02:26:30 +02: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-04-27 01:37:55 +02:00
|
|
|
local alert = ''
|
2015-11-25 03:22:04 +01:00
|
|
|
if input then
|
|
|
|
username = input
|
2016-04-11 06:04:47 +02:00
|
|
|
elseif self.database.users[msg.from.id_str].lastfm then
|
|
|
|
username = self.database.users[msg.from.id_str].lastfm
|
2015-11-25 03:22:04 +01:00
|
|
|
elseif msg.from.username then
|
|
|
|
username = msg.from.username
|
2016-05-27 02:59:45 +02:00
|
|
|
alert = '\n\nYour username has been set to ' .. username .. '.\nTo change it, use '..utilities.CMD_PAT..'fmset <username>.'
|
2016-04-11 06:04:47 +02:00
|
|
|
self.database.users[msg.from.id_str].lastfm = username
|
2015-11-25 03:22:04 +01:00
|
|
|
else
|
2016-05-27 02:59:45 +02:00
|
|
|
utilities.send_reply(self, msg, 'Please specify your last.fm username or set it with '..utilities.CMD_PAT..'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
|
|
|
url = url .. URL.escape(username)
|
2015-07-29 00:13:46 +02:00
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
local jstr, res
|
|
|
|
utilities.with_http_timeout(
|
|
|
|
1, function ()
|
|
|
|
jstr, res = HTTP.request(url)
|
|
|
|
end)
|
2015-07-29 00:13:46 +02:00
|
|
|
if res ~= 200 then
|
2016-05-27 02:26:30 +02:00
|
|
|
utilities.send_reply(self, msg, config.errors.connection)
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
2015-07-29 00:13:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local jdat = JSON.decode(jstr)
|
|
|
|
if jdat.error then
|
2016-05-27 02:59:45 +02:00
|
|
|
utilities.send_reply(self, msg, 'Please specify your last.fm username or set it with '..utilities.CMD_PAT..'fmset.')
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
2015-07-29 00:13:46 +02:00
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
jdat = jdat.recenttracks.track[1] or jdat.recenttracks.track
|
2015-11-25 03:22:04 +01:00
|
|
|
if not jdat then
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_reply(self, msg, 'No history for this user.' .. alert)
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
2015-07-29 00:13:46 +02:00
|
|
|
end
|
|
|
|
|
2016-04-27 01:37:55 +02:00
|
|
|
local output = input or msg.from.first_name
|
|
|
|
output = '🎵 ' .. output
|
2015-07-29 00:13:46 +02:00
|
|
|
|
|
|
|
if jdat['@attr'] and jdat['@attr'].nowplaying then
|
2016-04-27 01:37:55 +02:00
|
|
|
output = output .. ' is currently listening to:\n'
|
2015-11-25 03:22:04 +01:00
|
|
|
else
|
2016-04-27 01:37:55 +02:00
|
|
|
output = output .. ' 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-04-27 01:37:55 +02:00
|
|
|
output = output .. title .. ' - ' .. artist .. alert
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_message(self, msg.chat.id, output)
|
2015-07-29 00:13:46 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
return lastfm
|