警告
本文最后更新于 2019-05-09,文中内容可能已过时。
新浪图床挂了之后我的博客图片挂了一大堆,今天写了个脚本来解决下。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
| import requests
import re
import os
from datetime import datetime
COUNT = 0
def getimg(post, rule):
with open(post, 'r', encoding='utf-8') as f:
content = f.read()
imgs = re.findall(rule, content)
for markdown, img in imgs:
r = requests.get(img).content
filename = 'img/uploads/' + now() + '.jpg'
with open(filename, 'wb+')as file:
file.write(r)
global COUNT
COUNT += 1
print(filename, img)
markdown_str = markdown
markdown = markdown.replace(img, 'https://y4er.com/' + filename)
content = content.replace(markdown_str, markdown)
with open(post, 'w', encoding='utf-8')as mark:
mark.write(content)
print(post + " over! replace "+str(COUNT)+"张!")
return 0
def now():
return str(datetime.now().strftime("%Y%m%d%H") + str(datetime.now().microsecond)[-4:])
if __name__ == '__main__':
weibo = r'(!.*(https://.*sinaimg.*.jpg).*\))'
smms = r'(!.*(https://.*loli.net.*.png).*\))'
for post in os.listdir():
if post[-2:] == 'md':
with open(post, 'r', encoding='utf-8')as f:
text = f.read()
if 'sinaimg' in text:
getimg(post, weibo)
elif 'loli.net' in text:
getimg(post, smms)
else:
print(post+"没有图片")
|