Skip to content

Instantly share code, notes, and snippets.

@theychx theychx/itunesfeed.py
Last active Oct 21, 2018

Embed
What would you like to do?
For use with itunes podcast url's like "https://itunes.apple.com/us/podcast/grow-big-always/id1060318873"
#! /usr/bin/env python3
import sys
from urllib.parse import urlparse
from urllib.request import urlopen
import codecs
import json
ITUNES_URL = 'https://itunes.apple.com/lookup?id='
class InvalidUrlError(Exception):
pass
def getfeed(url):
parsed = urlparse(url)
if parsed.netloc != 'itunes.apple.com':
raise InvalidUrlError
try:
id = parsed.path.split('/')[-1][2:]
int(id)
except (IndexError, ValueError):
raise InvalidUrlError
reader = codecs.getreader('utf-8')
try:
with urlopen(ITUNES_URL + id) as response:
feed = json.load(reader(response))['results'][0]['feedUrl']
except urllib.error.HTTPError:
raise InvalidUrlError
return feed
if __name__ == '__main__':
try:
print(getfeed(sys.argv[1]), end='')
except InvalidUrlError:
sys.exit('invalid url')
except IndexError:
sys.exit('please specify an url')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.