Last active
March 4, 2018 14:48
-
-
Save juzim/8d0fb06feb5f58d06f07e360f85de7c0 to your computer and use it in GitHub Desktop.
Create wget commands for all claimed books on packtpub
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
/* | |
== Adapted from the code over at https://gist.github.com/graymouser/a33fbb75f94f08af7e36 == | |
Log into your account at packtpub.com and save the cookies with the "cookies.txt" Chrome extension or the "Export cookies" Firefox extension into the file cookies.txt. | |
Then open the console in your browsers dev tools and paste the following code. | |
You will get a list of wget commands that you can copy and paste as a whole into a terminal to download all books. | |
Example: wget --load-cookies=cookies.txt --content-disposition "https://packtpub.com//ebook_download/20217/mobi" -O "R Data Visualization Cookbook.mobi" | |
If you only want some filetypes, edit the "pattern" vaiable accordingly. | |
wget for Windows: https://eternallybored.org/misc/wget/ | |
wget for OS-X: http://osxdaily.com/2012/05/22/install-wget-mac-os-x/ | |
*/ | |
var pattern = /(MOBI|EPUB|PDF)$/i; | |
var downloadCmd = ''; | |
var titleNodes = document.getElementsByClassName('product-line'); | |
var name, rawName, id, titleById = {}, idPattern = /ebook_download\/(\d+)\/(.*)/i; | |
for (i = 0; i < titleNodes.length; i++) { | |
rawName = titleNodes[i].getAttribute('title'); | |
if (rawName) { | |
name = rawName.trim().replace(' [eBook]', ''); | |
id = titleNodes[i].getAttribute('nid'); | |
titleById[id] = name; | |
} | |
} | |
function download(url, id, ext, i) { | |
return 'wget --load-cookies=cookies.txt --content-disposition "' + "https://packtpub.com/" + url + "\"" + " -O \"" + titleById[id] + "." + ext + "\"\n"; | |
} | |
for (i in nodes) { | |
var a = nodes[i]; | |
if (a && a.text && pattern.test(a.text.trim())) { | |
url = a.attributes['href'].value; | |
meta = idPattern.exec(url) | |
downloadCmd += download(url, meta[1], meta[2], i); | |
} | |
} | |
console.log(downloadCmd); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment