#!/usr/bin/env python # playlister.py - # A program for reading your local CDDB archive and sharing your # CD playlist with the rest of the world. Really, it is quite # stupid. # # Copyright (C) 2004, Samuel Nils Hart Jr # # 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. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import httplib, urllib, re, sys, string, time, urlparse, os, fnmatch ######################### # Configuration section # ######################### # The source directory where all the HTML and source image files go SOURCE_DIRECTORY = "/home/sam/work/web/playlist" # The CDDB directory containing all the CDDB files CDDB_DIRECTORY = "/home/sam/.cddb" # The work directory (must exist) WORK_DIRECTORY = "/home/sam/work/web/playlist/work" # The pre html file PRE_HTML = "pre.html" # The post html file POST_HTML = "post.html" # The "Cover Unavailable" image NO_COVER = "cover_unknown.png" # Other files needed in work dir OTHER_FILES = ('bkg-box.gif', 'bkg-main.gif', 'playlist-logo.gif') # The output filename OUTPUT_HTML = "music.shtml" HTML_TXT = "" def prework(): # Clear the work directory os.system("cd "+WORK_DIRECTORY+"; rm -f *") # Place the NO_COVER image there os.system("cp -f "+SOURCE_DIRECTORY+"/"+NO_COVER+" "+WORK_DIRECTORY+"/.") # Place the other files there for file in OTHER_FILES: os.system("cp -f "+SOURCE_DIRECTORY+"/"+file+" "+WORK_DIRECTORY+"/.") # Grab the image and determine if its usable def imagework(image_url): image_file = "" try: scheme, location, path, query, fragment = urlparse.urlsplit(image_url) URL = path+query h = httplib.HTTP(location) h.putrequest('GET', URL) h.putheader('Host', location) h.putheader('User-agent', 'Mozilla/5.0') h.endheaders() try: returncode, returnmsg, headers = h.getreply() serv_path, image_file = os.path.split(path) h.close() if(returncode < 400): os.system("cd "+WORK_DIRECTORY+"; wget "+image_url) else: image_file = NO_COVER except: image_file = NO_COVER except: image_file = NO_COVER h.close() return image_file ############## # GOOGLE.COM # Using image search feature from google : # http://images.google.com/images?q=artist+name+song+name # then getting href=/imgres?imgurl=URL&imgrefurl ########################################### # URL= "http://images.google.com/images?q="+SEARCH # conn = urllib.urlopen(URL) # data = conn.read() # conn.close() # Original author Caligari def google(search_string): SEARCH=string.lower(string.replace(search_string+" "," ","+")); URL="/images?q="+SEARCH host = "images.google.com" print "http://"+host+URL error_times = 0; data = "" while error_times < 5: h = httplib.HTTP(host) h.putrequest('GET', URL) h.putheader('Host', host) h.putheader('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.5) Gecko/20041207 Firefox/1.0 (Debian package 1.0-5)') h.putheader('HTTP_CONNECTION', 'keep-alive') h.putheader('HTTP_ACCEPT_LANGUAGE', 'en-us,en;q=0.5') h.putheader('HTTP_KEEP_ALIVE', '300') h.putheader('SERVER_PROTOCOL', 'HTTP/1.1') h.endheaders() try: returncode, returnmsg, headers = h.getreply() foo = h.getfile() data=foo.read() h.close() error_times = 6 except: error_times += 1 print "Failed on "+host+URL+" "+str(error_times)+" times" time.sleep(2) h.close() # .jpg h.close() s = re.search("imgurl=.*?(jpg&imgrefurl)",data) if (s): s= re.search("imgurl=(?!.*imgurl=).*?(jpg&imgrefurl)",s.group()) # .JPG if (not(s)): s = re.search("imgurl=.*?(JPG&imgrefurl)", data) if (s): s = re.search("imgurl=(?!.*imgurl=).*?(JPG&imgrefurl)", data) # .jpeg if (not(s)): s = re.search("imgurl=.*?(jpeg&imgrefurl)", data) if (s): s = re.search("imgurl=(?!.*imgurl=).*?(jpeg&imgrefurl)", data) # .gif if (not(s)): s = re.search("imgurl=.*?(gif&imgrefurl)", data) if (s): s = re.search("imgurl=(?!.*imgurl=).*?(jpeg&imgrefurl)", data) # .GIF if (not(s)): s = re.search("imgurl=.*?(GIF&imgrefurl)", data) if (s): s = re.search("imgurl=(?!.*imgurl=).*?(GIF&imgrefurl)", data) if (s): #Hurray, we've found something image_url=string.replace(s.group(),"imgurl=","") image_url=string.replace(image_url,"&imgrefurl","") return image_url return "http://samhart.com/404.html" def parse_cddb_file(filename): in_list = 0 html = "
\n" title = "" title_uri = "" f = open(filename, 'r', -1) for line in f: if (not(line.startswith('#'))): # We are in a non-comment chunk, data = line.split('=', 2) if (re.match("DTITLE", chunk)): title = data title_uri = string.replace(data+" "," ","+") html += ""+data+"
\n
    " in_list = 1 if (re.search("TTITLE", chunk)): if (in_list): song_uri = string.replace(data+" mp3"," ","+") html += "
  • "+data+"
  • \n" if (in_list): html += "
\n" html +="
\n" #time.sleep(5) # Poor, poor Google image_url = google(title) #time.sleep(2) # Poor, poor whatever image_file = imagework(image_url) html +="
" html +="\""+title+"\"
\n" html +="
\n" f.close() return html def doFiles(junk, directory, cddb_files): global HTML_TXT for file in cddb_files: HTML_TXT += parse_cddb_file(CDDB_DIRECTORY+"/"+file) ############### # The main code prework() HTML_TXT = "" f = open (SOURCE_DIRECTORY+"/"+PRE_HTML, 'r', -1) for line in f: HTML_TXT += line f.close() os.path.walk(CDDB_DIRECTORY, doFiles, None) f = open (SOURCE_DIRECTORY+"/"+POST_HTML, 'r', -1) for line in f: HTML_TXT += line f.close() f = open (WORK_DIRECTORY+"/"+ OUTPUT_HTML, 'w', -1) f.write(HTML_TXT) f.close() #image_url = google("Paul Simon / Negotiations and Love Songs 1971-1986") #image_file = imagework(image_url) #print image_url+" "+image_file #image_url = google("Various / Are You Ready to Dance Vol. 3") #image_file = imagework(image_url) #print image_url+" "+image_file