Home » Sport » Subscribers: Auto-Download Linux Journal From the Command Line (v2.0)
lj tablet cover

Subscribers: Auto-Download Linux Journal From the Command Line (v2.0)

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 autolj script, which you can get here (updated to version 2.0, see the end of this article for a list of feature enhancements).

Follow the below few simple steps and you can download the entire magazine as a PDF, .epub or .mobi file with the greatest of ease each month. New to version 2.0 of this script, we’ve added the ability for you to pull down an individual Linux Journal article from the most recent issue within a terminal window (sometimes just reading one article at a time makes our wonderfully lengthy issues easier to get through!)

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_dirsave_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

# Organize downloads by month-year:
#   $HOME/linuxjournal/1-2018   - January
#   $HOME/linuxjournal/10-2018  - October
# 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!

# Disable notifications.

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-downloadoption 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.

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

autolj — Version 2.0.1

This version adds the following features:

  • Download past issues by specifying --offset N, where N is an offset from the current issue. Currently, negative and positive values of N have the same impact: they refer to N issues before the latest issue. Initial work to allow downloading future issues has been promising, but the feature is still in development.
  • Show the contents of the past N issues with the --contents N option. N defaults to 1 if not specified.
  • Read individual article PDFs by specifying an article number to the --read-pdf option.
  • Read text versions of individual articles by specifying an article number to the --read-text or --read-txt options.
  • Read a random unread article from the past N issues by specifying random:N as an option to one of the --read-* options above. N defaults to 1 if not specified.
  • Show the unread articles from the past N issues with the --unread N option. N defaults to 1 if not specified.

Config file changes:

You can now include “tar.gz” in “doctypes” to download the individual PDF and text versions of an issue’s articles, for example:

doctypes="pdf tar.gz"

Old options:

  • --init — Initialize configuration file.
  • --no-download — Don’t download, just show what would be downloaded.

New options:

  • --version — Show version number and exit.
  • --help — Show help and exit.
  • --contents NISS — Show contents of the latest NISS issues. If NISS is not specified, it defaults to 1.
  • --unread NISS — Show unread articles from latest NISS issues
  • --read-pdf ANUM — Show PDF version of article ANUM.
  • --read-text ANUM or --read-txt ANUM — Show text version of article ANUM.ANUM can be specified as random:NISS to pick a random article from the latest NISS issues. If NISS is not specified with --contents--unread or random:NISS, it defaults to 1.



Check Also

xl  ransomware

B0r0nt0K Ransomware Threatens Linux Servers

A new cryptovirus called “B0r0nt0K” has been putting Linux and possibly Windows Web servers at ...