|
| 1 | +import eyed3 |
| 2 | +import glob |
| 3 | +import wx |
| 4 | + |
| 5 | + |
| 6 | +class EditDialog(wx.Dialog): |
| 7 | + def __init__(self, mp3): |
| 8 | + title = 'Editing "{title}"'.format(title=mp3.tag.title) |
| 9 | + super().__init__(parent=None, title=title) |
| 10 | + |
| 11 | + self.mp3 = mp3 |
| 12 | + |
| 13 | + self.main_sizer = wx.BoxSizer(wx.VERTICAL) |
| 14 | + |
| 15 | + self.artist = wx.TextCtrl(self, value=self.mp3.tag.artist) |
| 16 | + self.add_widgets("Artist", self.artist) |
| 17 | + |
| 18 | + self.album = wx.TextCtrl(self, value=self.mp3.tag.album) |
| 19 | + self.add_widgets("Album", self.album) |
| 20 | + |
| 21 | + self.title = wx.TextCtrl(self, value=self.mp3.tag.title) |
| 22 | + self.add_widgets("Title", self.title) |
| 23 | + |
| 24 | + btn_sizer = wx.BoxSizer() |
| 25 | + save_btn = wx.Button(self, label="Save") |
| 26 | + save_btn.Bind(wx.EVT_BUTTON, self.on_save) |
| 27 | + |
| 28 | + btn_sizer.Add(save_btn, 0, wx.ALL, 5) |
| 29 | + btn_sizer.Add(wx.Button(self, id=wx.ID_CANCEL), 0, wx.ALL, 5) |
| 30 | + self.main_sizer.Add(btn_sizer, 0, wx.CENTER) |
| 31 | + |
| 32 | + self.SetSizer(self.main_sizer) |
| 33 | + |
| 34 | + def add_widgets(self, label_text, text_ctrl): |
| 35 | + row_sizer = wx.BoxSizer(wx.HORIZONTAL) |
| 36 | + label = wx.StaticText(self, label=label_text, size=(50, -1)) |
| 37 | + row_sizer.Add(label, 0, wx.ALL, 5) |
| 38 | + row_sizer.Add(text_ctrl, 1, wx.ALL | wx.EXPAND, 5) |
| 39 | + self.main_sizer.Add(row_sizer, 0, wx.EXPAND) |
| 40 | + |
| 41 | + def on_save(self, event): |
| 42 | + self.mp3.tag.artist = self.artist.GetValue() |
| 43 | + self.mp3.tag.album = self.album.GetValue() |
| 44 | + self.mp3.tag.title = self.title.GetValue() |
| 45 | + self.mp3.tag.save() |
| 46 | + self.Close() |
| 47 | + |
| 48 | + |
| 49 | +class Mp3Panel(wx.Panel): |
| 50 | + def __init__(self, parent): |
| 51 | + super().__init__(parent) |
| 52 | + main_sizer = wx.BoxSizer(wx.VERTICAL) |
| 53 | + self.row_obj_dict = {} |
| 54 | + self.current_folder_path = None |
| 55 | + |
| 56 | + self.list_ctrl = wx.ListCtrl( |
| 57 | + self, size=(-1, 100), style=wx.LC_REPORT | wx.BORDER_SUNKEN |
| 58 | + ) |
| 59 | + self.list_ctrl.InsertColumn(0, "Artist", width=140) |
| 60 | + self.list_ctrl.InsertColumn(1, "Album", width=140) |
| 61 | + self.list_ctrl.InsertColumn(2, "Title", width=200) |
| 62 | + self.list_ctrl.InsertColumn(3, "Year", width=200) |
| 63 | + main_sizer.Add(self.list_ctrl, 0, wx.ALL | wx.EXPAND, 5) |
| 64 | + |
| 65 | + edit_button = wx.Button(self, label="Edit") |
| 66 | + edit_button.Bind(wx.EVT_BUTTON, self.on_edit) |
| 67 | + main_sizer.Add(edit_button, 0, wx.ALL | wx.CENTER, 5) |
| 68 | + |
| 69 | + self.SetSizer(main_sizer) |
| 70 | + |
| 71 | + def on_edit(self, event): |
| 72 | + selection = self.list_ctrl.GetFocusedItem() |
| 73 | + if selection >= 0: |
| 74 | + mp3 = self.row_obj_dict[selection] |
| 75 | + dlg = EditDialog(mp3) |
| 76 | + dlg.ShowModal() |
| 77 | + self.update_mp3_listing(self.current_folder_path) |
| 78 | + dlg.Destroy() |
| 79 | + |
| 80 | + def update_mp3_listing(self, folder_path): |
| 81 | + self.current_folder_path = folder_path |
| 82 | + self.list_ctrl.ClearAll() |
| 83 | + |
| 84 | + self.list_ctrl.InsertColumn(0, "Artist", width=140) |
| 85 | + self.list_ctrl.InsertColumn(1, "Album", width=140) |
| 86 | + self.list_ctrl.InsertColumn(2, "Title", width=200) |
| 87 | + self.list_ctrl.InsertColumn(3, "Year", width=200) |
| 88 | + |
| 89 | + mp3s = glob.glob(folder_path + "/*.mp3") |
| 90 | + mp3_objects = [] |
| 91 | + index = 0 |
| 92 | + for mp3 in mp3s: |
| 93 | + mp3_object = eyed3.load(mp3) |
| 94 | + self.list_ctrl.InsertItem(index, mp3_object.tag.artist) |
| 95 | + self.list_ctrl.SetItem(index, 1, mp3_object.tag.album) |
| 96 | + self.list_ctrl.SetItem(index, 2, mp3_object.tag.title) |
| 97 | + mp3_objects.append(mp3_object) |
| 98 | + self.row_obj_dict[index] = mp3_object |
| 99 | + index += 1 |
| 100 | + |
| 101 | + |
| 102 | +class Mp3Frame(wx.Frame): |
| 103 | + def __init__(self): |
| 104 | + super().__init__(parent=None, title="Mp3 Tag Editor") |
| 105 | + self.panel = Mp3Panel(self) |
| 106 | + self.create_menu() |
| 107 | + self.Show() |
| 108 | + |
| 109 | + def create_menu(self): |
| 110 | + menu_bar = wx.MenuBar() |
| 111 | + file_menu = wx.Menu() |
| 112 | + open_folder_menu_item = file_menu.Append( |
| 113 | + wx.NewId(), "Open Folder", "Open a folder with MP3s" |
| 114 | + ) |
| 115 | + menu_bar.Append(file_menu, "&File") |
| 116 | + self.Bind(wx.EVT_MENU, self.on_open_folder, open_folder_menu_item) |
| 117 | + self.SetMenuBar(menu_bar) |
| 118 | + |
| 119 | + def on_open_folder(self, event): |
| 120 | + title = "Choose a directory:" |
| 121 | + dlg = wx.DirDialog(self, title, style=wx.DD_DEFAULT_STYLE) |
| 122 | + if dlg.ShowModal() == wx.ID_OK: |
| 123 | + self.panel.update_mp3_listing(dlg.GetPath()) |
| 124 | + dlg.Destroy() |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + app = wx.App(False) |
| 129 | + frame = Mp3Frame() |
| 130 | + app.MainLoop() |
0 commit comments