Last active
March 27, 2022 00:43
-
-
Save ibaiGorordo/69988fbf83a045e6c58701aa03eb423f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tempfile import mkstemp | |
from shutil import move, copymode | |
from os import fdopen, remove | |
import os | |
import re | |
def has_old_style(file_path): | |
pattern = "CODE=" | |
with open(file_path, encoding="utf8") as f: | |
# Read the old script | |
script = f.read() | |
return pattern in script | |
# https://stackoverflow.com/a/3368991/13706271 | |
def find_between_r( s, first, last ): | |
try: | |
start = s.rindex( first ) + len( first ) | |
end = s.rindex( last, start ) | |
return s[start:end] | |
except ValueError: | |
return "" | |
# Ref: https://stackoverflow.com/a/39110/13706271 | |
def replace_download_pattern(file_path): | |
#Create temp file | |
fh, abs_path = mkstemp() | |
with fdopen(fh,'w',encoding="utf8") as new_file: | |
with open(file_path,encoding="utf8") as old_file: | |
# Read the old script | |
old_lines = old_file.readlines() | |
new_lines = old_lines.copy() | |
curl_lines = [i for i,line in enumerate(old_lines) if "curl -sc" in line] | |
for curl_line in curl_lines: | |
# Extract file id | |
file_id = find_between_r(old_lines[curl_line], "id=", "\" ") | |
new_lines[curl_line] = f"fileid=\"{file_id}\"\n" | |
# Read file name: | |
file_name = find_between_r(old_lines[curl_line+2], "-o ", "\n") | |
new_lines[curl_line+1] = "html=`curl -c ./cookie -s -L \"https://drive.google.com/uc?export=download&id=${fileid}\"`\n" | |
new_lines[curl_line+2] = f"curl -Lb ./cookie \"https://drive.google.com/uc?export=download&`echo ${{html}}|grep -Po '(confirm=[a-zA-Z0-9\\-_]+)'`&id=${{fileid}}\" -o {file_name}\n" | |
[new_file.write(new_line) for new_line in new_lines] | |
#Copy the file permissions from the old file to the new file | |
copymode(file_path, abs_path) | |
#Remove original file | |
remove(file_path) | |
#Move new file | |
move(abs_path, file_path) | |
if __name__ == '__main__': | |
rootdir = 'PINTO_model_zoo' | |
for subdir, dirs, files in os.walk(rootdir): | |
for file in files: | |
if "download" in file and file.endswith(".sh"): | |
file_path = os.path.join(subdir, file) | |
if has_old_style(file_path): | |
replace_download_pattern(file_path) | |
else: | |
continue | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment