Created
August 24, 2020 20:51
-
-
Save dnicolson/f0bad1c78df4230adcbb4b0f124c931d to your computer and use it in GitHub Desktop.
Function that adds YouTube video IDs to a playlist using session information from window.ytcfg.data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function addVideosToPlaylist(playlistId, videoIds) { | |
const sej = JSON.stringify({ | |
commandMetadata: { | |
webCommandMetadata: { | |
url: '/service_ajax', | |
sendPost: true, | |
apiUrl: '/youtubei/v1/browse/edit_playlist' | |
} | |
}, | |
playlistEditEndpoint: { | |
playlistId: playlistId, | |
actions: videoIds.map(vid => ({ addedVideoId: vid, action: 'ACTION_ADD_VIDEO' })) | |
} | |
}); | |
const csn = window.ytcfg.data_['client-screen-nonce']; | |
const session_token = window.ytcfg.data_['XSRF_TOKEN']; | |
const params = { | |
credentials: 'include', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'X-YouTube-Client-Name': window.ytcfg.data_['INNERTUBE_CONTEXT_CLIENT_NAME'], | |
'X-YouTube-Client-Version': window.ytcfg.data_['INNERTUBE_CONTEXT_CLIENT_VERSION'], | |
'X-YouTube-Device': window.ytcfg.data_['DEVICE'], | |
'X-Youtube-Identity-Token': window.ytcfg.data_['ID_TOKEN'], | |
'X-YouTube-Page-CL': window.ytcfg.data_['PAGE_CL'], | |
'X-YouTube-Page-Label': window.ytcfg.data_['PAGE_BUILD_LABEL'], | |
'X-YouTube-Variants-Checksum': window.ytcfg.data_['VARIANTS_CHECKSUM'], | |
}, | |
referrer: 'https://www.youtube.com/playlist?list=WL', | |
body: `sej=${sej}&csn=${csn}&session_token=${session_token}`, | |
method: 'POST', | |
mode: 'cors' | |
}; | |
const resp = await fetch('https://www.youtube.com/service_ajax?name=playlistEditEndpoint', params); | |
if (resp.status === 200) { | |
return await resp.json(); | |
} | |
return false; | |
} | |
(async () => { | |
try { | |
console.log(await addVideosToPlaylist('WL', ['<videoIds>'])); | |
} catch (err) { | |
console.error(err); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment