This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot-2/miku/plugins/spotify.lua

53 lines
1.5 KiB
Lua
Raw Normal View History

local spotify = {}
local https = require('ssl.https')
local json = require('dkjson')
local utilities = require('miku.utilities')
spotify.triggers = {
"open.spotify.com/track/([A-Za-z0-9-]+)",
"play.spotify.com/track/([A-Za-z0-9-]+)"
}
local BASE_URL = 'https://api.spotify.com/v1'
function spotify:get_track_data(track)
local url = BASE_URL..'/tracks/'..track
local res,code = https.request(url)
if code ~= 200 then return nil end
local data = json.decode(res)
return data
end
function spotify:send_track_data(data, self, msg)
local name = data.name
local album = data.album.name
local artist = data.artists[1].name
local preview = data.preview_url
local milliseconds = data.duration_ms
-- convert s to mm:ss
local totalseconds = math.floor(milliseconds / 1000)
local duration = makeHumanTime(totalseconds)
local text = '*'..name..'* von *'..artist..'* aus dem Album *'..album..'* _('..duration..')_'
if preview then
utilities.send_typing(self, msg.chat.id, 'upload_audio')
local file = download_to_file(preview, name..'.mp3')
utilities.send_audio(self, msg.chat.id, file, msg.message_id, totalseconds, artist, name)
return
else
utilities.send_reply(self, msg, text, true)
return
end
end
function spotify:action(msg, config, matches)
local data = spotify:get_track_data(matches[1])
if not data then utilities.send_reply(self, msg, config.errors.connection) return end
spotify:send_track_data(data, self, msg)
return
end
return spotify