- https://open.spotify.com/track/34AWo71Ya5gq7wpNnatwr7 - spotify:track:34AWo71Ya5gq7wpNnatwr7 Works with tracks, albums, artists and playlists.pull/904/head
parent
8183b5423c
commit
4f137830a7
@ -0,0 +1,13 @@ |
||||
<template name="oembedSpotifyWidget"> |
||||
{{#if parsedUrl}} |
||||
<blockquote> |
||||
<a href="https://www.spotify.com" style="color: #9e9ea6">Spotify</a><br/> |
||||
{{#if match meta.ogAudio "spotify:artist:\\S+"}} |
||||
<a href="{{url}}">{{{meta.ogTitle}}}</a><br/> |
||||
{{else}} |
||||
<a href="{{url}}">{{{replace meta.ogDescription ", an? (?:song|album) by (.+?) on Spotify" " - $1" true}}}</a><br/> |
||||
{{/if}} |
||||
<iframe width="300" height="380" src="https://embed.spotify.com/?uri={{meta.ogAudio}}" frameborder="0"></iframe><br/> |
||||
</blockquote> |
||||
{{/if}} |
||||
</template> |
@ -0,0 +1,22 @@ |
||||
Package.describe({ |
||||
name: 'rocketchat:spotify', |
||||
version: '0.0.1', |
||||
summary: 'Message pre-processor that will translate spotify on messages', |
||||
git: '' |
||||
}); |
||||
|
||||
Package.onUse(function(api) { |
||||
api.versionsFrom('1.0'); |
||||
|
||||
api.use([ |
||||
'coffeescript', |
||||
'underscore', |
||||
'rocketchat:lib@0.0.1' |
||||
]); |
||||
|
||||
api.addFiles('spotify.coffee', ['server','client']); |
||||
}); |
||||
|
||||
Package.onTest(function(api) { |
||||
|
||||
}); |
@ -0,0 +1,48 @@ |
||||
### |
||||
# Spotify a named function that will process Spotify (ex: spotify:track:1q6IK1l4qpYykOaWaLJkWG) |
||||
# @param {Object} message - The message object |
||||
### |
||||
|
||||
class Spotify |
||||
process = (message, source, callback) -> |
||||
if _.trim source |
||||
# Separate text in code blocks and non code blocks |
||||
msgParts = source.split(/(```\w*[\n\ ]?[\s\S]*?```+?)/) |
||||
|
||||
for part, index in msgParts |
||||
# Verify if this part is code |
||||
codeMatch = part.match(/```(\w*)[\n\ ]?([\s\S]*?)```+?/) |
||||
if not codeMatch? |
||||
callback message, msgParts, part, index |
||||
|
||||
@transform: (message) -> |
||||
urls = [] |
||||
if Array.isArray message.urls |
||||
urls = urls.concat message.urls |
||||
|
||||
changed = false |
||||
|
||||
process message, message.msg, (message, msgParts, part) -> |
||||
re = /spotify:([^:]+):(\S+)/g |
||||
while match = re.exec(part) |
||||
url = "https://open.spotify.com/" + _.escape match[1] + "/" + _.escape match[2] |
||||
urls.push {'url': url} |
||||
changed = true |
||||
|
||||
# Re-mount message |
||||
if changed |
||||
message.urls = urls |
||||
|
||||
return message |
||||
|
||||
@render: (message) -> |
||||
process message, message.html, (message, msgParts, part, index) -> |
||||
msgParts[index] = part.replace /(^|\s)spotify:([^:]+):(\S+)(\s|$)/g, (match, p1, p2, p3, p4) -> |
||||
url = 'https://open.spotify.com/' + _.escape p2 + '/' + _.escape p3 |
||||
return p1 + '<a href="' + url + '" target="_blank">spotify:' + p2 + ':' + p3 + '</a>' + p4 |
||||
message.html = msgParts.join('') |
||||
|
||||
return message |
||||
|
||||
RocketChat.callbacks.add 'beforeSaveMessage', Spotify.transform, RocketChat.callbacks.priority.LOW |
||||
RocketChat.callbacks.add 'renderMessage', Spotify.render |
Loading…
Reference in new issue