Changeset 42 for mymrc

Show
Ignore:
Timestamp:
07/22/07 21:56:32 (5 years ago)
Author:
dlefevre
Message:

Added complete functions to feed the database + bug fixes.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • mymrc/trunk/mymrc/src/db_utils.py

    r31 r42  
    55import string  
    66import ID3 
     7import time 
    78 
    89DBDriver( '/home/dlefevre/workspace/mymrc/src' ) 
     
    1314#qprint rs.fetchall() 
    1415 
     16directory = 0 
     17count = 0 
    1518 
    16 while True: 
    17     choices = """ 
    18 1 - Add song to database 
    19 2 - Add directory to db 
    20 q - Quit 
    21 """ 
    22     entry = raw_input(choices).strip() 
    23     if entry == '1': 
    24         entry = raw_input('path: ').strip() 
    25         if os.path.isfile(entry): 
    26             if string.lower(os.path.splitext(entry)[1]) == ".mp3": 
    27                 tags = ID3.ID3(entry) 
    28                 print tags.genre 
    29                 print tags.artist 
    30                 print tags.album 
    31             else: 
    32                 print "not MP3 file" 
    33     elif entry == '2': 
    34         entry = raw_input('path: ').strip() 
    35         print "Entry: ", entry 
    36         files = os.listdir(entry) 
    37         print "Files: ", files 
    38         for file in files: 
    39             if string.lower(os.path.splitext(file)[1]) == ".mp3": 
    40                 # tags 
    41                 tags = ID3.ID3(os.path.join(entry,file)) 
    42                 print tags 
    43                 #artist 
    44                 artist = 'Unknown' 
    45                 if tags.artist and len(tags.artist): 
    46                     print artist 
    47                     artist = tags.artist 
    48                 # title 
    49                 title = os.path.splitext(file)[0] 
    50                 #path 
    51                 path = os.path.join(entry,file) 
    52                 # genre 
    53                 genre = -1 
    54                 if tags.genre != 255: 
    55                     genre_id = tags.genre 
    56                 #path 
    57                 time=None 
    58                  
    59                 # album  
    60                 album = 'Unknown' 
    61                 if tags.album: 
    62                     album = tags.album 
    63                      
    64                      
    65                  
    66                                     
    67                 db = AudioQueries() 
    68                 rs = db.addArtist(tags.artist) 
    69                 print rs.fetchall() 
    70                  
    71                 try: 
    72                     db = AudioQueries() 
    73                     rs = db.addArtist(tags.artist) 
    74                     print rs.fetchall() 
    75                      
    76                     rs = db.addAlbum(tags.album) 
    77                     print rs.fetchall() 
    78                      
    79                     query = """ 
     19def addSong( file ): 
     20    artist = 'Unknown' 
     21    print file 
     22    title = os.path.splitext(os.path.split(file)[1])[0] 
     23    path = file 
     24    genre = -1 
     25    album = 'Unknown' 
     26         
     27    if string.lower(os.path.splitext(file)[1]) == ".mp3": 
     28        # tags 
     29        tags = ID3.ID3(os.path.join(entry,file)) 
     30         
     31        #artist 
     32        if tags.artist and len(tags.artist): 
     33            artist = tags.artist 
     34         
     35        # title 
     36        if tags.title: 
     37            title = tags.title 
     38         
     39        #path 
     40        path = file 
     41         
     42        # genre 
     43        genre = -1 
     44        if tags.genre != 255: 
     45            genre = tags.genre 
     46         
     47         
     48        # time not yet supported 
     49        time=None 
     50         
     51        # album  
     52        if tags.album: 
     53            album = tags.album 
     54                            
     55        db = AudioQueries() 
     56        db.addArtist(tags.artist) 
     57        db.addAlbum(tags.album) 
     58         
     59    else: return False   
     60    query = """ 
    8061INSERT INTO audio_song( 
    8162    artist_id, title, path, album_id, track_no, genre_id, time) 
     
    9071)     
    9172    """ 
    92                     rs = DBDriver().execute(query, (artist.title(), 
    93                                                     title,  
    94                                                     path, 
    95                                                     album.title(),  
    96                                                     tags.track, 
    97                                                     genre, 
    98                                                     time)) 
    99                     print rs.fetchall() 
    100                                         
     73    try: 
     74        DBDriver().execute(query, ( artist.title(), 
     75                                    title,  
     76                                    path, 
     77                                    album.title(),  
     78                                    tags.track, 
     79                                    genre, 
     80                                    time)) 
     81    except Exception: 
     82        return False 
     83    return True 
    10184 
    102                 except Exception, e: 
    103                     print e 
    104                 print 
    105                 print 
    10685 
     86def addDirectory(aDirectory, aRecursive=False): 
     87    global directory, count  
     88    if os.path.isdir(aDirectory): 
     89        items = os.listdir(aDirectory) 
     90        for item in items: 
     91            item = os.path.join(aDirectory, item) 
     92            if os.path.isfile(item): 
     93                if addSong( item ): 
     94                    count += 1 
     95            elif os.path.isdir(item) and aRecursive: 
     96                addDirectory(item, True) 
     97                directory += 1 
     98    else: 
     99        print "Invalid directory"     
     100 
     101 
     102 
     103 
     104 
     105while True: 
     106    choices = """ 
     1071 - Add song to database 
     1082 - Add directory to db 
     1093 - Add directory to db recursively 
     110q - Quit 
     111""" 
     112    entry = raw_input(choices).strip() 
     113    if entry == '1': 
     114        global count 
     115        count = 0 
     116        entry = raw_input('path: ').strip() 
     117        start = time.time() 
     118        if os.path.isfile(entry): 
     119            if string.lower(os.path.splitext(entry)[1]) == ".mp3":  
     120                if addSong( entry ): 
     121                    count += 1 
     122            else: print "No file added" 
     123        stop = time.time() 
     124        elapsed = stop - start 
     125        print "%d file(s) added"%(count) 
     126        print "%f seconds" %(elapsed) 
     127                 
     128    elif entry == '2': 
     129        global count 
     130        count = 0         
     131        entry = raw_input('path: ').strip() 
     132        start = time.time() 
     133        addDirectory(entry) 
     134        stop = time.time() 
     135        elapsed = stop - start 
     136        print "%d file(s) added"%(count) 
     137        print "%f seconds" %(elapsed) 
     138         
     139    elif entry == '3': 
     140        global directory, count 
     141        directory = 0 
     142        count = 0 
     143        entry = raw_input('path: ').strip() 
     144        start = time.time() 
     145        addDirectory(entry, True) 
     146        stop = time.time() 
     147        elapsed = stop - start 
     148        print "%d file(s) added over %d directory(ies)"%(count, directory) 
     149        print "%f seconds" %(elapsed) 
    107150             
    108151    elif entry == 'q':