Saturday, June 16, 2012

How To Automatically Download TV Shows to Your Mac

So you've read the Perl programming language articles here on Macinstruct, and you're feeling more comfortable with Perl in general. Now you're wondering how to use this information to do something cool. How about building a solution that automatically downloads TV shows to your Mac?

That's right. With some free tools, a little configuration, and a few hundred lines of Perl, you can automatically download new episodes of TV shows to your Mac. But that's not all. This solution will also automatically convert the shows and import them into iTunes with the TV show name, episode name, season number, and episode number. Let's get started!

How This Works

Let's talk for a minute about how this solution works, and what it will do. First, you'll subscribe to TV shows with an application called TVShows 2. It will always be watching for new torrents of the TV shows that you subscribe to. When TVShows 2 finds a new episode, it will automatically download the torrent, and then Transmission will start downloading the file. After the download completes, Transmission will call a helper file which you'll create later. That file will call import.pl, which will convert, name, and import your brand new episode into iTunes.

First, Some Words of Warning

By now, you've probably realized what this means. You could download practically any TV show to your Mac, automatically and for free. But you might be breaking the law by doing so. You should not download TV shows unless a copyright owner has released them for free distribution. It's your responsibility to comply with federal and international regulations.
If nothing else, please consider the people who create and produce the shows you love. These people rely on advertising revenue for their livelihoods, and a show's continued success depends on the number of viewers who regularly, and legally, watch it. Wouldn't you want to be paid for the work you do?

Getting the Right Tools for the Job

First, go ahead and download the following applications and files:
We'll show you want to do with them in just a moment.

Subscribing to TV Shows

TVShows 2 is a free preference pane that lives in System Preferences. Once installed and configured, it watches for new episodes and automatically downloads new files when they become available. This is the backbone of our automated TV show downloading system, but it's worth noting that this application can also be used independently, without our nifty Perl scripts.
Now you should use TVShows 2 to subscribe to your favorite TV shows. Here's how:
  1. From the Apple menu, select System Preferences.
  2. Select TV Shows, as shown below.

  3. Click the Add Show button, as shown below.

  4. Subscribe to whichever shows you want to automatically download.
TVShows 2 is now configured to download TV shows to your Mac.

Configuring TVShows 2

Next, let's configure TVShows 2 to work with Transmission and our Perl scripts. This will help create a seamless integration between the applications, so you won't even notice the episodes downloading. Everything will happen in the background, without you having to do anything.
Here's how to configure TVShows 2:
  1. In the TVShows 2 preferences pane, click Preferences. The window shown below appears.

  2. Verify that Episode naming convention is set to S01E01.
  3. Verify that the Automatically open each Torrent file after download checkbox is selected.
  4. Select an a Episode save location. It doesn't matter what you select - just remember what it is.
  5. Close System Preferences
That's it! You've configured TVShows 2 to automatically download the TV shows you subscribed to.

Configuring Transmission

Now let's configure Transmission, the free BitTorrent client, to automatically download the torrents that are found by TVShows 2. Without this application, you wouldn't have a way of downloading the episodes to your Mac! Here's how to configure Transmission:
  1. Open the Transmission application.
  2. From the Transmission menu, select Preferences.
  3. Click Transfers. The window shown below appears.

  4. Verify that the Start transfers when added checkbox is selected.
  5. Verify that the Display a window when opening a torrent file checkbox is deselected.
Note that you do not need to have the Auto add checkbox selected. TVShows 2 will do that work for you.

Preparing Perl

Once you have downloaded and installed the applications, use the skills you learned in Using CPAN to Extend Perl on Mac OS X to download the following dependencies from CPAN:
  • Log::Log4perl
  • LWP::UserAgent
  • HTTP::Request
  • HTTP::Response
Next, make sure "import.pl" is executable by entering the following command in the Terminal application:
chmod +x import.pl
Remember? You learned how to do that in Getting Started with Perl on Mac OS X.

Putting the Pieces Together

Things are coming together nicely. We've installed the Perl dependancies and configured TVShows 2 and Transmit. Now we just need to glue everything together so it works seamlessly and automatically! Here's how:
  1. Open a text editor, such as TextWrangler.
  2. Paste following in a file named begin_import.sh:
    #!/bin/bash
    cd ~/Desktop/ && ./import.pl $TR_TORRENT_NAME"
  3. That code will work if you are downloading your TV Shows to your desktop. If not, change ~/Desktop to the appropriate directory.
  4. Save the file in a safe place.
  5. Open the Transmission application.
  6. From the Transmission menu, select Preferences.
  7. Click Transfers, and then click Management. The window shown below appears.

  8. Select the Call script checkbox.
  9. From the file selection window that appears, select the begin_import.sh so it's called when Transmission completes a download.
That's it! From now on, your Mac will automatically download TV shows when new episodes become available.

Final Thoughts

To recap, here's what's happening. TVShows 2 is always watching for new torrents of TV shows. When it finds one, it automatically downloads the torrent and Transmission starts downloading the file. When the download completes, Transmission calls the helper file (begin_import.sh), which calls import.pl, and converts, names, and imports your brand new episode into iTunes. Of course, once the shows are in iTunes, you can transfer them to your iPad, iPhone, or iPod Touch.
Enjoy your new TV shows! To keep improving your Perl chops, you may want to dive into those files that you downloaded from GitHub and look around. If you see something wonky, or if you have an improvement, be sure to fork the repo and submit changes! Retweet this story

No comments:

Post a Comment