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 bindings = require('bindings')
|
|
|
|
local utilities = require('utilities')
|
|
|
|
|
|
|
|
function lastfm:init()
|
|
|
|
if not self.config.lastfm_api_key then
|
|
|
|
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
|
|
|
|
|
|
|
|
bindings.command = 'lastfm'
|
|
|
|
bindings.doc = [[```
|
2016-01-08 14:44:37 +01:00
|
|
|
/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>
|
2016-03-22 11:16:26 +01:00
|
|
|
Sets your last.fm username. Otherwise, /np will use your Telegram username. Use "/fmset --" to delete it.
|
2016-01-08 14:44:37 +01:00
|
|
|
```]]
|
2015-07-29 00:13:46 +02:00
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
function lastfm:action(msg)
|
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
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
if string.match(msg.text, '^/lastfm') then
|
2016-04-11 06:04:47 +02:00
|
|
|
bindings.sendMessage(self, msg.chat.id, lastfm.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-04-11 06:04:47 +02:00
|
|
|
bindings.sendMessage(self, msg.chat.id, lastfm.doc, true, msg.message_id, true)
|
2016-03-22 11:16:26 +01:00
|
|
|
elseif input == '--' or input == '—' then
|
2016-04-11 06:04:47 +02:00
|
|
|
self.database.users[msg.from.id_str].lastfm = nil
|
|
|
|
bindings.sendReply(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
|
|
|
|
bindings.sendReply(self, msg, 'Your last.fm username has been set to "' .. input .. '".')
|
2015-08-04 22:11:55 +02:00
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
local url = 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&format=json&limit=1&api_key=' .. self.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-04-27 01:37:55 +02:00
|
|
|
alert = '\n\nYour username has been set to ' .. username .. '.\nTo change it, use /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-04-11 06:04:47 +02:00
|
|
|
bindings.sendReply(self, 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
|
|
|
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-04-11 06:04:47 +02:00
|
|
|
bindings.sendReply(self, msg, self.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-04-11 06:04:47 +02:00
|
|
|
bindings.sendReply(self, 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
|
|
|
|
|
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-04-29 06:12:04 +02:00
|
|
|
bindings.sendReply(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-04-29 06:12:04 +02:00
|
|
|
bindings.sendMessage(self, msg.chat.id, output)
|
2015-07-29 00:13:46 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
return lastfm
|