-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_wordpress.py
More file actions
290 lines (236 loc) · 8.38 KB
/
extract_wordpress.py
File metadata and controls
290 lines (236 loc) · 8.38 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python
# coding: utf-8
import os
import os.path
import shutil
import re
import pathlib
from collections import namedtuple
from urllib.parse import urlparse
from datetime import datetime
import requests
from bs4 import BeautifulSoup, Comment
from slugify import slugify
from pytz import timezone
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from PIL import Image
import markdownify as md
import frontmatter
import toml
from photo_match import ImageInfos, ImageMatcher, load_bucket
def make_tables(wp_id, engine):
Base = declarative_base()
class WpPost(Base):
__table__ = sqlalchemy.Table(
'wp_{}_posts'.format(wp_id),
Base.metadata,
autoload=True,
autoload_with=engine
)
class WpOption(Base):
__table__ = sqlalchemy.Table(
'wp_{}_options'.format(wp_id),
Base.metadata,
autoload=True,
autoload_with=engine
)
return (WpPost, WpOption)
Post = namedtuple(
'Post',
[
'post_name',
'post_title',
'post_date',
'post_content',
'post_date_from_images'
]
)
class ImageProcessor:
def __init__(self, path):
self.path = path
self.session = requests.Session()
self.imgs = []
def add_urls(self, urls):
dates = []
for url, relpath in urls:
filepath = os.path.abspath(os.path.join(self.path, relpath[1:]))
self.imgs.append((url, filepath))
if os.path.exists(filepath):
image = Image.open(filepath)
else:
image = None
date = ImageInfos.guess_date(pathlib.Path(filepath), image)
dates.append(date)
return dates
def save_file(self, url, filepath):
if os.path.exists(filepath):
return
res = self.session.get(url, stream=True)
if res.status_code == 200:
res.raw.decode_content = True
with open(filepath, "wb") as f:
shutil.copyfileobj(res.raw, f)
else:
print("Could not access", url)
def download(self):
for url, filepath in self.imgs:
os.makedirs(os.path.dirname(filepath), exist_ok=True)
self.save_file(url, filepath)
class PostProcessor:
def __init__(self, path, photodir):
self.path = path
self.photodir = photodir
self.img_processor = ImageProcessor(path)
self.timezone = "UTC"
def set_timezone(self, timezone_):
self.timezone = timezone_
def process_posts(self, posts):
for post in posts:
ppost = self.process_post(post)
if ppost is not None:
yield ppost
def process_post(self, post):
if self.filter_post(post):
return None
name = self.process_name(post)
str_date = self.process_date(post)
relpath = os.path.join(self.photodir, name)
html_abspath = os.path.join("/", relpath)
content, img_urls = self.process_content(
post.post_content,
html_abspath
)
dates = self.img_processor.add_urls(img_urls)
date_from_images = self.process_date_from_images(dates)
return Post(name, post.post_title, str_date, content, date_from_images)
def process_date_from_images(self, dates):
nonone = [date for date in dates if date is not None]
if len(nonone) == 0: return None
best_date = max(nonone)
if best_date is None: return None
best_date = best_date.strftime("%Y-%m-%d")
return best_date
def filter_post(self, post):
if post.post_status == "auto-draft":
return True
if post.post_status == "trash":
if post.post_modified_gmt < datetime(2020, 7, 29, 8, 16, 26):
return True
return False
def process_name(self, post):
name = post.post_name
if post.post_status == "trash":
name = slugify(post.post_title)
return name
def process_date(self, post):
utc_date = post.post_modified_gmt.replace(tzinfo=timezone('UTC'))
wp_date = utc_date.astimezone(timezone(self.timezone))
return wp_date.isoformat()
def process_content(self, content, img_dir_path):
soup = BeautifulSoup(content, 'html.parser')
img_urls = []
for img_tag in soup.find_all('img'):
src = img_tag.get("src")
if src:
filename = urlparse(src).path.split("/")[-1]
new_src = os.path.join(img_dir_path, filename)
img_urls.append((src, new_src))
img_tag["src"] = new_src
if img_tag.parent.name == "a":
img_tag.parent.unwrap()
for fig_tag in soup.find_all("figure"):
if not isinstance(fig_tag.parent, BeautifulSoup):
fig_tag.parent.unwrap()
for cap_tag in fig_tag.find_all("figcaption"):
cap_tag.name = "p"
fig_tag.name = "p"
is_comment = lambda tag:isinstance(tag, Comment)
for comment_tag in soup.find_all(string=is_comment):
comment_tag.extract()
soup.smooth()
converter = md.MarkdownConverter()
content = converter.process_tag(soup)
return content, img_urls
class WordpressExtractor:
CONFIG_FILE = "config_site.toml"
OUTPUT_FOLDER = "static"
CONTENT_FOLDER = "content"
POSTS_FOLDER = "posts"
def __init__(self, path):
self.path = path
self._load_config(path)
self.proc = PostProcessor(
os.path.join(self.path, self.OUTPUT_FOLDER),
self.photodir
)
def _load_config(self, path):
configpath = os.path.join(path, self.CONFIG_FILE)
with open(configpath, 'r', encoding="utf-8") as fin:
self.config = toml.load(fin)
self.sqlurl = self.config.get("extract", {}).get("database", "")
self.wp_id = self.config.get("extract", {}).get("site_id")
self.download = self.config.get("photos", {}).get("download", True)
self.photodir = self.config.get("photos", {}).get("path", "photos")
def write_post(self, post):
metadata = {
'title': post.post_title,
'date': post.post_date,
'date_event': post.post_date_from_images,
'description': "",
'categories': [],
'toc': False,
'dropCap': True,
'displayInMenu': False,
'displayInList': True,
'draft': False,
'resources': [
{
'name': 'featuredImage',
'src': '',
'params': {'description': 'description'}
}
]
}
post_md = frontmatter.Post(
post.post_content,
frontmatter.default_handlers.YAMLHandler(),
**metadata
)
filepath = os.path.join(
self.path,
self.CONTENT_FOLDER,
self.POSTS_FOLDER,
post.post_name,
"index.md"
)
os.makedirs(os.path.dirname(filepath), exist_ok=True)
with open(filepath, "wb") as fout:
frontmatter.dump(post_md, fout, encoding='utf-8')
def process_all(self):
engine = sqlalchemy.create_engine(self.sqlurl, echo=False)
WpPost, WpOption = make_tables(self.wp_id, engine)
Session = sessionmaker(bind=engine)
session = Session()
wordpress_timezone = (
session
.query(WpOption.option_value)
.filter(WpOption.option_name == "timezone_string")
.scalar()
)
self.proc.set_timezone(wordpress_timezone)
query = session.query(WpPost).filter(WpPost.post_type == "post")
for post in self.proc.process_posts(query):
self.write_post(post)
if self.download:
self.proc.img_processor.download()
def main():
import sys
if len(sys.argv) != 2:
print("Usage: {} path/to/site".format(sys.argv[0]))
sys.exit(-1)
wpe = WordpressExtractor(os.path.abspath(sys.argv[1]))
wpe.process_all()
if __name__ == "__main__":
main()