Skip to content

Instantly share code, notes, and snippets.

@vhugo
Last active June 2, 2017 16:08
Show Gist options
  • Save vhugo/8ce6817b6db1b0b259bfc7d13f8953f3 to your computer and use it in GitHub Desktop.
Save vhugo/8ce6817b6db1b0b259bfc7d13f8953f3 to your computer and use it in GitHub Desktop.
Script to claim free book from Packpub

USAGE

  • download ZIP
  • install requirements pip install -r requirements.txt
  • edit claim_free_title.py - Add authentication info
PACKPUB_USER = "[email protected]"
PACKPUB_PASS = "Y0urP4ssH3r3"
  • run the script python claim_free_title.py
  • edit cron crontab -e and add something like this:
0 0 * * * python ~/claim_free_title.py
#!/usr/bin/python
import requests
import re
PACKPUB_USER = "[email protected]"
PACKPUB_PASS = "Y0urP4ssH3r3"
s = requests.session()
home = s.get("https://www.packtpub.com/packt/offers/free-learning", headers={'user-agent': 'cfreebook/0.0.1'})
regxp = re.search("/freelearning-claim/\d*/\d*", home.text)
claim_link = regxp.group(0)
regxp = re.search("form_build_id.\s*id=.([^\"]+)\"", home.text)
form_build_id = regxp.group(1)
auth = s.post("https://www.packtpub.com/register", {
"email":PACKPUB_USER,
"password":PACKPUB_PASS,
"op": "Login",
"form_build_id": form_build_id,
"form_id": "packt_user_login_form"
}, headers={'user-agent': 'cfreebook/0.0.1'})
if auth.url == "https://www.packtpub.com/account":
claim_book = s.get("https://www.packtpub.com" + claim_link,
headers={'user-agent': 'cfreebook/0.0.1'})
if claim_book.url == "https://www.packtpub.com/account/my-ebooks":
print("Book has been added to your list. You current have:")
regexp = re.compile('<div class="title">\s*([^<]*)\s*<\/div>')
for title in regexp.finditer(claim_book.text):
print("- " + title.group(1))
exit(0)
else:
print("Authentication failed.")
exit(1)
print("Something went wrong. Sorry, low budget for full error handling. Check"
"your internet, DNS, user and password. If none of that keep checking"
"other stuff.")
exit(1)
requests==2.11.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment