-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqtDropboxImporter.pyw
executable file
·205 lines (167 loc) · 6.74 KB
/
qtDropboxImporter.pyw
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env python
"""Dropbox CameraUpload renamer
"""
from __future__ import division, print_function
import os
import sys
try:
from PySide import QtGui
except ImportError:
print("Fallback to PyQt")
import sip
sip.setapi(u'QDate', 2)
sip.setapi(u'QDateTime', 2)
sip.setapi(u'QString', 2)
sip.setapi(u'QTextStream', 2)
sip.setapi(u'QTime', 2)
sip.setapi(u'QUrl', 2)
sip.setapi(u'QVariant', 2)
from PyQt4 import QtGui
import dropboximport
class FileListWithDrop(QtGui.QListWidget):
"""Extend ListWidget with Drop capability."""
def __init__(self, parent):
super(FileListWithDrop, self).__init__(parent)
self.setAcceptDrops(True)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls:
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasUrls:
event.accept()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
for url in event.mimeData().urls():
self.addItem(url.toLocalFile())
else:
event.ignore()
class DropboxRenamerWindow(QtGui.QWidget):
"""Main window for the application"""
def __init__(self):
super(DropboxRenamerWindow, self).__init__()
# Add widgets
addbtn = QtGui.QPushButton("Add files", self)
delbtn = QtGui.QPushButton("Remove files", self)
self.filelist = FileListWithDrop(self)
self.filelist.setSelectionMode(
QtGui.QAbstractItemView.ExtendedSelection)
filegroupbox = QtGui.QGroupBox("Import files", self)
self.makedirscb = QtGui.QCheckBox("Put in folders", filegroupbox)
self.renfilescb = QtGui.QCheckBox("Rename files", filegroupbox)
dirlabel = QtGui.QLabel("Import to ...", filegroupbox)
self.dirname = QtGui.QLineEdit(self)
dirbtn = QtGui.QPushButton('...', self)
shiftgroupbox = QtGui.QGroupBox("Timeshift", self)
self.shiftimgcb = QtGui.QCheckBox("for images", shiftgroupbox)
self.shiftvidcb = QtGui.QCheckBox("for videos", shiftgroupbox)
shiftlabel = QtGui.QLabel("Shift time by", shiftgroupbox)
self.shifttime = QtGui.QSpinBox(shiftgroupbox)
importbtn = QtGui.QPushButton("Import", self)
# Connect signals
addbtn.clicked.connect(self.addFiles)
delbtn.clicked.connect(self.removeFiles)
dirbtn.clicked.connect(self.chooseDir)
importbtn.clicked.connect(self.renameFiles)
# Init widgets
self.init_gui_values()
# Do layout
mainbox = QtGui.QVBoxLayout()
filesbox = QtGui.QHBoxLayout()
filesbox.addWidget(addbtn)
filesbox.addWidget(delbtn)
mainbox.addLayout(filesbox)
mainbox.addWidget(self.filelist)
filebox = QtGui.QVBoxLayout()
filecbbox = QtGui.QHBoxLayout()
filecbbox.addWidget(self.makedirscb)
filecbbox.addWidget(self.renfilescb)
filebox.addLayout(filecbbox)
dirbox = QtGui.QHBoxLayout()
dirbox.addWidget(dirlabel)
dirbox.addWidget(self.dirname)
dirbox.addWidget(dirbtn)
filebox.addLayout(dirbox)
filegroupbox.setLayout(filebox)
mainbox.addWidget(filegroupbox)
shiftbox = QtGui.QVBoxLayout()
shiftcbbox = QtGui.QHBoxLayout()
shiftcbbox.addWidget(self.shiftimgcb)
shiftcbbox.addWidget(self.shiftvidcb)
shiftbox.addLayout(shiftcbbox)
timebox = QtGui.QHBoxLayout()
timebox.addWidget(shiftlabel)
timebox.addWidget(self.shifttime)
shiftbox.addLayout(timebox)
shiftgroupbox.setLayout(shiftbox)
mainbox.addWidget(shiftgroupbox)
mainbox.addWidget(importbtn)
self.setLayout(mainbox)
self.setWindowTitle("Dropbox photo import")
def init_gui_values(self):
if len(sys.argv) > 1:
self.filelist.addItems(sys.argv[1:])
self.dirname.setText(os.path.expanduser("~/Dropbox/Camera Uploads"))
self.shifttime.setRange(-24 * 60, 24 * 60)
self.shifttime.setValue(0)
self.shifttime.setSuffix(" min")
self.shiftvidcb.setChecked(True)
self.shiftimgcb.setChecked(False)
self.makedirscb.setChecked(False)
self.renfilescb.setChecked(True)
def addFiles(self):
dlg = QtGui.QFileDialog(self)
dlg.setAcceptMode(QtGui.QFileDialog.AcceptOpen)
dlg.setFileMode(QtGui.QFileDialog.ExistingFiles)
# dlg.setDirectory(os.path.abspath())
if dlg.exec_():
self.filelist.addItems(dlg.selectedFiles())
def removeFiles(self):
for item in self.filelist.selectedItems():
self.filelist.takeItem(self.filelist.row(item))
def chooseDir(self):
dlg = QtGui.QFileDialog(self)
dlg.setAcceptMode(QtGui.QFileDialog.AcceptOpen)
dlg.setFileMode(QtGui.QFileDialog.Directory)
dlg.setOption(QtGui.QFileDialog.ShowDirsOnly, True)
dlg.setDirectory(os.path.abspath(self.dirname.text()))
if dlg.exec_():
self.dirname.setText(dlg.selectedFiles()[0])
def renameFiles(self):
for index in range(self.filelist.count() - 1, -1, -1):
filename = self.filelist.item(index).text()
targetdir = self.dirname.text()
shift = 60 * self.shifttime.value()
shift_img = self.shiftimgcb.isChecked()
shift_vid = self.shiftvidcb.isChecked()
sort_in_dir = self.makedirscb.isChecked()
rename = self.renfilescb.isChecked()
status = dropboximport.import_file(filename, targetdir,
shift_seconds=shift,
shift_img=shift_img,
shift_vid=shift_vid,
sort_in_dir=sort_in_dir,
rename=rename)
if not status:
self.filelist.takeItem(index)
if self.filelist.count() > 0:
icon = QtGui.QMessageBox.Warning
title = "Warning"
text = 'Could not rename some files!'
infotext = 'Check files remained in the list'
else:
icon = QtGui.QMessageBox.Information
title = "Note"
text = 'Processing complete.'
infotext = 'No errors occured.'
mesg = QtGui.QMessageBox(icon, title, text)
mesg.setInformativeText(infotext)
mesg.exec_()
app = QtGui.QApplication(sys.argv)
window = DropboxRenamerWindow()
window.show()
sys.exit(app.exec_())