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-06-07 06:31:34 +02:00
|
|
|
local utilities = require('otouto.utilities')
|
2016-04-11 06:04:47 +02:00
|
|
|
|
2016-05-27 02:26:30 +02:00
|
|
|
function lastfm:init(config)
|
2016-08-14 04:26:44 +02:00
|
|
|
assert(config.lastfm_api_key,
|
|
|
|
'lastfm.lua requires a last.fm API key from http://last.fm/api.'
|
|
|
|
)
|
2016-03-04 19:06:13 +01:00
|
|
|
|
2016-05-27 05:28:44 +02:00
|
|
|
lastfm.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('lastfm', true):t('np', true):t('fmset', true).table
|
2016-07-25 11:03:35 +02:00
|
|
|
lastfm.doc = config.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-07-25 11:03:35 +02:00
|
|
|
]] .. config.cmd_pat .. [[fmset <username>
|
|
|
|
Sets your last.fm username. Otherwise, ]] .. config.cmd_pat .. [[np will use your Telegram username. Use "]] .. config.cmd_pat .. [[fmset --" to delete it.]]
|
2016-05-27 05:28:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
lastfm.command = 'lastfm'
|
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)
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
local from_id_str = tostring(msg.from.id)
|
|
|
|
self.database.userdata[from_id_str] = self.database.userdata[from_id_str] or {}
|
2015-08-04 22:11:55 +02:00
|
|
|
|
2016-05-27 05:28:44 +02:00
|
|
|
if string.match(msg.text, '^'..config.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 05:28:44 +02:00
|
|
|
elseif string.match(msg.text, '^'..config.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
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
self.database.userdata[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
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
self.database.userdata[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
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
elseif self.database.userdata[from_id_str].lastfm then
|
|
|
|
username = self.database.userdata[from_id_str].lastfm
|
2015-11-25 03:22:04 +01:00
|
|
|
elseif msg.from.username then
|
|
|
|
username = msg.from.username
|
2016-05-27 05:28:44 +02:00
|
|
|
alert = '\n\nYour username has been set to ' .. username .. '.\nTo change it, use '..config.cmd_pat..'fmset <username>.'
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
self.database.userdata[from_id_str].lastfm = username
|
2015-11-25 03:22:04 +01:00
|
|
|
else
|
2016-05-27 05:28:44 +02:00
|
|
|
utilities.send_reply(self, msg, 'Please specify your last.fm username or set it with '..config.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 05:28:44 +02:00
|
|
|
utilities.send_reply(self, msg, 'Please specify your last.fm username or set it with '..config.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
|