Skip to content

Instantly share code, notes, and snippets.

@rissole
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rissole/7f702d4f7ecd62d6dc25 to your computer and use it in GitHub Desktop.
Save rissole/7f702d4f7ecd62d6dc25 to your computer and use it in GitHub Desktop.
import xml.etree.ElementTree as ET
import urllib.request, urllib.parse, urllib.error
import json
import re
import os, subprocess, shutil
downloaded_album_art = {}
M4A_FOLDER = 'm4a/'
IMG_FOLDER = 'img/'
MP3_FOLDER = 'mp3/'
def initialise_album_cache():
for file in os.listdir(IMG_FOLDER):
downloaded_album_art[file] = True
def download_art_for_game(game_name, *ignored_urls):
print('[IMG] Retrieving album art for', game_name)
image_name = re.sub(r'\W', '', game_name)+'.png'
if downloaded_album_art.get(image_name) == True:
print('[IMG]',game_name,'already downloaded.')
return image_name
q = urllib.parse.quote('"%s" soundtrack' % (game_name,))
with urllib.request.urlopen("http://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=6&q="+q) as response:
images = json.loads(response.readall().decode('utf-8'))['responseData']['results']
images = list(filter(lambda i: i['unescapedUrl'] not in ignored_urls, images))
for ratio_tolerance in [x * 0.1 for x in range(1, 5)]:
best_image = next((i for i in images if abs(int(i['width']) / int(i['height']) - 1) < ratio_tolerance), None)
if best_image != None:
break
if best_image == None:
with open(IMG_FOLDER+'unknowns.txt', 'a') as f:
f.write(game_name+'\n')
return IMG_FOLDER+'_unknown.png'
image_url = best_image['unescapedUrl']
try:
urllib.request.urlretrieve(image_url, IMG_FOLDER+image_name)
except urllib.error.URLError:
return download_art_for_game(game_name, *(list(ignored_urls) + [image_url]))
downloaded_album_art[image_name] = True
print('[IMG] Downloaded', game_name)
return image_name
def make_mp3(song_name, game_name, m4a_filename, image_filename):
mp3_path = MP3_FOLDER + m4a_filename[:-3] + 'mp3'
if os.path.exists(mp3_path):
return False
subprocess.Popen([
'ffmpeg/bin/ffmpeg.exe', '-y', '-loglevel', 'error'
'-i', M4A_FOLDER + m4a_filename,
'-i', IMG_FOLDER + image_filename,
'-map', '0', '-map', '1', '-id3v2_version', '3',
'-metadata', 'title=%s' % (song_name,),
'-metadata', 'album=%s' % (game_name,),
mp3_path
]).wait()
return True
if __name__ == "__main__":
initialise_album_cache()
e = ET.parse('roster.xml').getroot()
ns = {'pl':'http://xspf.org/ns/0/'}
for track in e[0]:
creator = track.find('pl:creator', ns).text
title = track.find('pl:title', ns).text
location = track.find('pl:location', ns).text
image_filename = download_art_for_game(creator)
#make_mp3(title, creator, location.split('/')[-1], image_filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment