Guest User

Untitled

a guest
Oct 21st, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. async function sendToPlaylist(selectedListens) {
  2.   const playlistMbid = '1bf2a0c7-0fb9-459d-9b83-71da363f4a93';  // Your provided playlist MBID
  3.  
  4.   const JSPFPlaylistData = {
  5.     playlist:{
  6.       track: selectedListens.map(recordingMBID => {
  7.         return {identifier: [`https://musicbrainz.org/recording/${recordingMBID}`]}
  8.       })
  9.     }
  10.   }
  11.   try {
  12.     const response = await fetch(`https://api.listenbrainz.org/1/playlist/${playlistMbid}/item/add`, {
  13.       method: 'POST',
  14.       headers: {
  15.         'Authorization': `Token ${listenBrainzToken}`,
  16.         'Content-Type': 'application/json',
  17.       },
  18.       body: JSON.stringify(JSPFPlaylistData)
  19.     });
  20.  
  21.     if (response.ok) {
  22.       alert('Successfully added to playlist!');
  23.     } else {
  24.       const responseText = await response.text(); // Get the error text if available
  25.       console.error("Response Error Text:", responseText);  // Log the error text
  26.       throw new Error(`Failed to add to playlist: ${response.status} - ${responseText}`);
  27.     }
  28.   } catch (error) {
  29.     alert(`Error: ${error.message}`);
  30.     console.error('Error:', error);
  31.   }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment