Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- from ID3 import *
- import os
- from Tkinter import *
- import tkFileDialog
- class id3helper:
- def get_list_dir(self, path, start=0):
- if start:
- self.size = len(path)
- #print self.size
- else:
- pass
- elems = []
- directories = []
- music_files = []
- try:
- elems += os.listdir(path)
- except OSError:
- pass #permission denied
- for elem in elems:
- if os.path.isdir(path+'/'+elem):
- directories.append(path+'/'+elem)
- else:
- if elem[-4:] == '.mp3':
- music_files.append(path+'/'+elem)
- return directories, music_files
- def get_music(self, start_path):
- self.directories, self.music_files = self.get_list_dir(start_path ,1)
- while self.directories:
- current_dir = self.directories.pop()
- cdirectories, cmusic_files = self.get_list_dir(current_dir)
- self.directories += cdirectories
- self.music_files += cmusic_files
- return self.music_files
- def get_id3(self, music_list, tag):
- id_music = {}
- for music_file in music_list:
- try:
- id3info = ID3(music_file)
- if not(id3info[tag] in id_music):
- id_music[id3info[tag]] = []
- id_music[id3info[tag]].append(music_file)
- except KeyError:
- if not('%unknown%' in id_music):
- id_music['%unknown%'] = []
- id_music['%unknown%'].append(music_file)
- del id3info
- return id_music
- def set_tags():
- global tag_map
- #print ntag.get(), list.curselection()
- for music_file in tag_map[tag_map.keys()[int(list.curselection()[0])]]:
- id3s = ID3(music_file)
- id3s['ARTIST'] = ntag.get()
- id3s.write()
- del id3s
- scan_directory(True)
- path = 0
- tag_map = 0
- def scan_directory(refresh=False):
- global path,tag_map
- if not refresh:
- path = tkFileDialog.askdirectory(title='Select directory')
- if path:
- music_lib = id3helper()
- ml = music_lib.get_music(path)
- tag_map = music_lib.get_id3(ml, 'ARTIST')
- del music_lib
- list.delete(0, END)
- for i in tag_map:
- list.insert(END, i)
- root = Tk()
- root.title('flawless')
- list = Listbox(root)
- list.pack()
- ntag = Entry(root)
- ntag.pack()
- buttons = Frame(root)
- buttons.pack()
- button_open_directory = Button(buttons, text='Open', command=scan_directory)
- button_open_directory.pack()
- button_set_tags = Button(buttons, text='Set tags', command=set_tags)
- button_set_tags.pack()
- scan_directory()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement