-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy path3-extractImgs.py
executable file
·80 lines (56 loc) · 2.37 KB
/
3-extractImgs.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
import urllib.request
from os import walk, path, mkdir
import re
import shutil
import time
import http
basepath = "./src"
files = next(walk(basepath))[2]
files = filter(lambda x: not x.startswith("."), files)
def getcontent(filepath):
fileurl = ''
content = ''
imgs = []
with open(filepath, "r", encoding="utf-8") as f:
for line in f.readlines():
content += line
if line.startswith("permalink"):
fileurl = line.split(":")[1].strip()
imgurls = re.findall(r'(!\[.*?\]\(http.*?\))', line)
if imgurls:
imgs.extend(imgurls)
if not fileurl:
raise ValueError("No permalink in %s! Please add permalink before run this script!" % (filepath, ))
return (filepath, fileurl, content, imgs)
def writeback(option):
(filepath, fileurl, content, imgs, downloadimgs) = option
with open(filepath, "w", encoding="utf-8") as f:
f.write(content)
def changeurls(option):
(filepath, fileurl, content, imgs, downloadimgs) = option
for i, img in enumerate(imgs):
content = content.replace(img, downloadimgs[i])
return (filepath, fileurl, content, imgs, downloadimgs)
def downloadimgs(option):
targetpath = "./themes/jacman/source/img/articles"
targeturl = "/img/articles"
(filepath, fileurl, content, imgs) = option
downloadimgs = []
for img in imgs:
(imgoriname, imgurl, imgname) = re.findall(r'!\[(.*)\]\((.*/(.*))\)', img)[0]
imgname = imgname.replace("?", "") + str(time.time())
if not path.isdir(path.join(targetpath, fileurl)):
mkdir(path.join(targetpath, fileurl))
if not path.exists(path.join(targetpath, fileurl, imgname)):
try:
if "appcoda" in imgurl:
raise "jump"
urllib.request.urlretrieve(imgurl, path.join(targetpath, fileurl, imgname))
except:
print("Can't download %s-->%s, please check the url!" % (fileurl, imgurl))
downloadimgs.append(u"![%s](%s)" % (imgoriname, imgurl))
continue
downloadimgs.append("![%s](%s)" % (imgoriname, targeturl + "/" + fileurl + "/" + imgname))
return (filepath, fileurl, content, imgs, downloadimgs)
list(map(lambda x: writeback(changeurls(downloadimgs(getcontent(path.join(basepath, x))))), files))