Advertisement
Guest User

Untitled

a guest
Nov 17th, 2011
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. from ID3 import *
  4. import os
  5. from Tkinter import *
  6. import tkFileDialog
  7. class id3helper:
  8. def get_list_dir(self, path, start=0):
  9. if start:
  10. self.size = len(path)
  11. #print self.size
  12. else:
  13. pass
  14. elems = []
  15. directories = []
  16. music_files = []
  17. try:
  18. elems += os.listdir(path)
  19. except OSError:
  20. pass #permission denied
  21. for elem in elems:
  22. if os.path.isdir(path+'/'+elem):
  23. directories.append(path+'/'+elem)
  24. else:
  25. if elem[-4:] == '.mp3':
  26. music_files.append(path+'/'+elem)
  27. return directories, music_files
  28. def get_music(self, start_path):
  29. self.directories, self.music_files = self.get_list_dir(start_path ,1)
  30. while self.directories:
  31. current_dir = self.directories.pop()
  32. cdirectories, cmusic_files = self.get_list_dir(current_dir)
  33. self.directories += cdirectories
  34. self.music_files += cmusic_files
  35. return self.music_files
  36. def get_id3(self, music_list, tag):
  37. id_music = {}
  38. for music_file in music_list:
  39. try:
  40. id3info = ID3(music_file)
  41. if not(id3info[tag] in id_music):
  42. id_music[id3info[tag]] = []
  43. id_music[id3info[tag]].append(music_file)
  44. except KeyError:
  45. if not('%unknown%' in id_music):
  46. id_music['%unknown%'] = []
  47. id_music['%unknown%'].append(music_file)
  48. del id3info
  49. return id_music
  50. def set_tags():
  51. global tag_map
  52. #print ntag.get(), list.curselection()
  53. for music_file in tag_map[tag_map.keys()[int(list.curselection()[0])]]:
  54. id3s = ID3(music_file)
  55. id3s['ARTIST'] = ntag.get()
  56. id3s.write()
  57. del id3s
  58. scan_directory(True)
  59. path = 0
  60. tag_map = 0
  61. def scan_directory(refresh=False):
  62. global path,tag_map
  63. if not refresh:
  64. path = tkFileDialog.askdirectory(title='Select directory')
  65. if path:
  66. music_lib = id3helper()
  67. ml = music_lib.get_music(path)
  68. tag_map = music_lib.get_id3(ml, 'ARTIST')
  69. del music_lib
  70. list.delete(0, END)
  71. for i in tag_map:
  72. list.insert(END, i)
  73. root = Tk()
  74. root.title('flawless')
  75. list = Listbox(root)
  76. list.pack()
  77. ntag = Entry(root)
  78. ntag.pack()
  79. buttons = Frame(root)
  80. buttons.pack()
  81. button_open_directory = Button(buttons, text='Open', command=scan_directory)
  82. button_open_directory.pack()
  83. button_set_tags = Button(buttons, text='Set tags', command=set_tags)
  84. button_set_tags.pack()
  85. scan_directory()
  86. root.mainloop()
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement