From 5b9761552c3d0fcfb1e2ff23b6bd3015d6c29e54 Mon Sep 17 00:00:00 2001 From: Tommy Jans Date: Fri, 15 Jan 2016 04:30:53 +0100 Subject: [PATCH 1/2] Add librefm plugin copypasta of drews lastfm --- plugins/librefm.lua | 101 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 plugins/librefm.lua diff --git a/plugins/librefm.lua b/plugins/librefm.lua new file mode 100644 index 0000000..0af0b11 --- /dev/null +++ b/plugins/librefm.lua @@ -0,0 +1,101 @@ +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 +Sets your last.fm username. Otherwise, /np will use your Telegram username. Use "/fmset -" to delete it. +```]] + +local triggers = { + '^/libre[@'..bot.username..']*', + '^/lnp[@'..bot.username..']*', + '^/lfmset[@'..bot.username..']*' +} + +local action = function(msg) + + lastfm = load_data('librefm.json') + 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 + lastfm[msg.from.id_str] = nil + sendReply(msg, 'Your last.fm username has been forgotten.') + else + lastfm[msg.from.id_str] = input + sendReply(msg, 'Your last.fm username has been set to "' .. input .. '".') + end + save_data('librefm.json', lastfm) + return + end + + local url = 'http://alpha.libre.fm/2.0/?method=user.getrecenttracks&format=json&limit=1&api_key=0&user=' + + local username + local output = '' + if input then + username = input + elseif lastfm[msg.from.id_str] then + username = lastfm[msg.from.id_str] + elseif msg.from.username then + username = msg.from.username + output = '\n\nYour username has been set to ' .. username .. '.\nTo change it, use /lfmset .' + lastfm[msg.from.id_str] = username + save_data('lastfm.json', lastfm) + else + sendReply(msg, 'Please specify your last.fm username or set it with /lfmset.') + 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 + sendReply(msg, 'Please specify your last.fm username or set it with /lfmset.') + return + end + + local jdat = jdat.recenttracks.track[1] or jdat.recenttracks.track + if not jdat then + sendReply(msg, 'No history for this user.' .. output) + return + end + + local message = input or msg.from.first_name + message = '🎵 ' .. message + + if jdat['@attr'] and jdat['@attr'].nowplaying then + message = message .. ' is currently listening to:\n' + else + message = message .. ' last listened to:\n' + end + + local title = jdat.name or 'Unknown' + local artist = 'Unknown' + if jdat.artist then + artist = jdat.artist['#text'] + end + + message = message .. title .. ' - ' .. artist .. output + sendMessage(msg.chat.id, message) + +end + +return { + action = action, + triggers = triggers, + doc = doc, + command = command +} From 840cf2f7996515df606232365a9dba77d81c141b Mon Sep 17 00:00:00 2001 From: Tommy Jans Date: Fri, 15 Jan 2016 04:34:25 +0100 Subject: [PATCH 2/2] last.fm -> librefm in bot replies --- plugins/librefm.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugins/librefm.lua b/plugins/librefm.lua index 0af0b11..e09ef31 100644 --- a/plugins/librefm.lua +++ b/plugins/librefm.lua @@ -4,7 +4,7 @@ local doc = [[``` Returns what you are or were last listening to. If you specify a username, info will be returned for that username. /lfmset -Sets your last.fm username. Otherwise, /np will use your Telegram username. Use "/fmset -" to delete it. +Sets your libre.fm username. Otherwise, /np will use your Telegram username. Use "/fmset -" to delete it. ```]] local triggers = { @@ -26,10 +26,10 @@ local action = function(msg) sendMessage(msg.chat.id, doc, true, msg.message_id, true) elseif input == '-' then lastfm[msg.from.id_str] = nil - sendReply(msg, 'Your last.fm username has been forgotten.') + sendReply(msg, 'Your libre.fm username has been forgotten.') else lastfm[msg.from.id_str] = input - sendReply(msg, 'Your last.fm username has been set to "' .. input .. '".') + sendReply(msg, 'Your libre.fm username has been set to "' .. input .. '".') end save_data('librefm.json', lastfm) return @@ -49,7 +49,7 @@ local action = function(msg) lastfm[msg.from.id_str] = username save_data('lastfm.json', lastfm) else - sendReply(msg, 'Please specify your last.fm username or set it with /lfmset.') + sendReply(msg, 'Please specify your libre.fm username or set it with /lfmset.') return end @@ -63,7 +63,7 @@ local action = function(msg) local jdat = JSON.decode(jstr) if jdat.error then - sendReply(msg, 'Please specify your last.fm username or set it with /lfmset.') + sendReply(msg, 'Please specify your libre.fm username or set it with /lfmset.') return end