Auto-Download Linux Journal Each Month

""

There's an old saying, "anything worth doing, is worth automating"—or something like that. Downloading and reading Linux Journal always has been worth doing, and now you can automate it with our new autolj script, which you can get here.

Follow these few simple steps, and you can be downloading the PDF (or the .epub or the .mobi file) with the greatest of ease each month:

1) First download the script and save it somewhere; ~/bin is a good choice. You can name it whatever you like; it doesn't need to be called autolj.sh.

2) Open a terminal/shell and execute the following commands:


$ chmod +x ~/bin/autolj.sh
$ ~/bin/autolj.sh --init
Enter the email and zip/postal code associated
with your Linux Journal subscription
EMail: you@example.com    # Enter your email address
Zip  : 88888              # Enter your zip/postal code
Creating initial config file.
Change your preferences in '/home/YOU/.config/autolj.cfg'.
Sample crontab configuration is in '/home/YOU/.config/autolj.crontab'.

If you want to run the script from cron automatically each month, you can do this:


$ cp /home/YOU/.config/autolj.crontab mycrontab
$ crontab -l >>mycrontab
$ crontab <mycrontab
$ rm mycrontab

When you first run the script, use the --init command-line option to initialize the configuration file for the script. It will prompt for the email and zip/postal code associated with your Linux Journal subscription.

It saves that information in a file named ~/.config/autolj.cfg (if you saved the script with a different name, the base name of the config file will match the name that you saved the script under).

You can edit the configuration file with any text editor that you have on hand, or you can rerun the script with the --init option to re-create the config file (any existing changes that you've made will be lost).

The config file is a bash script that is sourced by the autolj script, so maintain valid bash syntax in the file. The config file contains a few other options that you may also want to change (the default value for each is shown):

  • doctypes — specifies the document types (PDF, EPUB, MOBI) to download (doctypes="pdf").
  • save_dir — specifies the directory where downloads are stored (save_dir='$HOME/linuxjournal/issues').
  • save_file — specifies the name used for downloaded files (save_file='LJ-$(printf %03d ${inum})-$year-$(printf %02d ${month}).${doc}').
  • notify_msg — specifies the message to use when notifying of a new download (notify_msg='The $(date +%B --date ${month}/1) ${year} Linux Journal ${doc^^}\\nhas been downloaded.').
  • do_notify — specifies if the script should attempt to notify you of new downloads (do_notify=1).

You may have noticed that the save_dir, save_file and notify_msg variables are in single quotes (meaning that the variables they reference won't get evaluated when the config file is sourced by the script). Rather, the script evaluates them when it needs them. When the strings are eval'd, the following variables will be set:

  • inum — issue number.
  • month — issue month as a number.
  • year — issue year.
  • doc — document type (pdf, epub or mobi).

By evaluating the strings when needed, you can customize where things are downloaded and how they are named.

Here are a few examples of what you can do:


# Download all types:
doctypes="epub mobi pdf"

# Organize downloads by document type:
#   $HOME/linuxjournal/epub   - epubs go here
#   $HOME/linuxjournal/mobi   - mobis go here
#   $HOME/linuxjournal/pdf    - pdfs go here
save_dir='$HOME/linuxjournal/${doc}'

# Organize downloads by month-year:
#   $HOME/linuxjournal/1-2018   - January
#   $HOME/linuxjournal/10-2018  - October
save_dir='$HOME/linuxjournal/${month}-${year}'
# Organize downloads by year-month (make sure month is 2 digits):
#   $HOME/linuxjournal/2018-01  - January
#   $HOME/linuxjournal/2018-10  - October
save_dir='$HOME/linuxjournal/${year}-$(printf %02d ${month})'

# Use the month name in the downloaded file:
#   Linux-Journal-January-2018.pdf
#   Linux-Journal-October-2018.pdf
save_file='Linux-Journal-$(date +%B ${month}/1)-$year.${doc}'

# Change the notification message.
notify_msg='The new LJ is here! The new LJ is here!
 ↪${month}-${year}-${doc}.'

# Disable notifications.
do_notify=0

If you run the script from cron and your system can deliver email to an account that you monitor, you'll get a notification when the script manages to download any new issue files.

If you have the program notify-send installed on your system, the script also will "attempt" to send a notification to your desktop when it downloads any files (notifications being the pop-ups that appear at the bottom right of your screen).

I use the word "attempt", because if you're running the script from cron, notify-send may not work. If you want to disable the use of notify-send, set do_notify to zero in the config file.

If you don't keep your system running all the time, you also can set up the script to auto-run whenever you log in.

A few more notes before we wrap it up:

  • You can only use the script to download the latest issue, so make sure you run it before the next issue comes out.
  • The XDG_CONFIG_HOME variable is honored: if you have it set, the config file, the sample crontab and the image file will be stored there rather than in ~/.config.
  • Double-check our generated crontab file to make sure it isn't doing anything you don't want.
  • The generated crontab entry runs at a randomly generated time between midnight and 5:59am, and it runs only on the first seven days of the month.
  • If you look at the script, you'll see a great big scary blob of base64-encoded data. Don't be afraid; it's just a png image that's saved in ~/.config/autolj.png, and it's used by notify-send to put an image in the notification message.
  • If you change the save_dir and save_file values in the config file, you can use the --no-download option to run the script, skip the actual downloading and generate some debug output to see if the directories and filenames are coming out as you expected.
""

Figure 1. Notification Image

And that's it! Download Linux Journal now and automate your life a little bit.

Send email to ljauto@linuxjournal.com to report bugs or if you need help with the script.

Load Disqus comments