Skip to content

Instantly share code, notes, and snippets.

@lethak
Created March 17, 2020 15:36
Show Gist options
  • Save lethak/2c10775de237d791fd4595d28b8e7c2f to your computer and use it in GitHub Desktop.
Save lethak/2c10775de237d791fd4595d28b8e7c2f to your computer and use it in GitHub Desktop.
Download AudioAddict music as it get played.
// ==UserScript==
// @name DIDownloadUserscript for AudioAddict platforms
// @namespace LTKDIFMDU
// @author LethaK Maas
// @license MIT
// @description Download AudioAddict music as it get played.
// @include https://*.di.fm*
// @include https://*.classicalradio.com*
// @include https://*.radiotunes.com*
// @include https://*.jazzradio.com*
// @include https://*.rockradio.com*
// @include https://*.zenradio.com*
// @noframes
// @inject-into page
// @grant GM_download
// @run-at document-idle
// @version 0.0.1
// ==/UserScript==
(function() {
// Defining our event listener
function onWebplayerTrackChange (e) {
// 'e' is a BackboneJS Model: @see https://cdnjs.com/libraries/backbone.js/tutorials/what-is-a-model
// creating a payload variable with track data
const payload = e ? {
id: e.get('id') ? e.get('id') : null,
src: e.get('src') ? 'https:' + e.get('src') : null,
display_artist: e.get('display_artist') ? e.get('display_artist') : null,
display_title: e.get('display_title') ? e.get('display_title') : null,
assets: e.get('content') ? (e.get('content').assets ? e.get('content').assets : null) : null
} : null;
/*
if 'payload' is null, no valid track was loaded.
'payload.id' and 'payload.src' can be used there for whatever purpose.
Since this event technically can be triggered multiple times with the same track,
the id can be used to dedupe whatever you are doing.
'src' is the track download url but without the full protocol (only //),
it is advised to use https whenever possible. This URL is only valid for 24h hours and query parameters should not be modified because of the oauth signature.
This also means this url is broadcasting your DI identity to the content server (DI user id and IP most likely)
if other arrtibutes are required, dig into 'e.attributes' and access them with 'e.get('something').somethingElse'.
Remember that any value could be non existant or null in e.attributes, always check what you are using is there or it will trigger exceptions.
'assets' will contains details about multiple url available, typically one asset for each available quality.
If you need the best quality, you can dig into 'di.app.options.stream_set_configs' to find out who is what.
*/
console.warn('[DIDownloadUserscript] onWebplayerTrackChange just triggered with payload = ', payload)
GM_download({
url: payload.src,
name: 'DIDU_' + payload.id + '_ ' + payload.display_artist + ' - ' + payload.display_title + '.mp4',
saveAs: false,
})
}
// Registering our new event listener to one of DI's event bus. Obviously 'di.app.vent' must to be accessible, typically after DOM is ready.
di.app.vent.on('webplayer:track:change', onWebplayerTrackChange)
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment