Skip to content

Instantly share code, notes, and snippets.

@hgouveia
Created April 11, 2017 06:45
Show Gist options
  • Save hgouveia/ba7eda9db9a4158d973a0e5fb9389bca to your computer and use it in GitHub Desktop.
Save hgouveia/ba7eda9db9a4158d973a0e5fb9389bca to your computer and use it in GitHub Desktop.
Script to redeem automatically books from `https://www.packtpub.com/packt/offers/free-learning` using bash and cronjob
#!/bin/bash
#==INSTRUCTIONS
# - Add EMAIL and PASS infomation
# - Replace script path with your path `/home/USER/scripts/` in the script
# - Check your machine timezone `date +"%Z %z"` output ex: UTC +0000
# - patckpub.com is UTC +0000, find the best hour for you to use in the crontab
# - Execute `crontab -e`
# - Add `0 1 * * * /bin/sh /home/USER/scripts/packtpub_redeem.sh > /home/USER/scripts/packtpub_redeem.out` , this mean everyday 1am
# - Restart Service `sudo service cron reload` (optional, crontab -e, will reload)
# - Add Permission `chmod 755 /home/USER/scripts/packtpub_redeem.sh`
#==
COOKIE="cookie.txt"
EMAIL=""
PASS=""
#Change dir to script folder
cd /home/USER/scripts/
#Login and store Cookie
curl --silent -X POST -c ${COOKIE} \
-d "mail=${EMAIL}&password=${PASS}" \
https://www.packtpub.com/api/user/login > login.json
#Get Claim URL
PACKT_CLAIM_URL=$(
curl --silent https://www.packtpub.com/packt/offers/free-learning | \
grep freelearning-claim | \
sed 's/\t//g' | \
awk '{print "https://www.packtpub.com"$2}' | \
sed -e 's/href\=\"//g' -e 's/.$//g'
)
#Claim book using the Cookies from previous login and the extracted Claim URL
#https://www.packtpub.com/freelearning-claim/XXX/YYY
curl --silent -L -X GET -b ${COOKIE} ${PACKT_CLAIM_URL} > output.html
@johand
Copy link

johand commented Apr 16, 2017

@hgouveia puedes crear una variable con whoami para no tener que reemplazar de forma manual el path hacia el home del usuario, por ejemplo

USER=$(whoami)

cd /home/$USER/scripts/

@hgouveia
Copy link
Author

@johand , la idea es que el usuario coloque el script donde quiera, y solo modifique la ruta completa en esa linea

Si añado lo que me comentas, el user sera, root ,que es el usuario por defecto del cronjob, otra forma mas directa, es usar cd ~/scripts y tendria el mismo efecto, pero sigue teniendo el problema del usuario sera root

Una solucion seria especificarlo en el crontab crontab -u myuser -e o especificarlo directamente en el comando

#<timing> <user> <command>
0 1 * * * myuser /bin/sh ~/scripts/packtpub_redeem.sh > ~/scripts/packtpub_redeem.out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment