Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Python

Downloading Podcasts from Changelog

5.00/5 (1 vote)
8 Nov 2018CPOL3 min read 3.9K  
Due to busy lives in the 21st century, finding time to learn about new topics and keeping up to date with technological trends becomes more and more difficult.

One great way to keep learning and improving without sacrificing time from other activities is to listen to some of the great podcasts out there.

If you are like me and enjoy listening to podcasts on your phone while you are offline, you will find that many sites don’t offer a direct download button, forcing you to look for the file manually. Recently, I encountered this problem with the podcasts on https://changelog.com, so I decided to write a small console application that downloads the mp3s directly to my disk.

Prerequisites

This will be a rather simple but hopefully still useful tutorial. To follow along, all you need is a Python IDE and basic Python skills. You can find the entire source code of the project on my Github page.

Preparing the Solution

Since we are going to write a rather small console application, I decided to keep everything in one file. To get started, create an empty changelog_mp3.py file. To make it executable, you should add the standard Python shebang at the top of the file:

Python
#!/bin/python3

Analysing Changelog.com

On the Changelog overview page, you will find several podcast categories to choose from. Once you open either category you are interested in and choose a podcast, you will be presented with some information about the podcast and an online player. To get to the actual mp3 file, you will have to right-click the Play button and open the link in a new tab. Take a look at the URL and you will see the file we want to download, for example, js-party-25 for the JS Party podcasts.

You can get the base URLs for the other podcasts in a similar manner, allowing us to initialize them in an array for all our podcasts:

Python
base_urls = ['https://cdn.changelog.com/uploads/podcast/<id>/the-changelog-<id>.mp3',
             'https://cdn.changelog.com/uploads/gotime/<id>/go-time-<id>.mp3',
             'https://cdn.changelog.com/uploads/rfc/<id>/request-for-commits-<id>.mp3',
             'https://cdn.changelog.com/uploads/founderstalk/<id>/founders-talk-<id>.mp3',
             'https://cdn.changelog.com/uploads/spotlight/<id>/spotlight-<id>.mp3',
             'https://cdn.changelog.com/uploads/jsparty/<id>/js-party-<id>.mp3',
            ]

Downloading Podcasts

Let’s get to the core of this program: The code to download the podcasts. All the magic will happen inside the following download_podcasts method:

Python
def download_podcast(podcast_id):
    base_url = base_urls[podcast_id - 1]
    print("Enter one or more episodes to download:")
    episodes = (int(x) for x in input().split(','))
    target = '/home/philipp/Downloads/'

    for episode in episodes:
        download_url = base_url.replace('<id>', str(episode))
        print('Downloading episode {} to {}'.format(str(episode),target))
        try:
            urllib.request.urlretrieve(download_url, target + download_url.split('/')[-1])
            print('Download complete!')
        except:
            print('Could not download file: {}'.format(download_url))

The only parameter for our method is the podcast_id which tells the program which category of podcasts should be downloaded. Next, we ask the user to enter one or multiple episode numbers he/she wants to download and define the target folder for the mp3 files.

In the following for loop, we simply iterate through all selected episodes, replace the ‘id’ placeholder with the episode number, so we get a valid URL and finally download the file. Since we are using the urllib library, you will have to add an import statement at the top of the file:

Python
import urllib.request

This is all we need to download podcasts from https://changelog.com and store them as mp3 files. To call our new code, I implemented a simple console UI inside the main method:

Python
def main():
    print('Which podcast would you like to download?')
    print('(1) - The Changelog')
    print('(2) - Go Time')
    print('(3) - Request for Commits')
    print('(4) - Founders Talk')
    print('(5) - Spotlight')
    print('(6) - JS Party')

    n = int(input())
    download_podcast(n)

This allows the user to select a podcast and then select episodes from within the download_podcast method. To run our new code upon execution of the file, simply add the following entry point to the program:

Python
if __name__ == "__main__":
    main()

Conclusion

I hope you enjoyed this short article and see how easy it can be to write small utility tools to make our lives easier. Feel free to extend the code to download any other podcasts you like in a more convenient manner!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)