Skip to content

Instantly share code, notes, and snippets.

@Hareric
Created May 23, 2021 08:43
Show Gist options
  • Save Hareric/e4b457f471e15349d92be0cd7cde6c74 to your computer and use it in GitHub Desktop.
Save Hareric/e4b457f471e15349d92be0cd7cde6c74 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright © 2021, All rights reserved.
# Author: Eric <wendachen@flickering.ai>
"""
替换github图床资源为镜像资源
"""
from pathlib import Path
import json
path = '/Users/har/Documents/Github/hexo-build'
origin_base_path = 'https://raw.githubusercontent.com/'
new_base_path = 'https://raw.fastgit.org/'
for file in Path(path).rglob("*"):
if file.is_dir():
continue
is_in_hide_folder = False
for p in file.parents:
if p.name.startswith('.'):
is_in_hide_folder = True
break
if is_in_hide_folder:
continue
try:
with open(file, "r+", encoding='utf-8') as f:
origin_data = f.read()
if origin_base_path in origin_data:
# print(file)
new_data = origin_data.replace(origin_base_path, new_base_path)
# 读写偏移位置移到最开始处
f.seek(0, 0)
f.write(new_data)
# 设置文件结尾 EOF
f.truncate()
except UnicodeDecodeError:
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment