-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsci_rename.py
executable file
·439 lines (362 loc) · 17.1 KB
/
sci_rename.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
#!/usr/bin/env python3
import fitz
import os
import sys
import platform
import pkg_resources
import signal
import string
import hashlib
import shutil
import re
import logging
from src.helper import *
# Define logger / logger config
log_level = logging.INFO
logging.basicConfig(format='[%(asctime)s] - [%(levelname)s]- %(message)s' , level=log_level)
logger = logging.getLogger()
set_helper_logger(log_level)
def keyboardInterruptHandler(signal, frame):
logger.info('You pressed Ctrl+C! Leaving...'.format(signal))
sys.exit(0)
def validate_arguments(arguments):
if len(arguments) > 2:
logger.error('Wrong number of arguments!')
print_usage()
sys.exit()
if len(sys.argv) == 1:
logger.error("Missing arguments!")
print_usage()
sys.exit()
if len(arguments) == 2:
logger.debug(arguments[1])
target = os.path.abspath(arguments[1])
base_dir = ''
filename = ''
logger.debug('target : ' + target)
if os.path.exists(target):
if os.path.isdir(target):
base_dir = os.path.abspath(target)
else:
if target.endswith('.pdf'):
base_dir = os.path.dirname(target)
filename = os.path.basename(target)
else:
logger.error('Argument is not a pdf file')
sys.exit()
else:
logger.error("Directory or file ["+ arguments[1] + "] path does not exist!")
sys.exit()
return base_dir, filename
def hash_file(target_file):
blocksize = 65536
hasher = hashlib.new('sha256')
target_file = os.path.abspath(target_file)
if os.path.isfile(target_file):
with open(target_file, 'rb') as f:
while True:
data = f.read(blocksize)
if not data:
break
hasher.update(data)
return hasher.hexdigest()
else:
logger.error(target_file + ' is not a file')
sys.exit()
def parse_title(title, max_length=None):
'''
uses regex to parse the title/file-name candidate
'''
if max_length == None:
max_length = 125
if len(title) > max_length:
title = title[:max_length]
title = re.sub(r'[^a-zA-Z0-9]+', ' ' , title)
title = title.strip()
title=re.sub(r'\s', '_', title)
title = string.capwords(title) + '.pdf'
return title
def get_page_text(current_page):
# first find python version
#python_version = sys.version
# Get the Python version as a string
python_version = platform.python_version()
tested_versions = ["3.11.6", "3.7.17", "3.8.18"]
library_name = "PyMuPDF"
library_version = pkg_resources.get_distribution(library_name).version
if library_version == "1.18.14":
if python_version == "3.7.0":
logger.debug('Python version is: ' + python_version)
logger.debug('PyMuPDF version is: ' + library_version)
blocks = current_page.getText('dict')['blocks']
else:
logger.warn('Python version is: ' + python_version)
logger.warn('PyMuPDF version is: ' + library_version)
logger.error('Your Python version is not at the required level to work with PyMuPDF 1.18.14')
logger.error('Please ensure that both meet the specified version requirements for this script to function properly.')
sys.exit();
elif library_version == "1.22.5":
if python_version in tested_versions:
blocks = current_page.get_text('dict')['blocks']
logger.debug('Python version is: ' + python_version)
logger.debug('PyMuPDF version is: ' + library_version)
else:
logger.warn('Python version is: ' + python_version)
logger.warn('PyMuPDF version is: ' + library_version)
logger.error('Your Python version is not at the required level to work with PyMuPDF 1.22.5')
logger.error('Please ensure that both meet the specified version requirements for this script to function properly.')
sys.exit();
else:
logger.warn('Python version is: ' + python_version)
logger.warn('PyMuPDF version is: ' + library_version)
logger.error('Your Python version or PyMuPDF is not at the required level!')
logger.error('Please ensure that both meet the specified version requirements for this script to function properly.')
sys.exit()
return blocks
def scan_title(full_file_name, page_num=None):
'''
scans the pdf file looking for a title, either based on the pdf metadata or
trying to figure out what is the sentence that has the larger font-size
'''
logger.info('*******************************************************************************')
logger.info('Searching title for file : ' + full_file_name)
if page_num is None:
page_num = 0
doc = fitz.open(full_file_name)
meta_title = doc.metadata['title'].strip()
# Check document's metadata for a potential title
if len(meta_title) > 5:
logger.debug('Document metadata title: ' + meta_title)
meta_title = parse_title(meta_title)
# Read first page and look for the setence with the largest font
page = doc.load_page(page_num)
size_text_tup_list =[]
title=''
# get paget text
#blocks = page.get_text('dict')['blocks']
blocks=get_page_text(page)
for blk in blocks: # iterate through text blocks
if blk['type'] == 0: # only considers text blocks (type 0)
for line in blk['lines']: # iterate through text lines
if line['dir'] == (1.0, 0.0) and line['wmode'] == 0: # only considers horizontal lines:
# check 'dir' argument (writing direction) in the Line dictionary
# line['dir'] == (1.0, 0.0) write direction is horizontal left to right
for span in line['spans']: # iterate through spans
size_text_tup = (span['size'], span['text'], span['origin'])
size_text_tup_list.append(size_text_tup)
# sort the list of tuples ('font_size','text_line') according to the 'font size' of a text line
#TODO: improve this above. to many 'for' in 'for'
# sort output
sorted_size_text_list=sorted(size_text_tup_list, key=lambda text_size: text_size[0], reverse=True)
larger_font = 0
title_max_lines = 5
for item in sorted_size_text_list:
t_font_size = item[0]
t_text = item[1]
t_text_len = len(t_text)
#logger.debug('t_font_size: ' + str(t_font_size) + ' t_text: ' + t_text + ' t_text_len: ' + str(t_text_len))
# consider only span text bigger than 2 , in order to avoid posible single characters
# with font size bigger than the Title being gotten as if they were the title.
# ex: in some papers the first letter of the paragraph is bigger than anything else,
# only getting the bigger font without considering the size of the text must generate titles that
# are as big as a single character. such as a.pdf, or b.pdf, thus only consider span at lines with more than 2 chars
if t_text_len > 2:
if t_font_size > larger_font: # compare the size of each item with the size of the larger font size
larger_font = t_font_size # replace text if larger, append if it is the same
title = t_text.strip() + ' '
title_max_lines-=1
else:
if t_font_size == larger_font:
title = title + t_text.strip() + ' '
title_max_lines-=1
if title_max_lines < 1:
break
doc.close()
parsed_found_title = parse_title(title)
logger.debug('Parsed Found title: ' + parsed_found_title)
logger.debug('--------------------------------------------------------')
return(meta_title, parsed_found_title)
def do_rename(fullpath_current_filename, fullpath_new_filename):
'''
function do_rename: renames the file name given by the full path in
'current_filename' to 'new_filename'
receives: current_filename and 'new_filename
returns:
'''
logger.info('Renaming pdf file...')
logger.info('Current file name: ' + fullpath_current_filename)
logger.info('New file name: ' + fullpath_new_filename)
if fullpath_current_filename == fullpath_new_filename:
logger.warning('Current filename and found title are already the same. Skipping...')
return False
logger.info("Renaming file...")
try:
os.rename(fullpath_current_filename, fullpath_new_filename)
except:
logger.error("An exception occurred. File not renamed!")
return False
finally:
logger.debug("File renamed!")
return True
def confirm_to_continue():
logger.debug('Choose [c] to continue')
logger.debug('Choose [s] to skip file')
logger.debug('Choose [a] to abort')
valid_choices = ['c', 's', 'a']
choice = input('Choose [c] to continue, [s] to skip, or [a] to abort : \n')
while(choice not in valid_choices):
logger.warning('Answer not valid!')
choice = input('Choose [c] to continue or [a] to abort : \n')
if choice == 'c':
return True
if choice == 's':
return False
if choice == 'a':
logger.info("aborting...")
sys.exit()
def select_loop_type():
'''
Function to allow user to select the loop mode, in the case of renaming all files in a directory.
User will be asked to choose between 'loop though all files and rename' and 'files one by one'
'''
logger.info('Choose [1] : for renaming all pdf files in the directory')
logger.info('Choose [2] : for one-by-one pdf file confirmation')
valid_choices = ['1' , '2', 'q']
choice = input('Choose [1], [2] or q [quit] : \n')
while(choice not in valid_choices):
logger.warning('Answer not valid!')
choice = input('Choose [1], [2] or q [quit] : \n')
if choice == '1':
return '1'
if choice == '2':
return '2'
if choice == 'q':
logger.info("aborting...")
sys.exit()
def move_file(fullpath_src_file, destination_dir, dest_file):
if(not os.path.isdir(destination_dir)):
os.mkdir(os.path.join(destination_dir))
try:
#shutil.move(fullpath_src_file, destination_dir)
shutil.move(fullpath_src_file, os.path.join(destination_dir, dest_file))
except OSError as e:
logger.exception(e.strerror)
logger.warning('File ' + fullpath_src_file + ' was not moved to ' + destination_dir + '/auto_renamed_pdf' )
def search_candidate_title(src_dir, current_file):
'''
Generic function to search for one of the potential title candidates: based either on the metadata or on the font-size title
'''
counter = 0
# look for title in the pdf file, returns 2 potential candidates (meta data in pdf and bigger-font sentence)
meta_title, font_based_title = scan_title(src_dir + '/' + current_file)
logger.debug('get_title returned: [meta_title]: ' + meta_title + ' [found_title]: ' + font_based_title)
if len(font_based_title) > 0:
# if found_title > 0: then font-based title was found. use font_based_title.
return font_based_title
elif len(meta_title) > 0:
# if meta_title > 0: then metadata title was found. user chooses between meta_title and mined title
return meta_title
else:
logger.info('No potential Title was found for : ' + current_file)
return None
def rename_files_in_dir(base_dir):
'''
For each pdf file in base_dir,
searches for a potential title in the pdf document (metadata and font-size)
'''
renamed_counter = 0
total_counter = 0
# file_fingerprints -> list of files (based on their hashes) used to avoid duplicated files.
# when a duplicated file is found it adds a 'dup' prefix to the file name
file_fingerprints = []
full_path_base_dir = os.path.abspath(base_dir)
if os.path.isdir(full_path_base_dir):
list_of_files=[]
for file in os.listdir(full_path_base_dir):
if file.endswith('.pdf'):
logger.debug('adding file to the list: ' + file)
list_of_files.append(file)
if len(list_of_files) < 1:
logger.info('no pdf files found in the target directory')
return renamed_counter, total_counter
else:
logger.info(str(len(list_of_files)) + ' files found in the target directory!')
loop_type=select_loop_type()
for current_file in list_of_files:
total_counter+=1
# use the hash of the file to avoir renaming files already renamed.
# avoiding duplications.
fingerprint = hash_file(full_path_base_dir + '/' + current_file)
logger.info('*' * 80)
logger.info('[Current file name] : ' + current_file)
logger.debug('[Current file hash] : ' + fingerprint)
logger.debug('[loop_type] : ' + loop_type)
if loop_type == '2':
answer = confirm_to_continue()
if answer is False:
continue
if fingerprint not in file_fingerprints:
file_fingerprints.append(fingerprint)
found_title = search_candidate_title(full_path_base_dir, current_file)
if found_title is not None:
renamed = do_rename(full_path_base_dir + '/' + current_file, full_path_base_dir + '/' + found_title)
if renamed:
move_file(full_path_base_dir + '/' + found_title, full_path_base_dir + '/auto_renamed_pdf', found_title)
renamed_counter+=1
else:
logger.warning('Another file with the same content (hash) was found in the source directory!')
logger.info('Skipping file: ' + current_file + ' adding prefix `duplicated_`to it')
os.rename(full_path_base_dir + '/' + current_file, full_path_base_dir + '/duplicated_' + current_file )
else:
logger.error('Directory does not exist!' )
return renamed_counter, total_counter
def rename_target_file(src_dir, filename):
'''
For received a pdf file in the command line 'src_dir / filename',
scans the file for a potential title in the pdf document (metadata and font-size)
and call the method 'rename'
'''
fullpath_filename = src_dir + '/' + filename
logger.debug('Searching file: ' + fullpath_filename)
if os.path.isfile(fullpath_filename):
if os.path.abspath(fullpath_filename).endswith('.pdf'):
found_title = search_candidate_title(src_dir, filename)
if found_title is not None:
renamed = do_rename(fullpath_filename, src_dir + '/' + found_title)
if renamed:
move_file(src_dir + '/' + found_title, src_dir + '/auto_renamed_pdf', found_title)
return True
else:
logger.debug("File is not a pdf!")
return False
else:
logger.error("File does not exist!")
return False
def main():
print_header()
# validade arguments passed in the command line
base_dir, filename = validate_arguments(sys.argv)
# set handler to capture the 'control+C' interruption from keyboard
signal.signal(signal.SIGINT, keyboardInterruptHandler)
# target path is either a (full path) directory or (full path) file name
target_path = base_dir + '/' + filename
logger.info('[Target Path]: ' + target_path)
rename_counter = 0
if os.path.isdir(target_path):
# target is directory
logger.debug('[base_dir]: ' + base_dir)
rename_counter, total_counter = rename_files_in_dir(base_dir)
logger.info('*' * 80)
logger.info('Finished => Total files: ' + str(total_counter) + ' Renamed files: ' + str(rename_counter))
else:
# target is a file
logger.debug('[base_dir]: ' + base_dir)
logger.debug('[filename]: ' + filename)
renamed = rename_target_file(base_dir, filename)
logger.debug("Renamed? : " + str(renamed))
if renamed:
rename_counter+=1
logger.info('*' * 80)
logger.info('Finished => Renamed files : ' + str(rename_counter))
if __name__ == "__main__":
main()