From 3b3fd424ebb84ff0b1398f491af6581ed2d37318 Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Thu, 7 Jul 2016 13:31:38 +0200 Subject: [PATCH] Added Python script to claim PACKT's free ebook of the day. --- CONFIG.example | 4 ++++ packtclaim.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100755 packtclaim.py diff --git a/CONFIG.example b/CONFIG.example index efc6229..53685c8 100644 --- a/CONFIG.example +++ b/CONFIG.example @@ -12,3 +12,7 @@ RUNTASTIC_USER_URL="markus-birth" # Create a new app on https://pushover.net/apps/build and enter API Token here RUNTASTIC_PUSHOVER_TOKEN="0123456...PushOver App Token...6789abcdef" + +# Login for packtclaim.py +PACKT_LOGIN = anon@example.org +PACKT_PASSWORD = 1234567 diff --git a/packtclaim.py b/packtclaim.py new file mode 100755 index 0000000..9aafc49 --- /dev/null +++ b/packtclaim.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# Needs grab, pip3 install -U grab + +import configparser +import logging +from grab import Grab +from io import StringIO + +config_str = '[DEFAULT]\n' + open('CONFIG', 'r').read() +config_fp = StringIO(config_str) + +c = configparser.RawConfigParser() +c.readfp(config_fp) + +logging.basicConfig(level=logging.DEBUG) + +g = Grab() +g.setup(follow_location=True) +g.setup(follow_refresh=True) +g.go('https://www.packtpub.com/packt/offers/free-learning') +g.doc.save('/tmp/free-learning.html') +g.doc.choose_form(id='packt-user-login-form') +login = c.get('DEFAULT', 'PACKT_LOGIN') +print("Logging in with account: {}".format(login)) +g.doc.set_input('email', c.get('DEFAULT', 'PACKT_LOGIN')) +g.doc.set_input('password', c.get('DEFAULT', 'PACKT_PASSWORD')) +g.doc.submit() +g.doc.save('/tmp/free-learning-after-login.html') +g.doc.text_assert('"sid":') +g.doc.text_assert('Claim Your Free eBook') +claim_url = g.doc.select('//a[@class="twelve-days-claim"]/@href').text() +print("Claim URL: " + claim_url) +g.go(claim_url) +g.doc.save('/tmp/free-learning-after-claim.html') +g.doc.text_assert('

My eBooks

') +print("Claim successful.")