[go: up one dir, main page]

Menu

[a37e2d]: / mp3drop.py  Maximize  Restore  History

Download this file

167 lines (151 with data), 5.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python
#-*- coding: utf-8 -*-
'''
Copy selected Artist files from collection folder to USB stick
into folders corresponding to ID3 tag signitures.
'''
import sys
import os
import shutil
from mutagen.easyid3 import EasyID3 as ID3
from PyQt4.QtGui import QApplication, QMainWindow, QFileDialog, QTreeWidget, QTreeWidgetItem, QProgressDialog
from PyQt4.QtCore import Qt
from dropDialog import Ui_MainWindow
EXPORT_PATH=u'%(artist)s-%(album)s/%(tracknumber)02i %(title)s.mp3'
EXPORT_PATH_MULTI=u'%(artist)s-%(album)s_%(discnumber)s/%(tracknumber)02i %(title)s.mp3'
CONVERSIONS=[
(u':', u'_'),
(u'"', u"'"),
(u'*', u'.'),
(u'?', u'.'),
(u'>', u''),
(u'<', u''),
(u'\x03', u''),
(u'\x19', u"'"),
(u'|', u"-"),
#(u'ö', u'oe'),
#(u'ä', u'ae'),
#(u'ü', u'ue'),
]
class FileList(QTreeWidget):
def __init__(self, parent, window):
QTreeWidget.__init__(self, parent)
self.setAcceptDrops(True)
self.mainWindow=window
self.artists={}
self.albums={}
def dragEnterEvent(self, event):
if event.mimeData().hasUrls:
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasUrls:
event.setDropAction(Qt.CopyAction)
event.accept()
else:
event.ignore()
def dropEvent(self, event):
urls=event.mimeData().urls()
pd=QProgressDialog(self.mainWindow)
pd.setMaximum(len(urls))
pd.show()
for i, url in enumerate(urls):
pd.setValue(i)
source=unicode(url.toLocalFile())
try:
destination, artist, album, title=self.getInfo(source)
except Exception, error:
try:
print error
except:
pass
continue
self.mainWindow.addFile(source, destination)
if not artist in self.artists:
item=QTreeWidgetItem([artist])
self.addTopLevelItem(item)
self.artists[artist]=item
if not (artist, album) in self.albums:
item=QTreeWidgetItem([album])
self.artists[artist].addChild(item)
self.albums[(artist, album)]=item
item=QTreeWidgetItem([title])
self.albums[(artist, album)].addChild(item)
pd.destroy()
self.sortItems(0, Qt.AscendingOrder)
def getInfo(self, filename):
id_=ID3(filename)
tag={}
for key, value in id_.items():
tag[key]=value[0].replace('/', '-').rstrip('.').strip()
tag['tracknumber']=int(tag['tracknumber'].split('-')[0])
if 'discnumber' in tag and not tag['discnumber'].endswith('-1'):
tag['discnumber']=tag['discnumber'].split('-')[0]
output=EXPORT_PATH_MULTI%tag
else:
output=EXPORT_PATH%tag
for conversion in CONVERSIONS:
output=output.replace(*conversion)
if 'discnumber' in tag:
return output, tag['artist'], tag['album'], u'%(discnumber)s%(tracknumber)02i-%(title)s'%tag
else:
return output, tag['artist'], tag['album'], u'%(tracknumber)02i-%(title)s'%tag
class DropDialog(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.ui=Ui_MainWindow()
self.ui.setupUi(self)
self.setAcceptDrops(True)
self.fileList=FileList(self.ui.fileListWidget, self)
self.ui.fileListWidget.layout().addWidget(self.fileList)
self.copy_list=[]
self.totalSize=0.
def selectDestination(self):
self.ui.destination.setText(QFileDialog.getExistingDirectory(parent=self,
directory=self.ui.destination.text()))
def addFile(self, source, destination):
self.copy_list.append((destination, source))
self.ui.noFiles.setText(str(len(self.copy_list)))
fsize=os.stat(source).st_size
self.totalSize+=fsize/1024.**2
self.ui.totalSize.setText('%.2f'%self.totalSize)
def clearList(self):
self.copy_list=[]
self.totalSize=0.
self.fileList.clear()
self.fileList.artists={}
self.fileList.albums={}
self.ui.noFiles.setText('0')
self.ui.totalSize.setText('0.')
def copyFiles(self):
self.copy_list.sort()
dest_folder=unicode(self.ui.destination.text())
pd=QProgressDialog()
pd.setMaximum(len(self.copy_list))
self._stopCopy=False
pd.canceled.connect(self._doStop)
for i, (destination, source) in enumerate(self.copy_list):
pd.setValue(i)
pd.setLabelText(destination)
QApplication.instance().processEvents()
if self._stopCopy:
break
to_name=os.path.join(dest_folder, destination)
if os.path.exists(to_name):
continue
print source+u'\n -> '+to_name
destfolder=os.path.dirname(to_name)
if not os.path.exists(destfolder):
os.makedirs(destfolder)
shutil.copy(source, to_name)
pd.destroy()
def _doStop(self):
self._stopCopy=True
def run():
app=QApplication(sys.argv)
dia=DropDialog()
dia.show()
app.exec_()
if __name__=='__main__':
sys.exit(run())