Actions:
- Trigger: New Video in Playlsit in YouTube
- Action: Run javascript (below)
- Action: Find Track in Spotify (using artist and track from previous step)
- 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) }) | |
} |