Skip to content

Instantly share code, notes, and snippets.

@theychx
Last active September 30, 2022 20:18
Show Gist options
  • Save theychx/f9fad123bef27bebac665847c7884cd9 to your computer and use it in GitHub Desktop.
Save theychx/f9fad123bef27bebac665847c7884cd9 to your computer and use it in GitHub Desktop.
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