import os
import time
import codecs
import re
import requests
from datetime import datetime, timedelta, timezone
import pytz
def workfile(mypath):
for root, dirs, files in os.walk(mypath):
for name in files:
if name.endswith(".md"):
filename = os.path.join(root, name)
content = open(filename, 'r', encoding='UTF-8')
str = content.read()
content.close()
matchObj = re.findall(r'(?:!\[(.*?\]\((.*?)\)))', str)
imgObj = re.findall('(?i:src)=\"([^\"]*)\"', str)
print(filename)
for img in matchObj:
imgObj.append(img[1])
# basic_url = 'https://pic.itstephen.cn/images/' + time.strftime("%Y/%m/%d", time.localtime()) + "/"
basic_url = 'https://pic.itstephen.cn/images/' + getServerTimestamp() + "/"
for pic in imgObj:
print(pic)
print(root)
print(filename)
if pic.find('https//') == -1 and pic.find('http//') == -1:
picPath = os.path.join(os.path.abspath(root), pic)
print(picPath)
try:
picupload(picPath)
(path, picname) = os.path.split(picPath)
print(picname)
str = str.replace(pic, basic_url + picname)
except:
pass
replacedFile = open(filename, 'w', encoding='UTF-8')
replacedFile.write(str)
replacedFile.close()
def getServerTimestamp():
td = timedelta(hours=-3)
tz = timezone(td)
dt = pytz.datetime.datetime.fromtimestamp(time.time(), tz)
return dt.strftime("%Y/%m/%d")
def picupload(pic):
url = 'https://pic.itstephen.cn/api/1/upload/?key=mykey'
action = 'upload'
form = {'source': open(pic, 'rb')}
r = requests.post(url, files=form)
print(r.content)
# time.sleep(5)
if __name__ == '__main__':
print('hello, world')
path = os.path.curdir
print(os.path.abspath("../"))
os.listdir
ss = 'ssasas'
print(ss.endswith('as'))
workfile(path)
time.sleep(5)
图片脚本上传最终版
未经允许不得转载:Stephen Young » 图片脚本上传最终版
相关推荐
-      阿里云ddns脚本全流程+python、pip环境安装
-      python脚本文件遍历,字符串查找,图片上传
-      数字一键激活Windows和office
-      Windows路由规则
-      Windows 文件管理器频繁卡死
-      解决v2rayn的UWP代理问题
-      密码保护:oneindex公共同步下载盘
-      Python版autoSummary脚本
评论前必须登录!
注册