Skip to content

Instantly share code, notes, and snippets.

@jimmed
Last active August 29, 2015 14:06
Show Gist options
  • Save jimmed/cec53862bd832f337e12 to your computer and use it in GitHub Desktop.
Save jimmed/cec53862bd832f337e12 to your computer and use it in GitHub Desktop.
Duplicate finder for google play music

Instructions

  1. Copy the code above (I recommend clicking the 'Raw' button to get at it more easily)
  2. Open all your music
  3. Zoom out as far as you can.
  4. Open up the Menu > Tools > Developer > JavaScript Console
  5. Paste in the code, and hit Enter.
// Configuration!
var numericKeys = ['track', 'play-count'];
var matchKeys = ['title','album','artist'];
var caseSensitive = false;
// Don't change anything below here...
var hasJquery = typeof $ !== 'undefined' && $.jQuery;
if(!hasJquery) {
console.info('Waiting for jQuery to load...');
(function(){var d=document;var h=d.getElementsByTagName('head')[0];var s=d.createElement('script');s.src='//code.jquery.com/jquery-2.1.0.min.js';h.appendChild(s);}());
}
setTimeout(function() {
hasJquery = typeof $ !== 'undefined' && $.jQuery;
console.info('Finding duplicates...');
function getSongsFromView() {
var songs = [];
$('.song-row').each(function(i, x) {
var song = { id: $(x).data('id'), row: $(x) };
$('[data-col]', x).each(function(i, x) {
var $x = $(x);
var val = $x.text().trim();
var key = $x.data('col');
if(numericKeys.indexOf(key) > -1) {
val = parseInt(val, 10);
if(isNaN(val)) {
val = 0;
}
}
song[key] = val;
});
songs.push(song);
});
return songs;
}
function getDuplicates(songs, keys, caseSensitive) {
var seen = [];
keys = keys && keys.length ? keys : ['title'];
return songs.reduce(function(dupes, curr) {
seen.forEach(function(prev) {
if(songsMatchOn(curr, prev, keys)) {
curr._duplicateOf = prev;
dupes.push(curr);
}
});
seen.push(curr);
return dupes;
}, []);
}
function songsMatchOn(a, b, keys, caseSensitive) {
return keys.every(function(key) {
if(caseSensitive || typeof a[key] !== 'string' || typeof b[key] !== 'string') {
return a[key] === b[key];
}
return a[key].toLowerCase() === b[key].toLowerCase();
});
}
function enqueueClick(song, index) {
var target = $('[data-col="title"]', song.row).eq(0)[0];
var myEvent = document.createEvent('MouseEvents');
myEvent.initMouseEvent('click', true, true, target, 0, 0, 0, 1, 1, index > 0, false, false, false, 0, null);
target.dispatchEvent(myEvent);
}
var visibleSongs = getSongsFromView();
var dupes = getDuplicates(visibleSongs, matchKeys, caseSensitive);
if(dupes.length) {
console.group('Found', dupes.length, 'duplicates, matching on ', matchKeys.join('/'));
dupes.forEach(function(song, index) {
console.log(index, song);
enqueueClick(song, index);
});
console.groupEnd();
} else {
console.info('No duplicates found!');
}
}, hasJquery ? 1 : 10000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment