- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
Latest commit
76 lines (61 loc) · 2.23 KB
/
Copy pathmain.py
File metadata and controls
76 lines (61 loc) · 2.23 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
fromos.pathimportexpanduser
fromosimportpath, listdir, sep
fromtimeimportsleep
fromplatformimportsystem
fromshutilimportunpack_archive
fromsend2trashimportsend2trash
frompy7zrimportSevenZipFile
fromwatchdog.observersimportObserver
fromwatchdog.eventsimportFileSystemEventHandler
defget_download_path():
"""Returns the default downloads path for linux or windows"""
ifsystem() =="Windows":
fromwinregimportOpenKey, QueryValueEx, HKEY_CURRENT_USER
sub_key=r'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
downloads_guid='{374DE290-123F-4565-9164-39C4925E467B}'
withOpenKey(HKEY_CURRENT_USER, sub_key) askey:
location=QueryValueEx(key, downloads_guid)[0]
delOpenKey, QueryValueEx, HKEY_CURRENT_USER
returnlocation
else:
returnpath.join(expanduser('~'), 'downloads')
downloads_path=get_download_path()
defextractor(file):
if".crdownload"infileor".tmp"infile:
return
formatted_file=file.split(sep)[-1].split(".")[0]
destination_name=f"Extracted {formatted_file}"
destination=downloads_path+sep+destination_name
try:
unpack_archive(
file,
destination)
sleep(0.5)
send2trash(file)
except:
try:
withSevenZipFile(file, mode='r') asarchive:
archive.extractall(path=destination)
sleep(0.5)
send2trash(file)
except:
pass
classEventHandler(FileSystemEventHandler):
def__init__(self):
self.whitelist= []
defon_modified(self, event):
ifevent.is_directoryisFalse:
file=event.src_path
iffilenotinself.whitelist:
self.whitelist.append(file)
extractor(file)
self.whitelist.remove(file)
if__name__=="__main__":
files=listdir(downloads_path)
forfileinfiles:
extractor(downloads_path+sep+file)
event_handler=EventHandler()
observer=Observer()
observer.schedule(event_handler, downloads_path, recursive=False)
observer.start()
observer.join()