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
|
#!/usr/bin/env python
import os
import pycurl
import string
import sys
import tempfile
import time
import urllib
import urlparse
import xmlrpclib
from HTMLParser import HTMLParser
MAX_POST_NO = 10000
class WordPress:
def __init__(self, url, username, password):
self.__username = username
self.__password = password
self.__proxy = xmlrpclib.ServerProxy(url)
def all_posts(self):
return self.__proxy.metaWeblog.getRecentPosts("",
self.__username,
self.__password,
MAX_POST_NO)
def post_comments(self, post):
return self.__proxy.wp.getComments("",
self.__username,
self.__password,
{"post_id" : post["postid"],
"number": MAX_POST_NO})
class MyHTMLParser(HTMLParser):
def __init__(self, url, imgdir):
HTMLParser.__init__(self)
self.url = url
self.imgdir = imgdir
def handle_starttag(self, tag, attrs):
if tag == "img":
for attr in attrs:
if attr[0] == "src":
self.download_image(attr[1])
def download_image(self, url):
parsed = urlparse.urlparse(url)
if parsed.netloc == self.url:
print " downloading %s" % url
filename = os.path.join(self.imgdir, parsed.path[1:])
try:
os.makedirs(os.path.dirname(filename))
except os.error:
pass
fp = open(filename, "wb")
curl = pycurl.Curl()
curl.setopt(pycurl.URL, str(url))
curl.setopt(pycurl.FOLLOWLOCATION, 1)
curl.setopt(pycurl.MAXREDIRS, 5)
curl.setopt(pycurl.CONNECTTIMEOUT, 30)
curl.setopt(pycurl.TIMEOUT, 300)
curl.setopt(pycurl.NOSIGNAL, 1)
curl.setopt(pycurl.WRITEDATA, fp)
curl.perform()
curl.close()
fp.close()
def make_dir(path):
os.mkdir(path)
return path + "/"
def write_file(path, content):
f = open(path, "w")
f.write(content.encode("utf-8"))
f.close()
def write_comment(comment, dir):
def make_metadata():
out = ""
keys = {"comment_id" : "id",
"author" : "author",
"author_email" : "author_email",
"author_url" : "author_url",
"author_ip" : "author_ip",
"status" : "approved"}
for k in keys:
if (k != "status") or ((k == "status") and (comment[k] != "spam")):
out += "%s: %s\n" % (k, comment[k])
date = comment["date_created_gmt"]
out += "timestamp: %s\n" % int(time.mktime(date.timetuple()))
return out
write_file(dir + str(comment["comment_id"]),
make_metadata() + "\n" + comment["content"])
def unescape(s):
s = s.replace("<", "<")
s = s.replace(">", ">")
s = s.replace("&", "&")
return s
def make_post_key(post):
d = post["dateCreated"].timetuple()
pre = "%d/%02d/%02d/%s" % (d.tm_year, d.tm_mon, d.tm_mday, post["wp_slug"])
return urllib.quote(pre, "").lower()
def get_post_images(post, url, imgdir):
parser = MyHTMLParser(url, imgdir)
parser.feed(post["description"])
def write_post(post, categories, comments, images_url, new_images_url):
print "writing post %s" % unescape(post["title"])
def make_metadata():
out = ""
keys = {"postid" : "id",
"wp_author_display_name" : "author",
"title" : "title",
"post_status" : "status",
"mt_allow_comments" : "comment_status"}
for k in keys:
value = post[k]
if k == "mt_allow_comments":
if value == 1:
value = "open"
else:
value = "closed"
out += "%s: %s\n" % (keys[k], value)
out += "tags: %s\n" % ", ".join(categories)
out += "timestamp: %s\n" % int(time.mktime(post["dateCreated"].timetuple()))
return unicode(out)
key = make_post_key(post)
d = make_dir(key)
content = string.replace(post["description"], images_url, new_images_url)
write_file(d + "content", content)
write_file(d + "metadata", make_metadata())
if comments:
c = make_dir(d + "comments")
for comment in comments:
write_comment(comment, c)
def main(args):
d_posts = tempfile.mkdtemp(prefix="wp2dir")
d_images = tempfile.mkdtemp(prefix="wp2img")
print "creating directories %s and %s" % (d_posts, d_images)
_, url, images_url, new_images_url, user, passwd = args
wp = WordPress(url, user, passwd)
posts = wp.all_posts()
for post in posts:
comments = wp.post_comments(post)
os.chdir(d_posts)
write_post(post, post["categories"], comments, images_url, new_images_url)
os.chdir(d_images)
get_post_images(post, images_url, d_images)
if __name__ == "__main__":
main(sys.argv)
|