|
|
@@ -0,0 +1,103 @@
|
|
|
+import hashlib
|
|
|
+import re
|
|
|
+import os
|
|
|
+import sys
|
|
|
+import xml.etree.ElementTree
|
|
|
+
|
|
|
+def mkdir(dest_path):
|
|
|
+ if os.path.exists(dest_path):
|
|
|
+ print('Error: output destination already exists.')
|
|
|
+ return
|
|
|
+ else:
|
|
|
+ os.makedirs(dest_path)
|
|
|
+ os.makedirs(os.path.join(dest_path, 'posts'))
|
|
|
+ os.makedirs(os.path.join(dest_path, 'posts', 'files'))
|
|
|
+
|
|
|
+def _comment(node):
|
|
|
+ approved = node.find(r'{http://wordpress.org/export/1.2/}comment_approved').text
|
|
|
+ if approved != '1':
|
|
|
+ return
|
|
|
+
|
|
|
+ return {
|
|
|
+ 'author': '%s (%s)' % (
|
|
|
+ node.find(r'{http://wordpress.org/export/1.2/}comment_author').text,
|
|
|
+ node.find(r'{http://wordpress.org/export/1.2/}comment_author_email').text
|
|
|
+ ),
|
|
|
+ 'date': node.find(r'{http://wordpress.org/export/1.2/}comment_date').text,
|
|
|
+ # 'content': node.find(r'{http://wordpress.org/export/1.2/}comment_content').text
|
|
|
+ }
|
|
|
+
|
|
|
+def _item(node):
|
|
|
+ data = {
|
|
|
+ 'type': node.find(r'{http://wordpress.org/export/1.2/}post_type').text,
|
|
|
+ 'date': node.find(r'{http://wordpress.org/export/1.2/}post_date').text,
|
|
|
+ 'status': node.find(r'{http://wordpress.org/export/1.2/}status').text,
|
|
|
+ 'link': node.find(r'link').text,
|
|
|
+ 'title': node.find(r'title').text,
|
|
|
+ 'content_html': node.find(r'{http://purl.org/rss/1.0/modules/content/}encoded').text,
|
|
|
+ }
|
|
|
+
|
|
|
+ data['id'] = hashlib.md5(data['link'].encode('utf-8')).hexdigest()
|
|
|
+
|
|
|
+ # if data['type'] == 'post':
|
|
|
+ # for child in node:
|
|
|
+ # print(child.tag)
|
|
|
+
|
|
|
+ if data['type'] == 'post':
|
|
|
+ data['comments'] = [_comment(node) for node in node.findall(r'{http://wordpress.org/export/1.2/}comment')]
|
|
|
+
|
|
|
+ return data
|
|
|
+
|
|
|
+def _write_files(dest_path, posts):
|
|
|
+ with open(os.path.join(dest_path, 'index.html'), 'w') as f:
|
|
|
+ f.write('<table>')
|
|
|
+ for post in posts:
|
|
|
+ f.write('<tr>')
|
|
|
+ f.write(' <td>%s</td>\n' % post['date'].split(' ')[0])
|
|
|
+ f.write(' <td><a href="posts/%s.html">%s</a></td>\n' % (post['id'], post['title']))
|
|
|
+ f.write('</tr>\n')
|
|
|
+ f.write('</table>')
|
|
|
+
|
|
|
+ for post in posts:
|
|
|
+ with open(os.path.join(dest_path, 'posts', '%s.html' % post['id']), 'w') as f:
|
|
|
+ f.write('<h1>%s: %s</h1>\n' % (post['date'], post['title']))
|
|
|
+ f.write(post['content_html'] or '')
|
|
|
+
|
|
|
+
|
|
|
+def main(dest_path, xml_files):
|
|
|
+ posts = []
|
|
|
+
|
|
|
+ for xml_file in xml_files:
|
|
|
+ # print('parsing %s...' % xml_file)
|
|
|
+ # https://docs.python.org/3/library/xml.etree.elementtree.html#module-xml.etree.ElementTree
|
|
|
+ tree = xml.etree.ElementTree.parse(xml_file).getroot()
|
|
|
+ for channel in tree:
|
|
|
+ for item in channel:
|
|
|
+ if item.tag != 'item':
|
|
|
+ continue
|
|
|
+
|
|
|
+ data = _item(item)
|
|
|
+
|
|
|
+ if data['status'] != 'publish' or data['type'] != 'post':
|
|
|
+ continue
|
|
|
+
|
|
|
+ posts.append(data)
|
|
|
+
|
|
|
+ posts.sort(key=lambda x: x['date'])
|
|
|
+
|
|
|
+ for post in posts:
|
|
|
+ if re.match(r'^20\d\d - ', str(post['title'])):
|
|
|
+ post['date'] = post['title'][:4]
|
|
|
+ post['title'] = post['title'][7:]
|
|
|
+
|
|
|
+ print(post)
|
|
|
+
|
|
|
+ _write_files(dest_path, posts)
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ if len(sys.argv) > 2:
|
|
|
+ # mkdir(sys.argv[1])
|
|
|
+ main(sys.argv[1], sys.argv[2:])
|
|
|
+ else:
|
|
|
+ print('Usage: python3 main.py <output destination> <xml files...>')
|
|
|
+
|