Friday, May 11, 2018

scheduling iTunes downloads

We obtain a lot of our video via iTunes, not least because we can download it and watch at our convenience instead of trying to stream and hoping the internets are uncluttered.

However, we need to watch our ISP data quota. We're on a plan with a generous off peak quota, but the off peak period is 1am to 7am. Since iTunes has no scheduling facility this generally involves me getting up in the middle of the night at some point. I've seen a few recipes on the net to work around this, but they all seem cumbersome and/or baroque. Now I have a better way.

I wrote a small helper script called itunes which can tell an open iTunes app to commence a download. It also has a command to fetch a list of the currently selected media items from an open iTunes.

Combined, my workflow is now as follows:

  1. Open iTunes.
  2. Locate the items to download and select them, or some of them.
  3. From a shell, issue the command: itunes download selected
  4. Check back in the morning.
If my items are scattered about, I need to iterate steps 2 and 3 a few times; because the script will wait until the off peak time I need to keep a few shells open, one for each little batch. Because the selection itself is collected when I dispatch the script, this works fine: select, start script, repeat. Conversely, if the items are together, such as the episodes of a television series, I can select them all and issue the script just once.

The nitty gritty:

The scheduling: my laptop's crontab includes these lines:

30 1 * * * . $HOME/.profile; flag ISP_OFF_PEAK 1
30 6 * * * . $HOME/.profile; flag ISP_OFF_PEAK 0
which turn on and off the ISP_OFF_PEAK flag, a persistent boolean value maintained by my flag script.

The itunes helper script waits for a particular flag state before issuing the download request: the defaults are ROUTE_DEFAULT ISP_OFF_PEAK !DOWNLOAD_DISABLE.

The ROUTE_DEFAULT flag is maintained by some other automation I run, and is true when the laptop has a default route, which I use to infer that it is online, with access to the internet.

The ISP_OFF_PEAK is maintained by cron as described above.

The DOWNLOAD_DISABLE flag is entirely notional; I could set it to true to prevent downloads happening during the night.

No comments: