-
-
Save coybit/f414b7e76aa5ebf620d3 to your computer and use it in GitHub Desktop.
Bash script to download all issues of The MagPi.
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
| #!/bin/bash | |
| # | |
| # Creates a directory named "magpi" on your home folder and all issues are downloaded there. | |
| # It also checks if you already have some of the issues and skips them from downloading. | |
| # Use argument: --log OFF to disable logging. It is there because I have this script on my crontab and I needed to monitor if it running properly. For enabling logging do: | |
| # $ sudo touch /var/log/magpi.log | |
| # $ sudo chmod 666 /var/log/magpi.log | |
| # That's it. | |
| # | |
| # Command execution is: | |
| # magpi.sh --log [ON|OFF] | |
| # | |
| # | |
| # | |
| ## GLOBALS | |
| NUMBER=1 | |
| MAGPIDIR=`pwd`"/magpi" | |
| LOG="/var/log/magpi.log" | |
| FLOG=$2 | |
| URL="" | |
| ## FUNCTIONS | |
| function log { | |
| echo "[`date`] $1" >> $LOG | |
| } | |
| function fetch { | |
| if [ ! -f "The-MagPi-issue-$1-en.pdf" ]; then | |
| if [ "$FLOG" == "ON" ]; then | |
| log "Downloading The MagPi Issue $1" | |
| fi | |
| URL="https://www.raspberrypi.org/magpi-issues/MagPi$(printf "%02d" $1).pdf" | |
| echo "Downloading The MagPi Issue $URL" | |
| curl -o "The-MagPi-issue-$1-en.pdf" $URL | |
| else | |
| if [ "$FLOG" == "ON" ]; then | |
| log "Already has MagPi issue $1" | |
| fi | |
| fi | |
| #wget --tries=5 --timeout=20 --wait=1 --waitretry=1 | |
| } | |
| ## MAIN | |
| if [ ! "$1" == "--log" ]; then | |
| echo "Command syntax is: $0 --log [ON|OFF]" | |
| exit 1 | |
| fi | |
| if [ ! $2 ]; then | |
| echo "Command syntax is: $0 --log [ON|OFF]" | |
| exit 1 | |
| fi | |
| ## Check if the directory for storing exists, if not it creates it | |
| if [ ! -d "$MAGPIDIR" ]; then | |
| mkdir $MAGPIDIR | |
| fi | |
| ## Change to that directory | |
| cd $MAGPIDIR | |
| ## Calculate number of issues so far (aprox) | |
| ISSUE=0 | |
| declare -i MONTH=5 | |
| declare -i YEAR=2012 | |
| declare -i CMONTH=`date +%m` | |
| declare -i CYEAR=`date +%Y` | |
| while (( $YEAR <= $CYEAR )); do | |
| if [ $MONTH -eq 12 ]; then | |
| let MONTH=0 | |
| let YEAR=YEAR+1 | |
| fi | |
| if (( $YEAR == $CYEAR )) && (( $MONTH == $CMONTH )); then | |
| let ISSUE=ISSUE+1 | |
| break | |
| fi | |
| let MONTH=MONTH+1 | |
| let ISSUE=ISSUE+1 | |
| done | |
| echo "Downlaod to " + $MAGPIDIR | |
| COUNTER=1 | |
| until [ $COUNTER -ge $ISSUE ]; do | |
| fetch $COUNTER | |
| let COUNTER=COUNTER+1 | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the script., but all the issues being downloaded has a 0kb value