Changes between Version 72 and Version 73 of HashSourceCodes
- Timestamp:
- Feb 4, 2024, 5:39:26 PM (10 months ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
HashSourceCodes
v72 v73 379 379 == Python == 380 380 381 https://gist.github.com/henryjfry/ 82c490ca8415a13fd6d388230835e448381 https://gist.github.com/henryjfry/f857ceae9cb1e5625f9c485e65da1d4d 382 382 * supports local and remote files 383 383 … … 385 385 #!python 386 386 import struct, os 387 import urllib388 387 __64k = 65536 389 388 __longlong_format_char = 'q' … … 404 403 return False 405 404 406 407 def hashFile_url(filepath): 405 def hash_size_File_url(filepath): 408 406 #https://trac.opensubtitles.org/projects/opensubtitles/wiki/HashSourceCodes 409 407 #filehash = filesize + 64bit sum of the first and last 64k of the file … … 415 413 416 414 if local_file == False: 417 from urllib import request418 415 f = None 419 opener = None420 416 url = name 421 request.urlcleanup()422 423 f = request.urlopen(url)424 425 filesize = int(f.headers['Content-Length']) 417 import requests 418 419 response = requests.head(url)#, verify=False) 420 filesize = int(response.headers['content-length']) 421 426 422 if filesize < __64k * 2: 427 try: filesize = int(str( f.headers['Content-Range']).split('/')[1])423 try: filesize = int(str(response.headers['content-range']).split('/')[1]) 428 424 except: pass 429 425 430 opener = request.build_opener()431 bytes_range = ('bytes=0-%s') % (str(__64k))432 opener.addheaders = [('Range',bytes_range)]433 426 434 427 first_64kb = temp_file() 435 428 last_64kb = temp_file() 436 429 437 request.install_opener(opener) 438 request.urlretrieve(url, first_64kb) 439 opener = request.build_opener() 440 430 headers = {"Range": 'bytes=0-%s' % (str(__64k -1 ))} 431 r = requests.get(url, headers=headers)#, verify=False) 432 with open(first_64kb, 'wb') as f: 433 for chunk in r.iter_content(chunk_size=1024): 434 if chunk: # filter out keep-alive new chunks 435 f.write(chunk) 441 436 442 437 if filesize > 0: 443 opener.addheaders = [('Range', 'bytes=%s-%s' % (filesize - __64k, filesize))]438 headers = {"Range": 'bytes=%s-%s' % (filesize - __64k, filesize-1)} 444 439 else: 445 440 f.close() 446 441 os.remove(first_64kb) 447 return "SizeError" 442 return "SizeError", 0 448 443 449 444 try: 450 request.install_opener(opener) 451 request.urlretrieve(url, last_64kb) 445 r = requests.get(url, headers=headers)#, verify=False) 446 with open(last_64kb, 'wb') as f: 447 for chunk in r.iter_content(chunk_size=1024): 448 if chunk: # filter out keep-alive new chunks 449 f.write(chunk) 452 450 except: 453 451 f.close() 454 452 if os.path.exists(last_64kb): 455 453 os.remove(last_64kb) 456 os.remove(first_64kb) 457 return 'IOError' 454 if os.path.exists(first_64kb): 455 os.remove(first_64kb) 456 return 'IOError', 0 458 457 f = open(first_64kb, 'rb') 459 458 460 459 try: 461 462 460 longlongformat = '<q' # little-endian long long 463 461 bytesize = struct.calcsize(longlongformat) 464 462 465 463 if local_file: 466 464 f = open(name, "rb") 467 465 filesize = os.path.getsize(name) 468 466 hash = filesize 469 467 470 468 if filesize < __64k * 2: 471 469 f.close() … … 473 471 os.remove(last_64kb) 474 472 os.remove(first_64kb) 475 return "SizeError" 473 return "SizeError", 0 476 474 477 475 range_value = __64k / __byte_size … … 500 498 os.remove(first_64kb) 501 499 returnedhash = "%016x" % hash 502 return returnedhash 500 return returnedhash, filesize 503 501 504 502 except(IOError): … … 506 504 os.remove(last_64kb) 507 505 os.remove(first_64kb) 508 return 'IOError' 506 return 'IOError', 0 509 507 510 508 '''