-
Notifications
You must be signed in to change notification settings - Fork 0
/
SFTools.py
477 lines (371 loc) · 18 KB
/
SFTools.py
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 25 18:15:26 2020
@author: SaFteiNZz
"""
#Constantes
GUIENLACE = 'resources/interfazMain.ui'
ICOENLACE = 'resources/sftools.png'
#Imports
import sys
#Procesos
import psutil
from subprocess import check_output,Popen, PIPE
#comandos cmd
import os
import win32com.shell.shell as shell
#sleep y threading
from threading import Thread
from time import sleep
from queue import Queue
from qtpy.QtCore import Qt, QFileSystemWatcher, QSettings, Signal
#youtube conversor (https://github.com/ytdl-org/youtube-dl)
import youtube_dl
#Ventana
from PyQt5 import uic
#Messagebox
import ctypes
#PyQT5
from PyQt5 import QtGui, QtCore
#Widgets pyQT5
from PyQt5.QtWidgets import QFileDialog, QMainWindow, QApplication
#Importar interfaz
Ui_MainWindow, QtBaseClass = uic.loadUiType(GUIENLACE)
# =============================================================================
# ~Funcion alertas messagebox
#
# @text comentario del messagebox
# @title titulo ventana messagebox
# @style (INT) tipo de ventana
# 0 : OK
# 1 : OK | Cancel
# 2 : Abort | Retry | Ignore
# 3 : Yes | No | Cancel
# 4 : Yes | No
# 5 : Retry | No
# 6 : Cancel | Try Again | Continue
#
# =============================================================================
def Messagebox(text, title, style):
return ctypes.windll.user32.MessageBoxW(0, text, title, style)
# =============================================================================
# ~Seleccionar Ruta de fichero/carpeta
#
# @filtro => Sera el tipo de extension
# @titulo => Titulo de la ventana
# @guardar => Booleano para saber si se quiere cargar o guardar
# - 1 = guardar
# - 0 = cargar
# Ejemplo filtro: "xls(*.xls);;csv(*.csv)"
#
# =============================================================================
def seleccionarFichero(filtro, titulo, guardar, carpetas):
qFD = QFileDialog()
if carpetas == 1:
return QFileDialog.getExistingDirectory(qFD, titulo, "", QFileDialog.ShowDirsOnly)
else:
if guardar == 0:
return QFileDialog.getOpenFileName(qFD, titulo, "",filtro)
elif guardar == 1:
return QFileDialog.getSaveFileName(qFD, titulo, "",filtro)
# =============================================================================
# ~Buscar proceso en los procesos del sistema
# =============================================================================
def has_handle(fpath):
for proc in psutil.process_iter():
try:
for item in proc.open_files():
if fpath == item.path:
return True
except Exception:
pass
return False
# =============================================================================
# ~Lanzar comando por cmd
#
# @myList => Array de comandos
#
# =============================================================================
def lanzarComando(admin, myList = [], *args):
filename = "C:/Users/SaFteiNZz/Documents/!testdelete/New Microsoft Word Document.docx"
if has_handle(filename):
print('holis')
# for proc in psutil.process_iter():
# try:
# # this returns the list of opened files by the current process
# flist = proc.open_files()
# if flist:
# print(proc.pid,proc.name)
# for nt in flist:
# print("\t",nt.path)
#
# # This catches a race condition where a process ends
# # before we can examine its files
# except psutil.NoSuchProcess as err:
# print("****",err)
# if admin == 1:
# for x in myList:
# shell.ShellExecuteEx(lpVerb='runas', lpFile='cmd.exe', lpParameters='/c '+x)
# else:
# for x in myList:
# os.system(x)
# =============================================================================
# ~Logger descargador
# =============================================================================
class MyLogger(object):
def debug(self, msg):
pass
def warning(self, msg):
pass
def error(self, msg):
print(msg)
##############################################################################################
#Fin de clase
# =============================================================================
# ~Clase main
# =============================================================================
class mainClass(QMainWindow):
finished = Signal()
updateProgress = Signal(str)
def __init__(self):
super(mainClass, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.setWindowIcon(QtGui.QIcon(ICOENLACE))
self.updateProgress.connect(self.ui.pTEStatus.appendPlainText)
# =============================================================================
# ~Eventos links
# =============================================================================
#Botones
#Boton resetear explorer
self.ui.pBResetExplorer.clicked.connect(self.resetExplorerClicked)
#Boton seleccionar archivo borrar
self.ui.pBSeleccionarArchivoBorrar.clicked.connect(self.seleccionarArchivoClicked)
#Boton forzar borrado archivo
self.ui.pBForzarBorradoArchivo.clicked.connect(self.borrarArchivoClicked)
#Boton seleccionar carpeta borrar
self.ui.pBSeleccionarCarpetaBorrar.clicked.connect(self.seleccionarCarpetaClicked)
#Boton forzar borrado carpeta
self.ui.pBForzarBorradoCarpeta.clicked.connect(self.borrarCarpetaClicked)
#Boton bajar video
self.ui.pBBajarVideo.clicked.connect(self.bajarVideoClicked)
#Boton bajar playlist
self.ui.pBPlaylistBajar.clicked.connect(self.bajarPlaylistClicked)
#Text edit video bajar
self.ui.tEVideoBajar.mousePressEvent = self.tEVideoBajarClicked
#Text edit playlist bajar
self.ui.tEPlaylistBajar.mousePressEvent = self.tEPlaylistBajarClicked
# =============================================================================
# ~Funciones eventos
# =============================================================================
# =============================================================================
# ~Evento clickar QTextEdit VideoBajar
# =============================================================================
def tEVideoBajarClicked(self, event):
self.ui.tEVideoBajar.setText('')
self.ui.tEVideoBajar.setTextColor(QtGui.QColor(0,0,0))
# =============================================================================
# ~Evento clickar QTextEdit PlaylistBajar
# =============================================================================
def tEPlaylistBajarClicked(self, event):
self.ui.tEPlaylistBajar.setText('')
self.ui.tEPlaylistBajar.setTextColor(QtGui.QColor(0,0,0))
# =============================================================================
# ~Evento clicar boton descarga videoç
# Se crea un hilo y se le asigna el trabajo
# =============================================================================
def bajarVideoClicked( self ):
rutaGuardar = seleccionarFichero("", "Extract path", 1, 1)
if not rutaGuardar:
Messagebox('You must enter a path in order to download', 'Error', 1)
return
mainWorker = Thread(target=self.bajarVideo, args=(rutaGuardar,))
mainWorker.start()
# =============================================================================
# ~Evento clicar boton descarga playlist
# Se crea un hilo y se le asigna el trabajo
# =============================================================================
def bajarPlaylistClicked( self ):
rutaGuardar = seleccionarFichero("", "Extract path", 1, 1)
if not rutaGuardar:
Messagebox('You must enter a path in order to download', 'Error', 1)
return
mainWorker = Thread(target=self.bajarPlaylist, args=(rutaGuardar, ))
mainWorker.start()
# =============================================================================
# ~Hook para estado descargas
# Funciona como worker (threading queue)
# @q => cola
#
# =============================================================================
def myHook( self, d):
if d['status'] == 'downloading':
self.updateProgress.emit('Downloading ' + os.path.splitext(os.path.basename(os.path.normpath(d['filename'])))[0] + '(' + d['_percent_str'] + ' )')
elif d['status'] == 'finished':
self.updateProgress.emit('! Done downloading ' + os.path.splitext(os.path.basename(os.path.normpath(d['filename'])))[0] + ', now deleting additional files...')
elif d['status'] == 'error':
self.updateProgress.emit('ERROR WHILE DOWNOLOADING ' + os.path.splitext(os.path.basename(os.path.normpath(d['filename'])))[0])
# =============================================================================
# ~Convertir playlist a urls
#
# @link es la url de la playlist
# @return retorna una lista de url de cada video de la playlist de manera individual
#
# =============================================================================
def getPlaylistVideosURL(self, link):
results = youtube_dl.YoutubeDL({'quiet': True}).extract_info(link, download=False)
return [i['webpage_url'] for i in results['entries']]
def worker( self, que):
while True:
args = que.get()
self.getMp3FromURL(args[1], args[0])
que.task_done()
# =============================================================================
# ~Descargar url a mp3
#
# =============================================================================
def getMp3FromURL( self, rutaGuardado, enlace):
# print(' -' + args[0] + '- ')
# filetmpl = u'%(id)s_%(uploader_id)s_%(title)s.%(ext)s'
# print(rutaGuardar + "/")
outtmpl = rutaGuardado + '/%(title)s.%(ext)s'
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': outtmpl,
'postprocessors': [
{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}
],
'progress_hooks': [self.myHook],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([enlace])
# info_dict = ydl.extract_info(link, download=True)
# print(info_dict)
# =============================================================================
# ~Programador de descargas
# =============================================================================
def downloadPlaylist(self, playlist, rutaGuardar):
length = len(playlist)
if length <= 1:
num_threads = 1
elif length <= 3:
num_threads = length
#Maximo de hilos 3
else:
num_threads = 3
que = Queue()
for i in range(num_threads):
worker = Thread(target=self.worker, args=(que,))
worker.setDaemon(True)
worker.start()
# self.updateProgress.emit('Begining download ..\n')
for video in playlist:
# print(video) #printear url (debug)
que.put((video, rutaGuardar))
self.updateProgress.emit('Download of ' + str(que.qsize()) + ' files started !')
que.join()
# =============================================================================
# ~Lanzar acción bajar video
# Se ejecuta desde otro hilo para evitar freeze de GUI
# =============================================================================
def bajarVideo( self , rutaGuardar):
self.downloadPlaylist([self.ui.tEVideoBajar.toPlainText()], rutaGuardar)
# =============================================================================
# ~Lanzar acción bajar Playlist
# Se ejecuta desde otro hilo para evitar freeze de GUI
# =============================================================================
def bajarPlaylist( self , rutaGuardar):
self.updateProgress.emit('Begining Download...')
self.updateProgress.emit('Extracting URLs...')
playlist = self.getPlaylistVideosURL(self.ui.tEPlaylistBajar.toPlainText())
self.downloadPlaylist(playlist, rutaGuardar)
# =============================================================================
# ~Evento resetear explorer
#
# taskkill [/s <computer> [/u [<Domain>\]<UserName> [/p [<Password>]]]] {[/fi <Filter>] [...] [/pid <ProcessID> | /im <ImageName>]} [/f] [/t]
#
# /s <computer> Specifies the name or IP address of a remote computer (do not use backslashes). The default is the local computer.
# /u <Domain>\<UserName> Runs the command with the account permissions of the user who is specified by UserName or Domain\UserName. /u can be specified only if /s is specified. The default is the permissions of the user who is currently logged on to the computer that is issuing the command.
# /p <Password> Specifies the password of the user account that is specified in the /u parameter.
# /fi <Filter> Applies a filter to select a set of tasks. You can use more than one filter or use the wildcard character (\*) to specify all tasks or image names. See the following table for valid filter names, operators, and values.
# /pid <ProcessID> Specifies the process ID of the process to be terminated.
# /im <ImageName> Specifies the image name of the process to be terminated. Use the wildcard character (\*) to specify all image names.
# /f Specifies that processes be forcefully terminated. This parameter is ignored for remote processes; all remote processes are forcefully terminated.
# /t Terminates the specified process and any child processes started by it.
#
# =============================================================================
def resetExplorerClicked(self):
comandos = ["taskkill /f /im explorer.exe", "start explorer.exe"]
lanzarComando(0, comandos)
# =============================================================================
# ~Evento borrar archivo
#
# del [/p] [/f] [/s] [/q] [/a[:]<attributes>] filename [/?]
#
# Brush up on how to read command syntax if you're not sure how to interpret the del command syntax as it's described above or in the list below.
#
# /p = Prompts for confirmation before deleting each file.
# /f = Force deletes read-only files.
# /s = Deletes the specified files from all the subdirectories.
# /q = Quiet mode; suppresses prompts for delete confirmations.
# /a:<attributes> = Deletes files based on one of the following attributes:
# r = Read-only files
# h = Hidden files
# i = Not content indexed files
# s = System files
# a = Files ready for archiving
# l = Reparse points
#
# =============================================================================
def borrarArchivoClicked(self):
head, tail = os.path.split(self.ui.lEArchivoBorrar.text())
# print(self.ui.lEArchivoBorrar.text())
# print(head + " " + tail)
print('del /f ' + self.ui.lEArchivoBorrar.text())
comandos = ["taskkill /f /im explorer.exe"]
lanzarComando(0, comandos)
comandos = ["del /f " + self.ui.lEArchivoBorrar.text()]
lanzarComando(1, comandos)
comandos = ["start explorer.exe"]
lanzarComando(0, comandos)
# comandos = ["taskkill /f /im explorer.exe", "cd " + head, "del /f " + tail, "start explorer.exe"]
# lanzarComando(1, comandos)
# =============================================================================
# ~Evento borrar carpeta
#
# RMDIR [/S] [/Q] [drive:]path
# RD [/S] [/Q] [drive:]path
# /S Removes all directories and files in the specified directory in addition to the directory itself. Used to remove a directory tree.
# /Q Quiet mode, do not ask if ok to remove a directory tree with /S
#
# =============================================================================
def borrarCarpetaClicked(self):
comandos = ["taskkill /f /im explorer.exe", "rd /s /q " + self.ui.lECarpetaBorrar.text(), "start explorer.exe"]
lanzarComando(1, comandos)
# =============================================================================
# ~Evento seleccionar archivo
# =============================================================================
def seleccionarArchivoClicked(self):
self.ui.lEArchivoBorrar.setText(seleccionarFichero("", "Seleccionar Archivo", 0, 0)[0])
# =============================================================================
# ~Evento seleccionar carpeta
# =============================================================================
def seleccionarCarpetaClicked(self):
self.ui.lECarpetaBorrar.setText(seleccionarFichero("", "Seleccionar carpeta", 0, 1)[0])
##############################################################################################
#Fin de clase
##
# Función MAIN
##
def main():
app = QApplication(sys.argv)
window = mainClass()
window.show()
app.exec_()
#Ejecucion
if __name__ == '__main__':
main()