-
Notifications
You must be signed in to change notification settings - Fork 1
/
__main__.py
384 lines (308 loc) · 13.5 KB
/
__main__.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
import os
import sys
from multiprocessing import Process, Value
from random import shuffle
from shutil import copy
from time import time as timestamp
from datetime import datetime
from pytz import timezone
class Compress:
def __init__(self, remote=None, local=None, **kwargs):
self.kwargs = kwargs
self.remote = remote
self.local = local
# Semaphore to count files compressed
self.value = Value('i', 0)
self.count = 0
if self.remote is None:
self.remote = input("Enter Remote URL : ")
if self.local is None:
self.local = input("Enter Local URL : ")
self.encrypt_ = kwargs.get('encrypt_') # encrypt while compressing
self.skip = 0
self.top_dir = self.remote.split('/')[-1] # name of the main folder
self.local = os.path.join(self.local, self.top_dir) # local abs path
self.files = [] # files to compress
self.files_by_ext = {}
self.video = ['mkv', 'mov', 'mp4']
self.not_down = ['vtt']
# resolution
if kwargs.get('res'):
self.res = kwargs.get('res')
else:
self.res = '720'
if kwargs.get('quitIfFolderExists'):
if os.path.exists(self.local):
sys.exit('Exiting - folder exists')
print('\n{:=^100}\n'.format(os.path.basename(self.remote).upper()))
before_size, before_size_bytes = Compress.calc_size(self.remote)
print('\nSize (Before compression): ', before_size, '\n\n')
if kwargs.get('count', 1):
self.count = self.count_files(self.remote,
hidden=kwargs.get('hidden'))
print('Total files : ', self.count, '\n')
print('| Ext | Count |')
print('|_______|_______|')
for ext, item_no in self.files_by_ext.items():
print('| {:<5} | {:>5} |'.format(ext , item_no))
print('|_______|_______|', '\n\n')
self.compress_st_time = timestamp()
self.main(kwargs) # start compressing
print('\nTime', self._time_taken(self.compress_st_time, timestamp()), end='')
after_size, after_size_bytes = Compress.calc_size(self.local)
reduction = ((before_size_bytes - after_size_bytes)/before_size_bytes)*100
print(before_size_bytes, after_size_bytes, reduction)
print('\nSize - Before {} / After {} ({:}%): '.format(before_size, after_size, int(reduction)), '\n\n')
print('\n{:-^100}\n'.format(os.path.basename(self.remote).upper()))
if kwargs.get('delete_dup'):
remove(self.local)
if kwargs.get('encrypt_'):
encrypt(self.local)
def add_not_down(self, xt):
'''Files whose extension is xt will be ignored (not copied or
compressed)'''
self.not_down.append(xt)
def _top_dir(self, folder):
return folder.split('/')[-1]
def valid_unix_name(self, name):
return '"'+name+'"'
def to_local(self, content):
return content.replace(self.remote, self.local)
def count_files(self, folder, **kwargs):
total=0
if os.path.isfile(folder):
return 1
# folder
for file in os.listdir(folder):
if os.path.isfile(folder + '/' + file):
ext = file.split('.')[-1].lower()
if self.files_by_ext.get(ext):
self.files_by_ext[ext] += 1
else:
self.files_by_ext[ext] = 1
total += 1
elif kwargs.get('hidden'):
total += self.count_files(folder+'/'+file)
elif not file.startswith('.'):
total += self.count_files(folder+'/'+file)
return total
def counter(self):
out = 'T' + str(self.count) + '-C' + str(self.value.value) + '-S' \
+ str(self.skip) + '-R' + str(self.count-self.value.value-self.skip)
print('{:<21} || '.format(out), end='')
def make_dirs(self, folder):
# Making Directory Copy
os.chdir(folder)
for dirs in os.listdir(folder):
if not dirs.split('/')[-1].startswith('.'):
new_content = folder + '/' + dirs
if os.path.isdir(new_content):
self.make_dirs(new_content)
if not os.path.exists(self.to_local(folder)):
os.system('mkdir -p ' +
self.valid_unix_name(self.to_local(folder)))
def should(self, file_name):
s = os.path.exists(file_name)
return not s
def shorten_name(self, name):
short = lambda x : x[:3]
shorten = '/'
for i in name.split('/')[:-1]:
if i:
if len(i)>10:
for word in i.split()[:3]:
shorten += short(word)+ '.. '
shorten += '/'
else:
shorten += short(i) + '/'
shorten += name.split('/')[-1]
return shorten
def _time_taken(self, start_time:float, end_time:float):
'''Takes start time(timestamp) and end time(timestamp) and return time
related info'''
dt1 = datetime.utcfromtimestamp(start_time) # datetime, no timezone
dt1 = dt1.replace(tzinfo=timezone('UTC')) # datetime object with utc timezone
dt1 = dt1.astimezone(timezone('Asia/Kolkata')) # Datetime in IST
dt1 = dt1.strftime('%H:%M') # format time
dt2 = datetime.utcfromtimestamp(end_time) # datetime, no timezone
dt2 = dt2.replace(tzinfo=timezone('UTC')) # datetime object with utc timezone
dt2 = dt2.astimezone(timezone('Asia/Kolkata')) # Datetime in IST
dt2 = dt2.strftime('%H:%M') # format time
# ST - start time, ET- end time, TT = time taken
elap_time = 'ST-{}|ET-{}|TT-{}'.format(
dt1, dt2, utils.read_seconds(end_time - start_time))
return elap_time
def compress(self, file, *args):
local_file = self.to_local(file)
saveas = self.valid_unix_name(local_file)
file_name = file.replace(self.remote, '') # includes subdirs/file
start_time = timestamp()
should_compress = True
ffmpeg_cmd = ''
def abs_size(size, full_unit=False):
'''will change 111.3MB to 111M'''
size = utils.readable_size(size).split(' ')
if full_unit:
return str(int(float(size[0]))) + size[1]
return str(int(float(size[0]))) + size[1][0]
# if exists check for size
if os.path.exists(local_file):
# checking here if incomplete recompress
orig_size, comp_size, should_compress = is_incomplete(local_file, file)
if comp_size > orig_size:
# check here if res in wrong
self.res = '360'
should_compress = True
elif not should_compress: # not incomplete
pass
else:
print('AC/CS {:>6}/{:<7} ||{:>19} {}'.format(abs_size(orig_size, 1).strip(),
abs_size(comp_size, 1).strip(), '||', file_name))
os.unlink(local_file)
ffmpeg_cmd = "ffmpeg -i " + self.valid_unix_name(file) + "\
-b:a 64k -ac 1 -vf scale=\"'w=-2:h=trunc(min(ih," + str(self.res) + ")/2)*2'\" \
-crf 32 -profile:v baseline -level 3.0 -preset slow -v error -strict -2 -stats \
-y -r 20 " + saveas
if self.count:
self.counter()
# increase compresed files value
with self.value.get_lock():
self.value.value += 1
if should_compress:
# incomplete or not exists
print('{:<15} ||'.format('COMPRESSING'), self.shorten_name(file_name))
os.system(ffmpeg_cmd)
orig_size, comp_size, _ = is_incomplete(local_file, file)
end_time = timestamp()
print('{:<26} | {:<5}/{:<4} {} {}'.format(self._time_taken(start_time, end_time),
abs_size(orig_size), abs_size(comp_size),
'||', self.shorten_name(file_name)))
else:
# exists and not incomplete
print('{:<6}{:>4}/{:<4} ||'.format('SKIP', abs_size(orig_size), abs_size(comp_size)
), self.shorten_name(self.shorten_name(file_name)))
def convert(self, file, ext="mp3"):
file_ext = file.split('.')[-1]
saveas = (file.replace(file_ext, ext)).replace(self.remote, self.local)
local_name = self.to_local(file)
file_name = file.replace(self.remote, '')
status = False
start_time = timestamp()
# if exists check for size
if os.path.exists(file_name):
orig_size, comp_size, status = is_incomplete(file_name, file, 8)
if not status: # not incomplete
pass
else:
print(f'AC/CS {orig_size//1024**2}MB/{comp_size//1024**2}MB -> ',
self.local)
os.unlink(local_file)
if self.count:
self.counter()
# print('converting', self.shorten_name(file))
convert(file_name=file, saveas=saveas, to=ext)
end_time = timestamp()
# increase compresed files value
with self.value.get_lock():
self.value.value += 1
print(self._time_taken(start_time, end_time), 'Converted\t', file_name.replace(
self.remote, ''))
def get_file(self, folder):
os.chdir(folder)
for file in os.listdir(folder):
if not file.startswith('.'):
new_file = folder + '/' + file
if os.path.isfile(new_file):
self.local_file = self.to_local(new_file)
file_ext = new_file.split('.')[-1].lower()
if file_ext in self.video:
self.files.append(new_file)
elif file_ext in self.not_down:
self.skip += 1
elif self.should(self.local_file): # check if file exists or not
try:
if self.count:
self.counter()
print('{:<15} ||'.format('Copy'), self.shorten_name(self.local_file.replace(self.local, '')))
copy(new_file, self.local_file)
except Exception as e:
print(e)
else:
# check if file is complete (zip, rar)
self.skip += 1
orig_size, comp_size, status = is_incomplete(self.local_file, new_file)
if orig_size != comp_size: # incomplete
try:
os.unlink(self.local_file)
if self.count:
self.counter()
print('{:<15} ||'.format('Replace'), self.shorten_name(self.local_file.replace(self.local, '')))
copy(new_file, self.local_file)
except Exception as e:
print(e)
else: # file is already copied and is complete
if self.count:
self.counter()
print('{:<15} ||'.format('Copied'), self.shorten_name(self.local_file.replace(self.local, '')))
else: # new content in folder
self.get_file(new_file)
def main(self, kwargs):
## Get all files recursively
self.make_dirs(self.remote) # make copies
self.get_file(self.remote) # list all files
if kwargs.get('shuffle'):
shuffle(self.files)
else:
try:
self.files = sorted(self.files, key=sort_func)
except Exception as e:
print('Can\'t sort with custom function\n', str(e))
self.files = sorted(self.files)
len_ = len(self.files)
target = eval(f"self.{kwargs.get('cmd', 'compress')}")
for i in range(0, len_-1, 2):
p1 = Process(target=target, args=(self.files[i], kwargs.get('ext', 'mp3')))
p2 = Process(target=target, args=(self.files[i+1], kwargs.get('ext', 'mp3')))
p1.start()
p2.start()
p1.join()
p2.join()
if len_%2:
p3 = Process(target=target, args=(self.files[-1], "mp3"))
p3.start()
p3.join()
@staticmethod
def calc_size(folder):
folder_size = utils.size(folder)
size = str(utils.readable_size(folder_size))
return (size, folder_size)
def __str__(self):
return '''
Parms :-
remote :- From Where | Gdrive url
local :- To Where | gdrive url
'''
def __repr__(self):
stngs = f'''
Remote = {self.remote}
Local = {self.local}'''
for key, val in self.kwargs.items():
stngs += f'\n\t{key} = {val}'
return stngs
def sort_func( name):
import re
regex = re.compile(r'\d+$')
string = name.split('/')[-1]
try:
num = int(regex.findall(string)[0])
except:
num = ''
return num
if __name__ == '__main__':
import utils
from utils import encrypt, is_incomplete, convert, get_size
from removeDups import remove
else:
from . import utils
from .utils import encrypt, is_incomplete, convert
from .removeDups import remove