#This script is a simple MP3 report program.
#creator: Nitin Arora, 2009
import os, fnmatch, sys, id3reader
i=0
#Greet user.
print "Hello.";
print "I'm MyMusicReporter.";
print "I'm written in Python\n";
print "I really recommend placing me on your Desktop for ease of use.";
print "\nAnyway, I'll create 2 files in the folder you are running this script from.";
print "... and that would be the Desktop perhaps? ;-)\n";
print "The first will be an XML file containing the ID3 tags:";
print "\tTitle, Performer, Track, and Year, and file Path for every MP3 in:";
print "\tthe folder you tell me to look in - as well as its subfolders.";
print "The second file is an XML stylesheet.";
print "\tYou won't use it, but it makes the XML file look pretty in your browser.\n\t:-)\n";
#Get root directory from user; set .mp3 as default extension.
myDir = raw_input("... so what directory should I look in?:\n");
myExt = "*.mp3"
#Check for existence of files and die if they exist already.
here = os.path.isfile("./MyMusicReport.xml")
if here == True:
print "Error: MyMusicReport.xml already exists."
print "I won't overwrite it.";
raw_input("\n\nPress the Enter key to exit.")
sys.exit()
here2 = os.path.isfile("./MyMusicReport.xsl")
if here2 == True:
print "Error: MyMusicReport.xsl already exists."
print "I won't overwrite it.";
raw_input("\n\nPress the Enter key to exit.")
sys.exit()
#Write files, etc.
fo1 = open("MyMusicReport.xml", "wb")
fo2 = open("MyMusicReport.xsl", "wb")
#Assign variable for the XML header and write them to file.
a1 = '\n'
a2 = '\n\n'
a4 = "\n\n"
# Write header.
fo1.write(a1);
fo1.write(a2);
fo1.write(a3);
fo1.write(a4);
# Assign variables for the XML tags.
b1 = "\t\n"
b2 = "\t\t"
b3 = "\n"
b4 = "\t\t"
b5 = "\n"
b6 = "\t\t\n"
b8 = "\t\t"
b9 = "\n"
b10 = "\t\t"
b11 = "\n"
b12 = "\t\n\n"
#Get ID3 data.
for dirname, dirnames, filenames in os.walk(myDir):
for filename in filenames:
if fnmatch.fnmatch (filename,myExt):
myPath = os.path.join(dirname, filename)
id3r = id3reader.Reader(myPath)
Title = id3r.getValue('title')
if Title==None:
Title="ALERT: ID3 Tag missing!"
Performer = id3r.getValue('performer')
if Performer==None:
Performer="ALERT: ID3 Tag missing!"
Track = id3r.getValue('track')
if Track==None:
Track="ALERT: ID3 Tag missing!"
Year = id3r.getValue('year')
if Year==None:
Year="ALERT: ID3 Tag missing!"
i=i+1
print i,":"
print "Title: ",Title
print "Performer:", Performer
print "Track: ",Track
print "Year: ",Year
print myPath;
print "\n";
# Write data to file.
fo1.write(b1);
fo1.write(b2);
fo1.write(Title);
fo1.write(b3);
fo1.write(b4);
fo1.write(Performer);
fo1.write(b5);
fo1.write(b6);
fo1.write(Track);
fo1.write(b7);
fo1.write(b8);
fo1.write(Year);
fo1.write(b9);
fo1.write(b10);
fo1.write(myPath);
fo1.write(b11);
fo1.write(b12);
#Assign a variable for the XML closer and write it to file; close file.
c1 = ""
fo1.write(c1);
# Assign variables for the XSL printout.
d1 = '\n\n