lmiranda
6/7/2019 - 6:45 PM

Script to automatically download free ebooks listed on oreilly

Script to automatically download free ebooks listed on oreilly

/* 
Just paste into your console.. or make a tampermonkey/greasemonkey script out of it.
Only tested in Chrome. Be careful.. three tabs are opened per book.

If a books image is not removed, you'll need to try to download that book yourself.

Use this script (enter into the console of your browser) on the following pages:

http://www.oreilly.com/programming/free/
http://www.oreilly.com/business/free/
http://www.oreilly.com/data/free/
http://www.oreilly.com/iot/free/
http://www.oreilly.com/security/free/
http://www.oreilly.com/web-platform/free/
http://www.oreilly.com/webops-perf/free/

*/

var jq = document.createElement('script');
jq.src = "https://code.jquery.com/jquery-latest.min.js";
jq.onload= function() {
    var $downloadableBooks = $('a[href*="/free/"]').filter(function(i, el){ 
        return $(el).find('img').length > 0;
    });
    
    var wgetList = '';
    $downloadableBooks.each(function (i, el) {
        var link = $(el).attr('href').split('?')[0];
        if (link.indexOf('.csp') < 0) {
            return;
        }
        $(el).remove();
        link = link.split('.');
        link.splice(-1,1);
        $(['mobi', 'epub', 'pdf']).each(function(i2, format) {
            var bookUrl = [].concat(link, format).join('.').replace('/free/', '/free/files/');
            wgetList += 'wget ' + bookUrl + '\n';
            
            window.open(bookUrl);
        });
    });
    console.log(wgetList);
}
document.getElementsByTagName('head')[0].appendChild(jq);