| 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 = """ |
| | 19 | def 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 = """ |
| | 86 | def 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 | |
| | 105 | while True: |
| | 106 | choices = """ |
| | 107 | 1 - Add song to database |
| | 108 | 2 - Add directory to db |
| | 109 | 3 - Add directory to db recursively |
| | 110 | q - 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) |