Sunday, November 15, 2009

Frets on Fire and FoFix Song Cover Art and/or Album Cover Art Downloader

Maybe you are a fan of Frest On Fire or FoFix (personnaly, I prefer Fofix, since it has much more capabilities and less bugs).
Maybe you have some songs to play, and you would like to have cover art for your song list. To help you retrieving them automatically, I created this short script.
It is a very dirty script, and is not able to get all cover art. However It seems to make the job pretty well
For linux users, this script is usefull to, since it changes filenames starting with a upper case to lower case in songs folders.

WARNING: BE VERY CAREFUL. THIS SCRIPT IS JUST AN EXAMPLE OF WHAT IT IS POSSIBLE TO DO. THIS SCRIPT CONNECT TO AMAZON TO FIND THE COVER ART. YOU HAVE TO CHECK WITH AMAZON FIRST TO KNOW IF YOU CAN USE IT AS IS. I'M NOT RESPONSIBLE FOR ANY THING THIS SCRIPT CAN DO. USE IT AT YOUR OWN RISK AND AT YOUR OWN RESPONSIBILITY. CHECK IT DOESN'T DO THINGS WHICH MAY BE FORBIDDEN BEFORE USING IT.

Maybe you can modify this script to use another search engine.
This script needs ImageMagick program called "convert" to work.
It is a python script. To use it, create a file called coverart.py in your "songs" folder, then copy/paste the script below, and save the file. Then, using a terminal, go in your song folder and execute: python coverart.py
The script will check for songs in sub-folders recursively and download album cover art for each song.

Here is the bad and dirty script:


#!/usr/bin/env python
# coding: utf-8


##########################################################
#                                                        #
# Copyright Walter http://computersstoneage.blogspot.com #
#                                                        #
# This program is free software; you can redistribute    #
# it and/or modify it under the terms of the GNU General #
# Public License as published by the Free Software       #
# Foundation; either version 2 of the License, or (at    #
# your option) any later version.                        #
##########################################################


import urllib
import shutil
import re, os
import commands

def download(url):
    webFile = urllib.urlopen(url)
    a = webFile.read()
    start = a.find('._SL')

    a = a[start-70:start]
    start = a.find('http://')
    img =  a[start:]+"._SL500_AA240_.jpg"
    #print img
    return img

def getinfo(path, cover):
    artist = ""
    song = ""
    try:   
        f = open(path+"song.ini", "r")
        tmp = f.readlines(2000)
        f.close()
        for line in tmp:
            if (line.strip().startswith('name') or line.strip().startswith('Name')):               
                song = line.strip().split("=")[1].strip()
                a = song.find('(')
                if a > 0:
                    song = song[:a]
            if (line.strip().startswith('artist') or line.strip().startswith('Artist')):
                artist = line.strip().split("=")[1].strip()
                a = artist.find('(')
                if a > 0:
                    artist = artist[:a]
        song = song.replace(' ','+');
        artist = artist.replace(' ','+');
       
        return 'http://www.amazon.com/s/ref=nb_ss?url=search-alias%3Dpopular&field-keywords=%22'+artist+'%22+%22'+song+'%22&x=0&y=0'
    except:
        print "Failed to open "+path+"song.ini"
        #return ""
        exit
       
   
def AddCoverArt(path):
    url = getinfo(path,True)
    if len(url) < 10:
        return
    img = download(url)
    if ((len(img) < 20) or (len(img)>1000)):
        print("Nothing found with cover for: "+path+" "+url);
        return
    img=urllib.unquote_plus(img)
    print("Loading: "+img)
    try:
        webFile = urllib.urlopen(img)
        localFile = open(path+"album.tmp.png", 'w')
        localFile.write(webFile.read())
        localFile.close()
    except:
        print "Can't get: "+img
        return ""


def ScanDir(g):
        files = os.listdir(g)
        for f in files:
                if(os.path.isdir(g+f)):
                        print('Entering: '+g+f+'/')
                        ScanDir(g+f+'/')
        if(os.path.exists(g+'Song.ogg')):
                print( 'mv '+g+'Song.ogg '+g+'song.ogg')
                os.rename(g+'Song.ogg', g+'song.ogg')
        if(os.path.exists(g+'Song.ini')):
                print( 'mv '+g+'Song.ini '+g+'song.ini')
                os.rename(g+'Song.ini', g+'song.ini')
        if(os.path.exists(g+'Notes.mid')):
                print( 'mv '+g+'Notes.mid '+g+'notes.mid')
                os.rename(g+'Notes.mid', g+'notes.mid')
        if(os.path.exists(g+'Rhythm.ogg')):
                print( 'mv '+g+'Rhythm.ogg '+g+'rhythm.ogg')
                os.rename(g+'Rhythm.ogg', g+'rhythm.ogg')
        if(os.path.exists(g+'Guitar.ogg')):
                print( 'mv '+g+'Guitar.ogg '+g+'guitar.ogg')
                os.rename(g+'Guitar.ogg', g+'guitar.ogg')
        if(os.path.exists(g+'guitar.ogg')) and (not os.path.exists(g+'song.ogg')):
                shutil.copy2(g+'guitar.ogg', g+'song.ogg')
                print( 'cp '+g+'guitar.ogg '+g+'song.ogg')
    if(os.path.exists(g+'Album.png')):
        os.rename(g+'Album.png', g+'album.png')
                print( 'mv '+g+'Album.png '+g+'album.png')
    if(os.path.exists(g+'song.ini'))and (not os.path.exists(g+'album.png')):
        AddCoverArt(g)
    if(os.path.exists(g+'album.tmp.png')):
        #os.rename(g+'album.png', g+'album.tmp.png')
        cmd = 'convert "'+g+'album.tmp.png" "'+g+'album.png"'
        print('Convertion: '+cmd)
        commands.getstatusoutput(cmd)
        os.unlink(g+'album.tmp.png')
       
ScanDir('./')



That's it...

2 comments:

jbrigaud said...

Very great ! Thank you !

Stive Peterson said...

Although a lot of people started doing covers of songs the over of Shawn Mendes will remain one of my favorites!