-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMediaSorter.py
71 lines (69 loc) · 2.59 KB
/
MediaSorter.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
import os
import re
import glob
import shutil
from tkinter import *
from tkinter import dialog
cwd = os.getcwd()
files = os.listdir(cwd)
def filelister():
tempfileStorage = []
for file in files:
if file.lower().endswith(('.avi','.mp4','.mkv')):
tempfileStorage.append(file)
else:
continue
if not tempfileStorage:
print("No suitable files found!")
else:
return tempfileStorage
def extensionremover(filename):
registry = '\.(avi|mp4|mkv)|([-])+([ ])+([0-9])+'
new_name = re.sub(registry,r'',filename)
return new_name
def crc16(data: bytes, poly=0x8408):
data = bytearray(data)
crc = 0xFFFF
for b in data:
cur_byte = 0xFF & b
for _ in range(0, 8):
if (crc & 0x0001) ^ (cur_byte & 0x0001):
crc = (crc >> 1) ^ poly
else:
crc >>= 1
cur_byte >>= 1
crc = (~crc & 0xFFFF)
crc = (crc << 8) | ((crc >> 8) & 0xFF)
return crc & 0xFFFF
def main():
for _ in files:
if _.lower().endswith(('.avi', '.mp4', '.mkv')):
print("Attempting to copy: ",_)
if _ == '.ipynb_checkpoints':
continue
if extensionremover(_) == _:
continue
else:
if _.lower().endswith(('.avi', '.mp4', '.mkv')):
if not os.path.exists(extensionremover(_)):
os.mkdir(extensionremover(_))
shutil.copy(os.path.join(cwd,_),os.path.join(cwd,extensionremover(_)))
print("Copy complete, verifying")
if crc16(open(os.path.join(cwd,_))) == crc16(open(os.path.join(cwd,extensionremover(_),_))):
os.remove(os.path.join(cwd,_))
print("File",_,"is verified")
else:
os.remove(os.path.join(cwd,extensionremover(_),_))
print("Copying Failed for: ",_)
else:
shutil.copy(os.path.join(cwd,_),os.path.join(cwd,extensionremover(_)))
print("Copy complete,verifying")
if crc16(open(os.path.join(cwd,_))) == crc16(open(os.path.join(cwd,extensionremover(_),_))):
os.remove(os.path.join(cwd,_))
print("File",_,"is verified")
else:
os.remove(os.path.join(cwd,extensionremover(_),_))
print("Copying Failed for: ",_)
if __name__ == '__main__':
main()
input('Press ENTER to EXIT')