Skip to content

Instantly share code, notes, and snippets.

@MarioRicalde
Last active November 30, 2020 23:43
Show Gist options
  • Save MarioRicalde/8e9fd16d9405c7fdbff606727c4d9a0a to your computer and use it in GitHub Desktop.
Save MarioRicalde/8e9fd16d9405c7fdbff606727c4d9a0a to your computer and use it in GitHub Desktop.

Zapier YouTube Playlist to Spotify Playlist sync

Actions:

  1. Trigger: New Video in Playlsit in YouTube
  2. Action: Run javascript (below)
  3. Action: Find Track in Spotify (using artist and track from previous step)
  4. Action: Add Track to Playlist in Spotify (id passed from previous step )
// const fetch = require("node-fetch")
// Stubbing
// inputData = {}
// inputData.title = "Dua Lipa - Physical ( Official Music Video )"
// inputData.title = "Physical by Dua Lipa ( Official Music Video )"
// inputData.title = "Physical ( Official Music Video )"
// inputData.id = "XaSVkb_XLt4"
// function callback(a, b) {
// console.log([a,b])
// }
function filterTerms(str) {
var tl = TERMS.length
for (let i = 0; i < tl; i++) {
if (str.indexOf(TERMS[i]) != -1) {
var removeIndex = (str.indexOf(TERMS[i]))
var removeString = (str.slice(removeIndex))
str = str.replace(removeString, '')
}
}
return str.trim()
}
const title = inputData.title
const id = inputData.id
const playlist = "07oLcAl9gzEocmosnBqgLh"
const SEPARATOR = "<>"
const TERMS = ['ft. ', 'ft ', 'feat. ', 'feat ', 'featuring ', '(', '[']
let curated = title.replace(/([\-\:]| by )/g, SEPARATOR).split(SEPARATOR)
let curatedTitle = curated.length == 2
if (curatedTitle) {
let alternateSeparator = title.match(/ by /gi)
let filteredSegment = filterTerms(curated[1].trim())
if (alternateSeparator) {
callback(null, [{ track: curated[0].trim(), artist: filteredSegment}])
} else {
callback(null, [{ track: filteredSegment, artist: curated[0].trim() }])
}
} else {
// No terms, attempt to use Channel Name
fetch(`https://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=${id}&format=json`)
.then(response => response.json())
.then(data => {
callback(null, [{ track: filterTerms(title), artist: data.author_name.replace(/\- Topic/i, '') }])
}).catch(error => { console.log(error) })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment