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
| # 사용법
# J:\test.py "J:\Torrent\IP\Zombieland Double Tap 2019 PROPER HYBRID 2160p UHD Blu-ray Remux DoVi HDR DTS-HD MA 7.1-HDT.mkv" "Zombieland Double Tap 2019 PROPER HYBRID 2160p UHD Blu-ray Remux DoVi HDR DTS-HD MA 7.1-HDT.mkv" "IP_test"
# J:\test.py "J:\Torrent\IP\Spin.Me.Round.2022.1080p.BluRay.H264.AAC.MP4-MTDz.mp4" "Spin.Me.Round.2022.1080p.BluRay.H264.AAC.MP4-MTDz.mp4" "IP_test"
# J:\test.py "J:\Torrent\IP\Thor 2011 br 10bit hdr dts hevc-d3g\Thor (2011) UltraHD BluRay HDR10 10Bit 2160p Dts-HDMa7.1 HEVC-d3g.mkv" "Thor 2011 br 10bit hdr dts hevc-d3g" "IP_test"
# J:\test.py "J:\Torrent\IP\The Revenant 2015 2160p UHD BluRay Hybrid DV HDR10+ DDP 7.1 x265-BiTOR" "The Revenant 2015 2160p UHD BluRay Hybrid DV HDR10+ DDP 7.1 x265-BiTOR" "IP_test"
# J:\test.py "J:\Torrent\IP\The.Core.2003.HDR.2160p.WEB.H265-HEATHEN" "The.Core.2003.HDR.2160p.WEB.H265-HEATHEN" "IP_test"
import os
import sys
import shutil
from datetime import datetime
import time
from pathlib import Path
import rarfile
import psutil
import re
def printLog(message):
with open(log_file, "a", encoding="utf-8") as f:
f.write(message + "\n")
print(message)
def disk_usage_check():
condition = True
while condition:
read = 0
write = 0
disk_io_counters = psutil.disk_io_counters(perdisk=True)
for disk, counters in disk_io_counters.items():
if disk == "PhysicalDrive1":
read = counters.read_bytes
write = counters.write_bytes
time.sleep(60)
disk_io_counters = psutil.disk_io_counters(perdisk=True)
for disk, counters in disk_io_counters.items():
if disk == "PhysicalDrive1":
if 600 < int((counters.read_bytes - read)/(1024 ** 2) + (counters.write_bytes - write)/(1024 ** 2)):
printLog(datetime.now().strftime("%Y-%m-%d %H:%M") + " : Read {:.2f} MB/m".format((counters.read_bytes - read)/(1024 ** 2)) + ", Write {:.2f} MB/m".format((counters.write_bytes - write)/(1024 ** 2)))
print("Usage is too high")
else:
condition = False
print("Usage is low")
def format_movie_title_1(input_str):
# 영화 제목과 연도 추출
match = re.search(r'^(.+)\.(\d{4})\.', input_str)
if match:
movie_title = match.group(1).replace('.', ' ')
year = match.group(2)
formatted_title = f"{movie_title} ({year})"
return formatted_title
else:
return format_movie_title_2(input_str)
def format_movie_title_2(input_str):
# 영화 제목과 연도 추출
match = re.search(r'^(.+?)\s+(\d{4})\s+', input_str)
if match:
movie_title = match.group(1)
year = match.group(2)
formatted_title = f"{movie_title} ({year})"
return formatted_title
else:
return input_str
def process_file(file, destination):
file_size_bytes = os.path.getsize(file)
file_size_gb = file_size_bytes / (1024 ** 3)
printLog("{:.2f} GB".format(file_size_gb) + " : " + file)
if file.endswith(".mkv") or file.endswith(".mp4"):
# disk_usage_check()
shutil.copy(file, destination)
if file.endswith(".rar"):
# disk_usage_check()
rar_file = rarfile.RarFile(file)
try:
rar_file.extractall(path=destination)
rar_file.close()
except:
# os.remove(tmp_file)
printLog("Error extract")
sys.exit()
def process_directory(directory, destination):
for root, dirs, files in os.walk(directory):
for file in files:
full_path = os.path.join(root, file)
process_file(full_path, destination)
print("The script name is:", sys.argv[0])
print("The first argument is:", sys.argv[1])
print("The second argument is:", sys.argv[2])
print("The thrid argument is:", sys.argv[3])
# Set variables for file paths
file_path = sys.argv[1]
file_name = sys.argv[2]
parent_dir = sys.argv[3]
tmp_file = "J:\dostuff_is_doing_stuff.tmp"
log_file = "J:\Log\{}_{}.log".format(datetime.now().strftime("%Y-%m-%d_%H-%M"), file_name)
printLog("Log file name : " + log_file)
with open(log_file, "w", encoding="utf-8") as f:
f.write(datetime.now().strftime("%Y-%m-%d %H:%M") + "\n")
f.write("J:\\test.py " + "\"" + file_path + "\" \"" + file_name + "\" \"" + parent_dir + "\"\n\n")
f.write("file_path : " + file_path + "\n")
f.write("file_path : \"" + str(Path(file_path).parent) + "\"\n")
f.write("file_name : " + file_name + "\n")
f.write("parent_dir : " + parent_dir + "\n")
printLog(datetime.now().strftime("%Y-%m-%d %H:%M") + " : Start")
if file_name.endswith(".mkv") or file_name.endswith(".mp4"):
new_folder = os.path.splitext(file_name)[0]
else:
new_folder = file_name
new_folder = format_movie_title_1(new_folder)
destination = "W:\Torrent\\" + parent_dir + "\\" + new_folder
# print("Destination folder name : " + new_folder)
shutil.rmtree(destination, ignore_errors=True)
folder = Path(destination)
folder.mkdir(parents=True, exist_ok=True)
printLog(f"Folder created at {destination}")
if os.path.isfile(file_path):
print("Is file : " + file_path)
file_size_bytes = os.path.getsize(file_path)
file_size_gb = file_size_bytes / (1024 ** 3)
printLog("{:.2f} GB".format(file_size_gb) + " : " + file_path)
# disk_usage_check()
shutil.copy(file_path, destination)
else:
process_directory(file_path, destination)
printLog(datetime.now().strftime("%Y-%m-%d %H:%M") + " : Complete")
printLog("Dir : \"" + destination + "\"")
for file in os.scandir(destination):
if file.is_file():
file_size_bytes = os.path.getsize(file)
file_size_gb = file_size_bytes / (1024 ** 3)
printLog("{:.2f} GB".format(file_size_gb) + " : " + file.name)
printLog(datetime.now().strftime("%Y-%m-%d %H:%M") + " : Done")
os.rename(Path(log_file), str(Path(log_file).parent) + "\\Done_" + Path(log_file).name)
|