Advertisement
Guest User

Full Circle Magazine - Python Part 9

a guest
Mar 12th, 2010
1,005
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.36 KB | None | 0 0
  1. def WalkThePath(musicpath):
  2.  
  3.     ecntr = 0  # Error Counter
  4.  
  5.     rcntr = 0  # Folder Counter
  6.  
  7.     fcntr = 0  # File Counter
  8.  
  9.     # Open the error log file
  10.  
  11.     efile = open('errors.log',"w")
  12.  
  13.     for root, dirs, files in os.walk(musicpath):
  14.  
  15.         rcntr += 1  # This is the number of folders we have walked
  16.  
  17.         for file in [f for f in files if f.endswith(".mp3")]:
  18.  
  19.             fcntr += 1  # This is the number of mp3 files we found
  20.  
  21.             # Clear the holding variables each pass
  22.  
  23.             _title=''
  24.  
  25.             _artist=''
  26.  
  27.             _album=''
  28.  
  29.             _genre=''
  30.  
  31.             _year = ''
  32.  
  33.             _bitrate=''
  34.  
  35.             _length=''
  36.  
  37.             _fsize=''
  38.  
  39.             _track = 0
  40.  
  41.             # Combine path and filename to create a single variable.
  42.  
  43.             fn = join(root,file)  
  44.  
  45.             try:
  46.  
  47.                 audio = MP3(fn)
  48.  
  49.                 keys = audio.keys()
  50.  
  51.                 for key in keys:
  52.  
  53.                     if key == 'TDRC':           # Year
  54.  
  55.                         _year = audio.get(key)
  56.  
  57.                     elif key == 'TALB':         # Album
  58.  
  59.                         _album = audio.get(key)
  60.  
  61.                     elif key == 'TRCK':         # Track
  62.  
  63.                         try:
  64.  
  65.                             _trk = audio.get(key)
  66.  
  67.                             if _trk[0].find("/"):
  68.  
  69.                                 _trk1 = _trk[0]
  70.  
  71.                                 _track=_trk1[_trk1.find("/")+1]
  72.  
  73.                             elif len(trk[0]) == 0:
  74.  
  75.                                 _track = 0
  76.  
  77.                             else:
  78.  
  79.                                 _track = _trk[0]
  80.  
  81.                         except:
  82.  
  83.                             track = 0
  84.  
  85.                     elif key == "TPE1":         # Artist
  86.  
  87.                         _artist = audio.get(key)
  88.  
  89.                     elif key == "TIT2":         # Song Title
  90.  
  91.                         _title = audio.get(key)
  92.  
  93.                     elif key == "TCON":         # Genre
  94.  
  95.                         _genre = audio.get(key)
  96.  
  97.                 _bitrate = audio.info.bitrate   # Bitrate
  98.  
  99.                 _length = S2HMS(audio.info.length)    # Audio Length
  100.  
  101.                 _fsize = getsize(fn)            # File Size
  102.  
  103.                 # Now write the database
  104.  
  105.                 # This is a different way of doing it from last time.  Works much better.
  106.  
  107.                 sql = 'INSERT INTO mp3 (title,artist,album,genre,year,track,bitrate,playtime,filesize,path,filename) VALUES (?,?,?,?,?,?,?,?,?,?,?)'
  108.  
  109.                 cursor.execute(sql,(str(_title),str(_artist),str(_album),str(_genre),str(_year),int(_track),str(_bitrate),str(_length),str(_fsize),root,file))
  110.  
  111.             except ValueError:
  112.  
  113.                 ecntr += 1
  114.  
  115.                 efile.writelines('===========================================\n')
  116.  
  117.                 efile.writelines('VALUE ERROR - Filename: %s\n' % fn)
  118.  
  119.                 efile.writelines('Title: %s - Artist: %s - Album: %s\n' %(_title,_artist,_album))
  120.  
  121.                 efile.writelines('Genre: %s - Year: %s - Track: %s\n' % (_genre,_year,_track))
  122.  
  123.                 efile.writelines('bitrate: {0} - length: {1} \n'.format(_bitrate,_length))              
  124.  
  125.                 efile.writelines('===========================================\n')
  126.  
  127.             except TypeError:
  128.  
  129.                 ecntr += 1
  130.  
  131.                 efile.writelines('===========================================\n')
  132.  
  133.                 efile.writelines('TYPE ERROR - Filename: {0}\n'.format(fn))
  134.  
  135.                 efile.writelines('Title: {0} - Artist: {1} - Album: {2}\n'.format(_title,_artist,_album))
  136.  
  137.                 efile.writelines('Genre: {0} - Year: {1} - Track: {2}\n'.format(_genre,_year,_track))
  138.  
  139.                 efile.writelines('bitrate: {0} - length: {1} \n'.format(_bitrate,_length))
  140.  
  141.                 efile.writelines('===========================================\n')
  142.  
  143.             except:
  144.  
  145.                 ecntr += 1
  146.  
  147.                 efile.writelines('TYPE ERROR - Filename: {0}\n'.format(fn))
  148.  
  149.             print fcntr
  150.  
  151.         # Close the log file
  152.  
  153.         efile.close
  154.  
  155.     # Finish Up
  156.  
  157.     print ("\n")
  158.  
  159.     print ("Number of errors: {0}").format(ecntr)
  160.  
  161.     print ("Number of files processed: {0}").format(fcntr)
  162.  
  163.     print ("Number of folders processed: {0}").format(rcntr)
  164.  
  165.     # End of WalkThePath
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement