import os
from urllib.parse import unquote

image_path = r'E:\yuxin\ship-wiki\images'

url_file = open(r"E:\yuxin\ship-wiki\images_url.txt", 'r', encoding='utf-8')
urls = url_file.read().split('\n')
url_file.close()
target_files = os.listdir(image_path)
for url in urls:
    url = unquote(url)
    correct_file_name = url.split('/')[-1]
    second_file_name = url.split('/')[-2]
    second_alter_file_name = second_file_name + '.png'
    if correct_file_name in target_files:
        print(f"[PASS] {correct_file_name}")
    elif second_file_name in target_files:
        os.rename(image_path + '/' + second_file_name, image_path + '/' + correct_file_name)
        print(f"[RENAME] {second_file_name}")
    elif second_alter_file_name in target_files:
        os.rename(image_path + '/' + second_alter_file_name, image_path + '/' + correct_file_name)
        print(f"[RENAME] {second_alter_file_name}")
    else:
        print(f"[NOT EXIST] {url}")
