Skip to content

Instantly share code, notes, and snippets.

@bahmanm
Created June 24, 2015 00:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bahmanm/626e8932de6ea3611c5f to your computer and use it in GitHub Desktop.
Save bahmanm/626e8932de6ea3611c5f to your computer and use it in GitHub Desktop.
/**
* @author Bahman Movaqar <Bahman AT BahmanM.com>
*/
@Grab('org.jsoup:jsoup:1.8.2')
import static org.jsoup.Jsoup.parse
def cookieManager = new CookieManager(null, CookiePolicy.ACCEPT_ALL)
CookieHandler.setDefault(cookieManager)
def doc = parse(
new URL('https://www.packtpub.com/packt/offers/free-learning').text
)
2.times { // a weird hack! to make this work on slow connections
doLogin(
loginParams(doc.select('input[type=hidden][id^=form][value^=form]')?.val())
)
}
claimBook(
doc.select('a.twelve-days-claim').attr('href'),
CookieHandler.getDefault().cookieStore.cookies
)
println('Claimed! Login to Packt website to download the book.')
///////////////////////////////////////////////////////////////////////////////
def loginParams(formBuildId) {
[
email: 'YOUR_EMAIL_ADDRESS',
password: 'YOUR_PASSWORD',
op: 'Login',
form_id: 'packt_user_login_form',
form_build_id: formBuildId ?: ''
].findAll { k, v -> v }.collect { k, v ->
"$k=${URLEncoder.encode(v, 'UTF8')}"
}.join('&')
}
///////////////////////////////////////////////////////////////////////////////
def doLogin(loginParams) {
new URL(
'https://www.packtpub.com/packt/offers/free-learning'
).openConnection().with {
requestMethod = 'POST'
setRequestProperty('Content-Type', 'application/x-www-form-urlencoded')
doOutput = true
doInput = true
allowUserInteraction = true
outputStream.withWriter { w -> w << loginParams }
connect()
if (parse(inputStream.text).select('div.error'))
throw new Exception("Failed to login.")
}
}
///////////////////////////////////////////////////////////////////////////////
def claimBook(bookUrl, cookies) {
new URL(
"https://www.packtpub.com${bookUrl}"
).openConnection().with {
requestMethod = 'GET'
cookies.each { setRequestProperty('Cookie', it.toString()) }
connect()
content
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment