Advertisement
gregwa

Article 23 Code

Apr 6th, 2011
514
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # Code for article 23
  3. import sys
  4. import os.path
  5. try:
  6.     from mutagen.mp3 import MP3
  7. except:
  8.     print "This application requires the mutagen python library."
  9. try:
  10.     import pygtk
  11.     pygtk.require("2.0")
  12. except:
  13.     pass
  14. try:
  15.     import gtk
  16.     import gtk.glade
  17. except:
  18.     sys.exit(1)
  19.    
  20. class PlayListCreator:
  21.     def __init__(self):
  22.         self.gladefile = "playlistmaker.glade"
  23.         self.wTree = gtk.glade.XML(self.gladefile,"MainWindow")
  24.         self.SetEventDictionary()
  25.         self.SetWidgetReferences()  
  26.         self.SetupToolTips()
  27.         self.SetupTreeview()      
  28.         self.CurrentPath = ""
  29.         self.CurrentRow = 0
  30.         self.RowCount = 0  
  31.        
  32.     def SetEventDictionary(self):
  33.         dict = {"on_MainWindow_destroy": gtk.main_quit,
  34.                 "on_tbtnQuit_clicked": gtk.main_quit,
  35.                 "on_tbtnAdd_clicked": self.on_tbtnAdd_clicked,
  36.                 "on_tbtnDelete_clicked": self.on_tbtnDelete_clicked,
  37.                 "on_tbtnClearAll_clicked": self.on_tbtnClearAll_clicked,
  38.                 "on_tbtnMoveToTop_clicked": self.on_tbtnMoveToTop_clicked,
  39.                 "on_tbtnMoveUp_clicked": self.on_tbtnMoveUp_clicked,
  40.                 "on_tbtnMoveDown_clicked": self.on_tbtnMoveDown_clicked,
  41.                 "on_tbtnMoveToBottom_clicked": self.on_tbtnMoveToBottom_clicked,                                                
  42.                 "on_tbtnAbout_clicked": self.on_tbtnAbout_clicked,
  43.                 "on_btnGetFolder_clicked": self.on_btnGetFolder_clicked,
  44.                 "on_txtFilename_key_press_event": self.txtFilenameKeyPress,
  45.                 "on_btnSavePlaylist_clicked": self.on_btnSavePlaylist_clicked}
  46.         self.wTree.signal_autoconnect(dict)
  47.  
  48.     def SetWidgetReferences(self):
  49.         self.txtFilename = self.wTree.get_widget("txtFilename")
  50.         self.txtPath = self.wTree.get_widget("txtPath")
  51.         self.tbtnAdd = self.wTree.get_widget("tbtnAdd")
  52.         self.tbtnDelete = self.wTree.get_widget("tbtnDelete")
  53.         self.tbtnClearAll = self.wTree.get_widget("tbtnClearAll")
  54.         self.tbtnQuit = self.wTree.get_widget("tbtnQuit")
  55.         self.tbtnAbout = self.wTree.get_widget("tbtnAbout")
  56.         self.tbtnMoveToTop = self.wTree.get_widget("tbtnMoveToTop")
  57.         self.tbtnMoveUp = self.wTree.get_widget("tbtnMoveUp")
  58.         self.tbtnMoveDown = self.wTree.get_widget("tbtnMoveDown")
  59.         self.tbtnMoveToBottom = self.wTree.get_widget("tbtnMoveToBottom")
  60.         self.btnGetFolder = self.wTree.get_widget("btnGetFolder")
  61.         self.btnSavePlaylist = self.wTree.get_widget("btnSavePlaylist")        
  62.         self.sbar = self.wTree.get_widget("statusbar1")
  63.         self.context_id = self.sbar.get_context_id("Statusbar")
  64.            
  65.     def SetupToolTips(self):
  66.         self.tbtnAdd.set_tooltip_text("Add a file or files to the playlist.")
  67.         self.tbtnAbout.set_tooltip_text("Display the About Information.")
  68.         self.tbtnDelete.set_tooltip_text("Delete selected entry from the list.")
  69.         self.tbtnClearAll.set_tooltip_text("Remove all entries from the list.")
  70.         self.tbtnQuit.set_tooltip_text("Quit this program.")
  71.         self.tbtnMoveToTop.set_tooltip_text("Move the selected entry to the top of the list.")
  72.         self.tbtnMoveUp.set_tooltip_text("Move the selected entry up in the list.")
  73.         self.tbtnMoveDown.set_tooltip_text("Move the selected entry down in the list.")
  74.         self.tbtnMoveToBottom.set_tooltip_text("Move the selected entry to the bottom of the list.")
  75.         self.btnGetFolder.set_tooltip_text("Select the folder that the playlist will be saved to.")
  76.         self.btnSavePlaylist.set_tooltip_text("Save the playlist.")
  77.         self.txtFilename.set_tooltip_text("Enter the filename to be saved here.  The extension '.m3u' will be added for you if you don't include it.")
  78.  
  79.     def SetupTreeview(self):
  80.         self.cFName = 0
  81.         self.cFType = 1
  82.         self.cFPath = 2
  83.         self.sFName = "Filename"
  84.         self.sFType = "Type"
  85.         self.sFPath = "Folder"
  86.         self.treeview = self.wTree.get_widget("treeview1")
  87.         self.AddPlaylistColumn(self.sFName,self.cFName)
  88.         self.AddPlaylistColumn(self.sFType,self.cFType)
  89.         self.AddPlaylistColumn(self.sFPath,self.cFPath)
  90.         self.playList = gtk.ListStore(str,str,str)
  91.         self.treeview.set_model(self.playList)
  92.         self.treeview.set_grid_lines(gtk.TREE_VIEW_GRID_LINES_BOTH)
  93.        
  94.     def AddPlaylistColumn(self,title,columnId):
  95.         column = gtk.TreeViewColumn(title,gtk.CellRendererText(),text=columnId)
  96.         column.set_resizable(True)
  97.         column.set_sort_column_id(columnId)
  98.         self.treeview.append_column(column)
  99.                
  100.     def on_tbtnAdd_clicked(self,widget):
  101.         fd = FileDialog()
  102.         selectedfiles,self.CurrentPath = fd.ShowDialog(0,self.CurrentPath)
  103.         self.AddFilesToTreeview(selectedfiles)        
  104.                
  105.     def on_tbtnDelete_clicked(self,widget):
  106.         sel = self.treeview.get_selection()
  107.         (model,rows) = sel.get_selected_rows()
  108.         iters=[]
  109.         for row in rows:
  110.             iters.append(self.playList.get_iter(row))
  111.         for i in iters:
  112.             if i is not None:
  113.                 self.playList.remove(i)
  114.                 self.RowCount -= 1
  115.         self.sbar.push(self.context_id,"%d files in list." % (self.RowCount))        
  116.        
  117.     def on_tbtnClearAll_clicked(self,widget):
  118.         self.playList.clear()        
  119.        
  120.     def on_tbtnMoveToTop_clicked(self,widget):
  121.         sel = self.treeview.get_selection()
  122.         (model,rows) = sel.get_selected_rows()
  123.         for path1 in rows:
  124.             path2 = 0
  125.         iter1=model.get_iter(path1)
  126.         iter2 = model.get_iter(path2)
  127.         model.move_before(iter1,iter2)        
  128.        
  129.     def on_tbtnMoveUp_clicked(self,widget):
  130.         sel = self.treeview.get_selection()
  131.         (model,rows) = sel.get_selected_rows()
  132.         for path1 in rows:
  133.             path2 = (path1[0]-1,)
  134.         if path2[0] >= 0:
  135.             iter1=model.get_iter(path1)
  136.             iter2 = model.get_iter(path2)
  137.             model.swap(iter1,iter2)  
  138.                  
  139.     def on_tbtnMoveDown_clicked(self,widget):
  140.         sel = self.treeview.get_selection()
  141.         (model,rows) = sel.get_selected_rows()
  142.         for path1 in rows:
  143.             path2 = (path1[0]+1,)
  144.         iter1=model.get_iter(path1)
  145.         if path2[0] <= self.RowCount-1:
  146.             iter2 = model.get_iter(path2)
  147.             model.swap(iter1,iter2)
  148.  
  149.     def on_tbtnMoveToBottom_clicked(self,widget):
  150.         sel = self.treeview.get_selection()
  151.         (model,rows) = sel.get_selected_rows()
  152.         for path1 in rows:
  153.             path2 = self.RowCount-1
  154.         iter1=model.get_iter(path1)
  155.         iter2 = model.get_iter(path2)
  156.         model.move_after(iter1,iter2)
  157.                
  158.     def on_tbtnAbout_clicked(self,widget):
  159.         self.ShowAbout()
  160.        
  161.     def on_btnGetFolder_clicked(self,widget):
  162.         fd = FileDialog()
  163.         filepath,self.CurrentPath = fd.ShowDialog(1,self.CurrentPath)
  164.         self.txtPath.set_text(filepath[0])
  165.        
  166.     def on_btnSavePlaylist_clicked(self,widget):
  167.         self.SavePlaylist()
  168.        
  169.     def txtFilenameKeyPress(self,widget,data):
  170.         if data.keyval == 65293: # The value of the return key
  171.             self.SavePlaylist()
  172.            
  173.     def AddFilesToTreeview(self,FileList):
  174.         counter = 0
  175.         for f in FileList:
  176.             extStart = f.rfind(".")
  177.             fnameStart = f.rfind("/")
  178.             extension = f[extStart+1:]
  179.             fname = f[fnameStart+1:extStart]
  180.             fpath = f[:fnameStart]
  181.             data = [fname,extension,fpath]
  182.             self.playList.append(data)
  183.             counter += 1
  184.         self.RowCount += counter
  185.         self.sbar.push(self.context_id,"%s files added for a total of %d" % (counter,self.RowCount))    
  186.        
  187.     def SavePlaylist(self):
  188.         fp = self.txtPath.get_text()     # Get the file path from the text box
  189.         fn = self.txtFilename.get_text() # Get the filename from the text box
  190.  
  191.         if fp == "":  # IF filepath is blank...
  192.             self.MessageBox("error","Please provide a filepath for the playlist.")
  193.         elif fn == "": # IF filename is blank...
  194.             self.MessageBox("error","Please provide a filename for the playlist file.")
  195.         else:  # Otherwise
  196.             extStart = fn.rfind(".") # Find the extension start position          
  197.             print extStart
  198.             if extStart == -1:
  199.                 fn += '.m3u' #append the extension if there isn't one.
  200.                 print "Added extension.  fn = %s" % fn
  201.                 self.txtFilename.set_text(fn) #replace the filename in the text box
  202.             if os.path.exists(fp + "/" + fn):
  203.                 self.MessageBox("error","The file already exists.  Please select another.")
  204.             else:    
  205.                 plfile = open(fp + "/" + fn,"w")  # Open the file
  206.                 plfile.writelines('#EXTM3U\n')    #Print the M3U header
  207.                 for row in self.playList:
  208.                     fname = "%s/%s.%s" % (row[2],row[0],row[1])
  209.                     artist,title,songlength = self.GetMP3Info(fname)
  210.                     if songlength > 0 and (artist != '' and title != ''):
  211.                         plfile.writelines("#EXTINF:%d,%s - %s\n" % (songlength,artist,title))
  212.                     #plfile.writelines("%s/%s.%s\n" % (row[2],row[0],row[1]))
  213.                     plfile.writelines("%s\n" % fname)
  214.                 plfile.close  # Finally Close the file
  215.                 self.MessageBox("info","Playlist file saved!")            
  216.        
  217.     def MessageBox(self,level,text):
  218.         if level == "info":
  219.             dlg = gtk.MessageDialog(None,0,gtk.MESSAGE_INFO,gtk.BUTTONS_OK,text)
  220.         elif level == "warning":
  221.             dlg = gtk.MessageDialog(None,0,gtk.MESSAGE_WARNING,gtk.BUTTONS_OK,text)
  222.         elif level == "error":
  223.             dlg = gtk.MessageDialog(None,0,gtk.MESSAGE_ERROR,gtk.BUTTONS_OK,text)
  224.         elif level == "question":
  225.             dlg = gtk.MessageDialog(None,0,gtk.MESSAGE_QUESTION,gtk.BUTTONS_YES_NO,text)
  226.         if level == "question":
  227.             resp = dlg.run()
  228.             dlg.destroy()
  229.             return resp
  230.         else:
  231.             resp = dlg.run()
  232.             dlg.destroy()    
  233.  
  234.     def ShowAbout(self):
  235.         about = gtk.AboutDialog()
  236.         about.set_program_name("Playlist Maker")
  237.         about.set_version("1.0")
  238.         about.set_copyright("(c) 2011 by Greg Walters")
  239.         comments = "Written for Full Circle Magazine\n"
  240.         comments += "This program will create a M3U format play list\n"
  241.         comments += "for use with any media player that supports the .m3u playlist format"
  242.         #about.set_comments("Written for Full Circle Magazine")
  243.         about.set_comments(comments)
  244.         about.set_logo(gtk.gdk.pixbuf_new_from_file("logo.png"))
  245.         about.set_website("http://thedesignatedgeek.com")
  246.         about.run()
  247.         about.destroy()            
  248.  
  249.     def GetMP3Info(self,filename):
  250.         artist = ''
  251.         title = ''
  252.         songlength = 0
  253.         audio = MP3(filename)
  254.         keys = audio.keys()
  255.         for key in keys:
  256.             try:
  257.                 if key == "TPE1":         # Artist
  258.                     artist = audio.get(key)
  259.             except:
  260.                 artist = ''
  261.             try:
  262.                 if key == "TIT2":         # Song Title
  263.                     title = audio.get(key)
  264.             except:
  265.                 title = ''
  266.             songlength = audio.info.length    # Audio Length
  267.         return (artist,title,songlength)    
  268.  
  269.  
  270.            
  271. class FileDialog:
  272.     def ShowDialog(self,which,CurrentPath):
  273.         if which == 0: # file chooser
  274.             dialog = gtk.FileChooserDialog("Select files to add...",None,
  275.                                gtk.FILE_CHOOSER_ACTION_OPEN,
  276.                                (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
  277.                                 gtk.STOCK_OPEN, gtk.RESPONSE_OK))
  278.             filter = gtk.FileFilter()
  279.             filter.set_name("Music Files")
  280.             filter.add_pattern("*.mp3")
  281.             filter.add_pattern("*.ogg")
  282.             filter.add_pattern("*.wav")
  283.             dialog.add_filter(filter)
  284.             filter = gtk.FileFilter()
  285.             filter.set_name("All files")
  286.             filter.add_pattern("*")
  287.             dialog.add_filter(filter)
  288.                                
  289.         else:          # folder chooser
  290.             dialog = gtk.FileChooserDialog("Select Save Folder..",None,
  291.                                 gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
  292.                                (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
  293.                                 gtk.STOCK_OPEN, gtk.RESPONSE_OK))
  294.            
  295.         dialog.set_default_response(gtk.RESPONSE_OK)
  296.         dialog.set_select_multiple(True)
  297.         if CurrentPath != "":
  298.             dialog.set_current_folder(CurrentPath)
  299.         response = dialog.run()
  300.         if response == gtk.RESPONSE_OK:
  301.             fileselection = dialog.get_filenames()
  302.             CurrentPath = dialog.get_current_folder()
  303.             dialog.destroy()
  304.             return (fileselection,CurrentPath)
  305.         elif response == gtk.RESPONSE_CANCEL:
  306.             print 'Closed, no files selected'
  307.             dialog.destroy()                
  308.             return ([],"")
  309.                            
  310. if __name__ == "__main__":
  311.     plc = PlayListCreator()
  312.     gtk.main()
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement