-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimporter.py
155 lines (124 loc) · 4.78 KB
/
importer.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import xmltodict
from datetime import datetime
from collections import OrderedDict
USERS = {}
CATEGORIES = {}
POSTS = {}
def main():
with open('backup.xml', 'r') as f:
content = f.read()
xml = xmltodict.parse(content)
for item in xml['rss']['channel']['wp:author']:
USERS[item['wp:author_id']] = {
'display_name': item['wp:author_display_name'],
'nickname': item['wp:author_login'],
'email': item['wp:author_email']
}
print 'Found %s users!' % len(USERS.keys())
for item in xml['rss']['channel']['wp:category']:
CATEGORIES[item['wp:term_id']] = {
'category_nicename': item['wp:category_nicename'],
'category_name': item['wp:cat_name'],
'parent': item['wp:category_parent']
}
print 'Found %s categories!' % len(CATEGORIES.keys())
for item in xml['rss']['channel']['item']:
try:
if item['wp:post_type'] == 'post':
POSTS[item['wp:post_id']] = {
'title': item['title'],
'post_name': item['wp:post_name'],
'link': item['link'],
'creator': find_user_by_name(item['dc:creator']),
'status': item['wp:status'],
'content': item['content:encoded'],
'categories': find_cat_by_nicename(item['category']),
'date': datetime.strptime(
item['wp:post_date'], '%Y-%m-%d %H:%M:%S'),
'comments': None
}
try:
POSTS[item['wp:post_id']]['comments'] = parse_comments(item['wp:comment'])
except KeyError:
POSTS[item['wp:post_id']]['comments'] = []
except Exception as e:
# TODO investigate why!
print 'Could not read one of the posts: %s' % e
print 'Found %s posts to write' % len(POSTS.keys())
for post in POSTS:
write_post_file(POSTS[post])
def find_user_by_name(creator_name):
for author in USERS:
if USERS[author]['nickname'] == creator_name:
return USERS[author]
return None
def find_cat_by_nicename(categories):
category_dicts = []
for category in [categories] if isinstance(categories, OrderedDict) else categories:
for term_id in CATEGORIES:
if CATEGORIES[term_id]['category_nicename'] == category['@nicename']:
category_dicts.append(CATEGORIES[term_id])
return category_dicts
def parse_comments(comments):
all_comments = []
for comment in [comments] if isinstance(comments, OrderedDict) else comments:
c = {
'id': comment['wp:comment_id'],
'author': comment['wp:comment_author'],
'date': comment['wp:comment_date'],
'comment': comment['wp:comment_content']
}
try:
c['author_extra'] = USERS[comment['wp:comment_user_id']]
except KeyError:
c['author_extra'] = None
all_comments.append(c)
return all_comments
def write_post_file(post):
post_name = post['post_name'].replace('%c2%b7','')
print 'Writing post [%s]' % post_name
post_content = u"""
{{% extends 'post_template.html' %}}
{{% block title %}}
{0}
{{% endblock %}}
{{% block author %}}
{1}
{{% endblock %}}
{{% block post_date %}}
{2}
{{% endblock %}}
{{% block categories %}}
{3}
{{% endblock %}}
{{% block post %}}
{4}
{{% endblock %}}
{{% block comments %}}
{5}
{{% endblock %}}
"""
with open('app/src/templates/posts/' + post_name + '.html','wb') as f:
comment_template = u'<li class="list-group-item comment">{0}<p class="comment-author">Escrit al {2} per {1}</p></li>'
comments = []
for comment in post['comments']:
d = comment['date']
c = comment['comment']
if comment['author_extra']:
a = comment['author_extra']['display_name']
else:
a = comment.get('author', 'Anónim')
comments.append(comment_template.format(c, a, d))
content = post_content.format(
post['title'],
post['creator']['display_name'],
post['date'],
', '.join(category['category_name'] for category in post['categories']),
post['content'],
''.join(comments) if comments else '<li class="list-group-item">Sense comentaris</li>'
)
f.write(content.encode('utf-8'))
if __name__ == '__main__':
main()