Skip to content

Instantly share code, notes, and snippets.

@tobbbe
Last active April 27, 2020 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobbbe/c9142494f7f34abebe9c36d42f311dbb to your computer and use it in GitHub Desktop.
Save tobbbe/c9142494f7f34abebe9c36d42f311dbb to your computer and use it in GitHub Desktop.
get list of spotify links from slack channel
javascript:!function(){const SLACK_BOT_TOKEN="your slack bot token";async function get(a=[],b){let c=`https://slack.com/api/conversations.history?token=${SLACK_BOT_TOKEN}&channel=CJTD5RP7Y`;b&&(c+="&cursor="+b);const d=await fetch(c),e=await d.json();return a=[...a,...e.messages],e.has_more?await get(a,e.response_metadata.next_cursor):a}async function init(){const a=await get(),b=a.reduce((a,b)=>{try{const c=b.text.split("com/track/")[1].split("?")[0],d=`https://open.spotify.com/track/${c}`;a.arr.push(d),a.str+=`${d}\n`}catch{}return a},{arr:[],str:""});console.log(b);var c=window.open();const d=c.document.createElement("textarea");d.value=b.str,d.style="width:100%;height: 600px;font-size: 16px",c.document.body.innerHTML=`<p>${b.arr.length} tracks</p>`,c.document.body.appendChild(d)}init();}();
const SLACK_BOT_TOKEN = "your slack bot token";
async function get(prevData = [], nextId) {
let url = `https://slack.com/api/conversations.history?token=${SLACK_BOT_TOKEN}&channel=CJTD5RP7Y`;
if (nextId) {
url += "&cursor=" + nextId;
}
const resp = await fetch(url);
const data = await resp.json();
prevData = [...prevData, ...data.messages];
if (data.has_more) {
return await get(prevData, data.response_metadata.next_cursor);
}
return prevData;
}
async function init() {
const data = await get();
const spotifyLinks = data.reduce(
(acc, x) => {
try {
const trackId = x.attachments[0].original_url.split("com/track/")[1].split("?")[0];
const trackLink = `https://open.spotify.com/track/${trackId}`;
acc.arr.push(trackLink);
acc.str += `${trackLink}\n`;
} catch {}
return acc;
},
{ arr: [], str: "" }
);
console.log(spotifyLinks);
var newWindow = window.open();
const el = newWindow.document.createElement("textarea");
el.value = spotifyLinks.str;
el.style = "width:100%;height: 600px;font-size: 16px";
newWindow.document.body.innerHTML = `<p>${
spotifyLinks.arr.length
} tracks</p>`;
newWindow.document.body.appendChild(el);
}
init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment