Created
May 17, 2024 23:30
-
-
Save jscholes/0bc329ba2451897364513bc21fbe32c3 to your computer and use it in GitHub Desktop.
Udio media URL extraction script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import json | |
import re | |
import sys | |
import urllib.error | |
import urllib.request | |
from urllib.parse import urlparse | |
HTTP_TIMEOUT = 10 | |
HTTP_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36' | |
SONGS_API_URL = 'https://www.udio.com/api/songs?songIds=' | |
def main(): | |
parser = argparse.ArgumentParser( | |
prog='udioSongURL.py', | |
description='Given a link to a song on Udio, print its media file URL.', | |
) | |
parser.add_argument('link', help='The Udio song link, with or without "www." in the domain name, and with or without additional query string params on the end.') | |
args = parser.parse_args() | |
try: | |
result = run(args.link) | |
except RuntimeError as e: | |
print(f'Error extracting media URL for song link: {args.link}:\n{e}') | |
sys.exit(1) | |
print(f'{result["artist"]} - {result["title"]}:\n{result["mediaURL"]}') | |
def run(songLink): | |
urlPath = urlparse(songLink).path | |
if match := re.search(r'/songs/([^/?]+)', urlPath): | |
songID = match.group(1) | |
else: | |
raise RuntimeError('Could not extract song ID from provided link') | |
requestURL = f'{SONGS_API_URL}{songID}' | |
try: | |
request = urllib.request.Request(requestURL, headers={'User-Agent': HTTP_USER_AGENT}, method='GET') | |
with urllib.request.urlopen(request, timeout=HTTP_TIMEOUT) as response: | |
responseBody = response.read().decode('utf-8') | |
except urllib.error.HTTPError as e: | |
raise RuntimeError(f'Request to API URL {requestURL} failed: {e}') | |
except TimeoutError as e: | |
raise RuntimeError(f'Request to API URL {requestURL} failed: HTTP connection timed out after {HTTP_TIMEOUT} seconds') | |
try: | |
data = json.loads(responseBody) | |
except Exception as e: | |
raise RuntimeError(f'Unable to decode HTTP response as JSON from: {requestURL}') | |
songs = data.get('songs', []) | |
numberOfSongs = len(songs) | |
if numberOfSongs == 0: | |
raise RuntimeError('No matching songs') | |
elif numberOfSongs > 1: | |
raise RuntimeError(f'{numberOfSongs} matching songs found; multiple song matches are not supported') | |
song = songs[0] | |
result = { | |
'artist': song.get('artist', 'Unknown Artist'), | |
'title': song.get('title', 'Unknown Title'), | |
'mediaURL': song.get('song_path', None), | |
} | |
if result['mediaURL'] is None: | |
raise RuntimeError(f'Unable to extract media URL for song from API response:\n{responseBody}') | |
return result | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment