#!/usr/bin/env python

"""
 id3.py
 
 Classe per la lettura delle informazioni 
 ID3 v1.0 contenute nei file MP3 
	
 Copyright (C) 2000-2003 Michele Ferretti
 black.bird@tiscali.it
 http://www.blackbirdblog.it

 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 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.

"""

from sys import argv, exit
import string

# classe I
class ID3Tag:
	"Classe ID3Tag"
	
	# varuabili privte della classe
	titolo 		= 'vuoto'
	artista 	= 'vuoto'
	album 		= 'vuoto'
	anno 		= 'vuoto'
	commento 	= 'vuoto'
	genere 		= 'vuoto'
	
	__ispresente = False
	
	__generi = ('Blues',
              'Classic Rock',
              'Country',
              'Dance',
              'Disco',
              'Funk',
              'Grunge',
              'Hip-Hop',
              'Jazz',
              'Metal',
              'New Age',
              'Oldies',
              'Other',
              'Pop',
              'R&B',
              'Rap',
              'Reggae',
              'Rock',
              'Techno',
              'Industrial',
              'Alternative',
              'Ska',
              'Death Metal',
              'Pranks',
              'Soundtrack',
              'Euro-Techno',
              'Ambient',
              'Trip-Hop',
              'Vocal',
              'Jazz+Funk',
              'Fusion',
              'Trance',
              'Classical',
              'Instrumental',
              'Acid',
              'House',
              'Game',
              'Sound Clip',
              'Gospel',
              'Noise',
              'Alt. Rock',
              'Bass',
              'Soul',
              'Punk',
              'Space',
              'Meditative',
              'Instrum. Pop',
              'Instrum. Rock',
              'Ethnic',
              'Gothic',
              'Darkwave',
              'Techno-Indust.',
              'Electronic',
              'Pop-Folk',
              'Eurodance',
              'Dream',
              'Southern Rock',
              'Comedy',
              'Cult',
              'Gangsta',
              'Top 40',
              'Christian Rap',
              'Pop/Funk',
              'Jungle',
              'Native American',
              'Cabaret',
              'New Wave',
              'Psychadelic',
              'Rave',
              'Showtunes',
              'Trailer',
              'Lo-Fi',
              'Tribal',
              'Acid Punk',
              'Acid Jazz',
              'Polka',
              'Retro',
              'Musical',
              'Rock & Roll',
              'Hard Rock',
              'Folk',
              'Folk/Rock',
              'National Folk',
              'Swing',
              'Fusion',
              'Bebob',
              'Latin',
              'Revival',
              'Celtic',
              'Bluegrass',
              'Avantgarde',
              'Gothic Rock',
              'Progress. Rock',
              'Psychadel. Rock',
              'Symphonic Rock',
              'Slow Rock',
              'Big Band',
              'Chorus',
              'Easy Listening',
              'Acoustic',
              'Humour',
              'Speech',
              'Chanson',
              'Opera',
              'Chamber Music',
              'Sonata',
              'Symphony',
              'Booty Bass',
              'Primus',
              'Porn Groove',
              'Satire',
              'Genres')
	 
	def __init__(self,filename):
		"""
			Costruttore dell'oggetto
			preleva le informazioni contenute nel file mp3
		"""
		try:
			filemp3 = open(filename,'r')
			filemp3.seek(-128,2)
			id3 = filemp3.read(128)
			filemp3.close()
			
			if id3[0:3] == 'TAG':
				self.__ispresente = True
				self.titolo		= id3[3:33].strip()
				self.artista 	= id3[33:63].strip()
				self.album		= id3[63:93].strip()
				self.anno		= id3[93:97].strip()
				self.commento	= id3[97:127].strip()
				self.genere		= self.__generi[ord(id3[127])]
		except IOError, ioex:
			raise Exception('Si sono verificati dei problemi: %s' % ioex)

			
	def isPresent(self):
		"""Restituisce un bolleano che indica se 
			ci sono bel file mp3 le informazioni id3"""
		return self.__ispresente
		
		

if __name__ == '__main__':
	# prendo il nome del file
	if len(argv) == 2 and argv[1] != "":
		filename = argv[1]
	else:
		print 'Errore - Parametri non validi'	
		exit(1)
		
	try:
		f = ID3Tag(filename)
		print 'Nome del file MP3: <'+ filename +'>'
		print '------------------------------------'
		print 'Titolo:   ',f.titolo
		print 'Artista:  ',f.artista
		print 'Album:    ',f.album
		print 'Anno:     ',f.anno
		print 'Commento: ',f.commento
		print 'Genere:   ',f.genere
	except Exception, ex:
		print ex
