1 #! /usr/bin/python
  2
  3 #   Copyright (c) 2010 Joseph Robert Gillotti <joe@u13.net>
  4 #
  5 #   This file is part of PyAlbumArtFetcher
  6 #
  7 #   PieStats free software; you can redistribute it and/or modify
  8 #   it under the terms of the GNU General Public License as published by
  9 #   the Free Software Foundation; either version 2 of the License, or
 10 #   (at your option) any later version.
 11 #
 12 #   This program is distributed in the hope that it will be useful,
 13 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 #   GNU General Public License for more details.
 16 #
 17 #   You should have received a copy of the GNU General Public License
 18 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 19
 20
 21 # We need these
 22 import os
 23 import sys
 24 import glob
 25 import httplib
 26 import traceback
 27 from lxml import etree
 28
 29 # Configure this
 30 musicDir = '/home/Music'
 31
 32 # This might be more useufl
 33 if len(sys.argv) > 1:
 34         musicDir = sys.argv[1]
 35
 36 # Don't touch below
 37 apiKey = 'e40c04743632809d8176349ed86d2ade'
 38
 39 # Save a file somewhere
 40 def saveFile(URL, savePath):
 41
 42         # Don't waste time
 43         if (os.path.isfile(savePath)):
 44                 return
 45
 46         # Don't let one thing bring us down
 47         try:
 48
 49                 # Get server settings
 50                 server = URL.replace('http://', '')
 51                 parts = server.split('/', 2)
 52                 server = parts[0]
 53                 pathStart = len(server) + len('http://') + 1
 54                 path = '/%s' % URL[pathStart:]
 55
 56                 # Fetch image contents
 57                 conn = httplib.HTTPConnection(server)
 58                 conn.request('GET', path)
 59                 r1 = conn.getresponse()
 60                 fileContents = r1.read()
 61                 conn.close()
 62
 63                 # Write the file contents
 64                 f = open(savePath, 'w')
 65                 f.write(fileContents)
 66                 f.close()
 67
 68         except:
 69                 return 
 70
 71 try:
 72         # Cute message
 73         print 'Fetching album art...'
 74         
 75         # Find each album folder
 76         for folder in glob.glob('%s/*/*/' % musicDir):
 77                 
 78                 # Configure album and artist name and api location
 79                 album = os.path.basename(os.path.dirname(folder))
 80                 artist = os.path.basename(os.path.dirname(os.path.dirname(folder)))
 81                 apiURL = '/2.0/?method=album.getinfo&api_key=%s&artist=%s&album=%s' % (apiKey, artist.replace(' ', '%20'), album.replace(' ', '%20'))
 82                 apiServer = 'ws.audioscrobbler.com'
 83                 imageBigSavePath = '%sFolder.jpg' % folder
 84                 imageMedSavePath = '%sAlbumArtSmall.jpg' % folder
 85                 
 86                 # Already have it?
 87                 if (os.path.isfile(imageBigSavePath) and os.path.isfile(imageMedSavePath)):
 88                         print '%s - %s: Already have it' % (artist, album)
 89                         continue
 90
 91                 # Connect to API to get album info
 92                 conn = httplib.HTTPConnection(apiServer)
 93                 conn.request('GET', apiURL)
 94                 r1 = conn.getresponse()
 95                 xmlContent = r1.read()
 96                 conn.close()
 97                 
 98                 # Make sure we found the album
 99                 root = etree.fromstring(xmlContent)
100                 status = root.get('status')
101                 if status != 'ok':
102                         print '%s - %s: Album not found' % (artist, album)
103                         continue
104                 
105                 # Get the imageu urls
106                 for element in root.iter():
107                         if element.tag == 'image' and element.get('size') == 'large':
108                                 imageBigURL = element.text
109                                 break
110                         elif element.tag == 'image' and element.get('size') == 'medium':
111                                 imageMedURL = element.text
112                         else:
113                                 continue
114                 
115                 # Did we not?
116                 if imageBigURL == None and imageMedURL == None:
117                         print '%s - %s: Image not found' % (artist, album)
118                         continue
119                 
120                 # Save both image sizes
121                 if imageBigURL != None:
122                         saveFile(imageBigURL, imageBigSavePath)
123
124                 if imageMedURL != None:
125                         saveFile(imageMedURL, imageMedSavePath)
126                 
127                 # That should have done it. Say so.
128                 print '%s - %s: done' % (artist, album)
129
130 except:
131         print 'failed'
132         traceback.print_exc()