Spotify integrations using following possible links/syntaxes:

- https://open.spotify.com/track/34AWo71Ya5gq7wpNnatwr7
- spotify:track:34AWo71Ya5gq7wpNnatwr7

Works with tracks, albums, artists and playlists.
pull/904/head
kakawait 10 years ago
parent 8183b5423c
commit 4f137830a7
  1. 1
      .meteor/packages
  2. 1
      .meteor/versions
  3. 13
      packages/rocketchat-oembed/client/baseWidget.coffee
  4. 13
      packages/rocketchat-oembed/client/oembedSpotifyWidget.html
  5. 1
      packages/rocketchat-oembed/package.js
  6. 22
      packages/rocketchat-spotify/package.js
  7. 48
      packages/rocketchat-spotify/spotify.coffee

@ -41,6 +41,7 @@ rocketchat:oembed
rocketchat:slashcommands-invite
rocketchat:slashcommands-join
rocketchat:slashcommands-leave
rocketchat:spotify
rocketchat:statistics
rocketchat:webrtc
#rocketchat:livechat

@ -120,6 +120,7 @@ rocketchat:oembed@0.0.1
rocketchat:slashcommands-invite@0.0.1
rocketchat:slashcommands-join@0.0.1
rocketchat:slashcommands-leave@0.0.1
rocketchat:spotify@0.0.1
rocketchat:statistics@0.0.1
rocketchat:webrtc@0.0.1
rocketchat:wordpress@0.0.1

@ -1,3 +1,11 @@
Template.registerHelper 'replace', (source, find, replace, useRegex) ->
if useRegex is true
find = new RegExp(find)
return source.replace(find, replace)
Template.registerHelper 'match', (source, regex) ->
return new RegExp(regex).test(source)
Template.oembedBaseWidget.helpers
template: ->
# console.log this
@ -10,4 +18,7 @@ Template.oembedBaseWidget.helpers
if this.parsedUrl?.host is 'www.youtube.com' and this.meta?.twitterPlayer?
return 'oembedYoutubeWidget'
return 'oembedUrlWidget'
if this.parsedUrl?.host is 'open.spotify.com' and this.meta?.ogAudio?
return 'oembedSpotifyWidget'
return 'oembedUrlWidget'

@ -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>

@ -23,6 +23,7 @@ Package.onUse(function(api) {
api.addFiles('client/oembedAudioWidget.html', 'client');
api.addFiles('client/oembedYoutubeWidget.html', 'client');
api.addFiles('client/oembedSpotifyWidget.html', 'client');
api.addFiles('client/oembedUrlWidget.html', 'client');
api.addFiles('client/oembedUrlWidget.coffee', 'client');

@ -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…
Cancel
Save