项目背景: 项目资源服务器迁移, 之前 img url, 批量替换为 新服务器的 ulr 地址:
看了下 一共100处 修改涉及 19个文件;想想只能写个脚本 批量处理了
解决思路:
- 先找出源文件中的 url
- 下载源文件到本地(这一步骤其实不用下载到本地,直接缓存到内存也是可以的 都是 二进制数据)
- 上传文件到新的文件地址, 生成新的 url 地址
- 替换原来的url 为 新生成的url 并保存文件
- 生成 josn 日志,用于查看
实现代码如下: FindStr.py
import os
import re
import datetime
import requests
import json
from requests_toolbelt import MultipartEncoder
includes = ['*.vue', '*.nvue', '*.js']
root = os.walk(r'D:\Git\b2b-i18n-mobile\app\src')
#单个上传
uploadURL = 'https://xxxxxxxxx/file/upload/public/img'
#批量上传
uploadURLbatch = 'https://xxxxxxxxx/file/upload/public/img/batch'
pattern = re.compile(r'(https://img.vandream.com[^, "]*?\.(?:jpg|png))') #正则表达式找出匹配的字符串 以 xx开头, 并且以 jpg 或者 png 结尾
matchFilesList = []
os.makedirs('./image/', exist_ok=True)
def uploadFile(path):
loadfile = os.path.basename(path)
m = MultipartEncoder(
fields={
"file" : (loadfile, open("./image/{}".format(loadfile), "rb"), "image/jpeg"),
}
)
req = requests.post(uploadURL, data=m, headers={'Content-Type': m.content_type})
# print(req.text)
if req.status_code == 200 :
resoultJson = json.loads(req.text)
if( resoultJson['success'] == True) :
serverPath = resoultJson['data']['url']
# print('上传成功新的地址: {}'.format(serverPath))
return serverPath
else :
Exception('网络请求异常 1{}'.format(req.text))
else:
Exception('网络请求异常 2{}'.format(req.text))
def downLoadFile(url):
imageName = os.path.basename(url)
req = requests.get(url,stream=True)
from urllib.request import urlretrieve
resoult = urlretrieve(url, './image/{}'.format(imageName))
# print(resoult[0])
if len(resoult) == 2 :
return resoult[0]
else:
Exception('下载源文件出错 {}'.format(req.text))
def checkFile():
for path,dir_list,file_list in root:
for file_name in file_list:
if file_name.endswith('.vue') or file_name.endswith('.nvue') or file_name.endswith('.js') :
file_path = os.path.join(path, file_name)
with open(file_path, '+r',encoding='utf-8') as text:
# words = text.read().split()
words = text.read()
matchFiles = pattern.findall(words)
for item in matchFiles:
# print(item)
loadfile = downLoadFile(item)
serverFile = uploadFile(loadfile)
print('源文件:{0} 本地文件:{1}, 服务器路径{2}'.format(item,loadfile,serverFile))
words = words.replace(item, serverFile)
#读写偏移位置移到最开始处
text.seek(0, 0)
text.write(words)
dic = {}
dic['sourcefile'] = item
dic['loadfile'] = loadfile
dic['serverfile'] = serverFile
matchFilesList.append( dic )
print('開始:---------------> {}'.format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
checkFile()
print("操作完成 一共操作 {} 个文件对".format(len(matchFilesList)))
# print(matchFilesList)
json_str = json.dumps(matchFilesList)
with open("./replace.json","w") as f:
json.dump(json_str,f)
print("操作完成...")
print('結束:---------------> {}'.format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))