Download playlists from plug.dj

Off topicProgramming → Download playlists from plug.dj

Plug.dj is great right? Yeah, but it requires an internet connection. Sometimes i just want to listen to my songs on my phone… Or in the car without having to worry about internet connectivity. So i did some googling yesterday finished it today.

First off make sure you have youtube-dl installed. If you don’t then you can “sudo apt-get install youtube-dl” to get it. Now to do the conversion to mp3 we need this package : libav-tools wich you can get with the same command “sudo apt-get install libav-tools”

Great so thats done. Now we need to get a list of youtube ids for your playlist, i found this code on reddit:

/**
 * @author thedark1337
 * @author TAT
 * @description Gets active playlist from plug.dj and returns a dialog box of all youtube media ID's.
 */
! function() {
  var d = require.s.contexts._.defined,
      Dialog = function () {
        for (var i in d) if (d.hasOwnProperty(i) && d[i] && d[i].prototype && _.isFunction(d[i].prototype.onContainerClick)) return d[i]
      }(),
      Events = function () {
        for (var i in d) if (d.hasOwnProperty(i) && d[i] && _.isFunction(d[i].dispatch) && d[i].dispatch.length === 1) return d[i]
      }(),
      ShowDialogEvent = function () {
        for (var i in d) if (d.hasOwnProperty(i) && d[i] && d[i]._name === 'ShowDialogEvent') return d[i]
      }();
    var r = Dialog.extend({
        id: "dialog-plug2youtube",
        className: "dialog",
        initialize: function() {
            this._super()
        },
        render: function() {
            var e = [],
                t = this;
            $.getJSON("https://plug.dj/_/playlists", function(a) {
                if ("ok" !== a.status) return API.chatLog("Error, playlists not obtainable due to server error.");
                if (!(a.data && a.data.length > 0)) return API.chatLog("Error, it seems you might not have playlists. Please try again.");
                for (var r = a.data, i = r.length, n = 0; i > n; n++) {
                    var o = r[n];
                    o.active && $.getJSON("https://plug.dj/_/playlists/" + o.id + "/media", function(a) {
                        if ("ok" !== a.status) return API.chatLog("Error getting playlist media. Please try again.");
                        for (var r = a.data, i = r.length, n = 0; i > n; n++) {
                            var o = r[n];
                            1 === o.format && e.push(o.cid)
                        }
                        return e && e.length > 0 ? (t.$el.append(t.getHeader("plug2youtube")).append(t.getBody().append($("<span/>").text(e.join(", ")))), t.$el.css({
                            width: "90%",
                            padding: "1%"
                        })) : void 0
                    })
                }
            })
        },
        close: function() {
            this._super()
        }
    });
    Events.dispatch(new ShowDialogEvent(ShowDialogEvent.SHOW, new r))
}();

Paste this code into the console which you can get to by right clicking on the plugdj page and selecting inspect element.

This will popup a list of ids copy paste them into a file called list.txt located wherever you want your songs to end up.

Now create a python file named whatever you want.py

import os

file = open('list.txt','r')
contents = list(file.read())
link = "https://youtu.be/"
finishedLinks = []
for char in contents:
	if char != ",":
		if char != " ":
			link += char
	else:
		finishedLinks.append(link)
		link = "https://youtu.be/"

file.close()

for link in finishedLinks:
	os.system("youtube-dl --extract-audio --audio-format mp3 "+link)


Paste that code in and save Now run the python file and enjoy.

You’re welcome, needless to say this only works on linux, however if you get the dependencies installed on windows and install python it “should” work.
I have Linux Mint on my laptop… Luv it :)
Oh on that note, the package for youtube-dl is outdated on aptitute, i suggest you use pip to install it. Or some videos will not download