-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGoogleGroupToDiscuzSql.py
293 lines (229 loc) · 12.2 KB
/
GoogleGroupToDiscuzSql.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
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
291
292
293
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
This script extracts entries from a google group to a SQL file
that can be used by a DiscuzX 1.5 forum
Require 'BeautifulSoup' module
Released under the GPLv3. Report bugs to [email protected]
Hosted on https://github.com/tangramor/extract-google-group
(c) Wang Jun Hua, homepage: http://blog.bbsers.org/tattoo
General Public License: http://www.gnu.org/licenses/gpl-3.0.html
"""
__VERSION__="0.1"
import string
import sys
import os
import logging
from ExtractGoogleGroup import Extract
from string import Template
from optparse import OptionParser
#Set the end of the SQL file to ;
def CorrectSqlEnd(fileName):
f = open(fileName, 'r+')
f.seek(-2, 2)
f.write(';')
f.close()
#Transform the Google Group data to SQL
def Transform(startpage, endpage, threadId, postId):
forumId = 36 #the forum id to import these data into
author = 'GoogleGroup' #the user that used to import data
authorId = 51 #the user id of the user
j = threadId #j is counter for threads, should be set to the latest thread ID number + 1
k = postId #k is counter for posts, should be set to the latest post ID number + 1
global extract
counterCache = None
counterFileName = "_threadAndPostID.cache"
if os.path.exists(counterFileName):
logging.info('Found ID cache file')
counterCache = open(counterFileName, 'r')
x = counterCache.readlines()
counterCache.close()
if len(x) == 2:
j = int(x[0])
k = int(x[1])
logging.info("Current thread ID: %s", str(j))
logging.info("Current post ID: %s", str(k))
else:
counterCache = open(counterFileName, 'w')
counterCache.write("1\n1")
counterCache.close()
else:
counterCache = open(counterFileName, 'w')
counterCache.write("1\n1")
counterCache.close()
filesurfix = '_' + str(startpage) + '_' + str(endpage)
threadsFileName = 'discuzx_threads' + filesurfix + '.sql'
postsFileName = 'discuzx_posts' + filesurfix + '.sql'
postTableidFileName = 'discuzx_post_tableid' + filesurfix + '.sql'
f_threads = open(threadsFileName, 'w')
f_posts = open(postsFileName, 'w')
f_post_tableid = open(postTableidFileName, 'w')
threadHeader = u"/*!40101 SET NAMES utf8 */;\n\nINSERT INTO `bbsers_forum_thread` (`tid`, `fid`, `posttableid`, `typeid`, `sortid`, `readperm`, `price`, `author`, `authorid`, `subject`, `dateline`, `lastpost`, `lastposter`, `views`, `replies`, `displayorder`, `highlight`, `digest`, `rate`, `special`, `attachment`, `moderated`, `closed`, `stickreply`, `recommends`, `recommend_add`, `recommend_sub`, `heats`, `status`, `isgroup`, `favtimes`, `sharetimes`, `stamp`, `icon`, `pushedaid`) VALUES \n"
threadT = Template(u"""(${threadId}, ${forumId}, 0, 0, 0, 0, 0, '${author}', ${authorId}, '${subject}', ${dateline}, ${lastpost}, '${lastposter}', 0, ${replies}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 0),\n""")
postHeader = u"/*!40101 SET NAMES utf8 */;\n\nINSERT INTO `bbsers_forum_post` (`pid`, `fid`, `tid`, `first`, `author`, `authorid`, `subject`, `dateline`, `message`, `useip`, `invisible`, `anonymous`, `usesig`, `htmlon`, `bbcodeoff`, `smileyoff`, `parseurloff`, `attachment`, `rate`, `ratetimes`, `status`, `tags`, `comment`) VALUES \n"
postT = Template(u"""(${postId}, ${forumId}, ${threadId}, ${first}, '${author}', ${authorId}, '${subject}', ${dateline}, '${message}', '${useip}', 0, 0, 1, 1, -1, -1, 0, 0, 0, 0, 0, '', 0),\n""")
postTableidHeader = u"INSERT INTO `bbsers_forum_post_tableid` (`pid`) VALUES \n"
postTableidT = Template(u"""(${postId}),\n""")
f_threads.write(threadHeader)
f_posts.write(postHeader)
f_post_tableid.write(postTableidHeader)
f_threads.close()
f_posts.close()
f_post_tableid.close()
f_threads = open(threadsFileName, 'a')
f_posts = open(postsFileName, 'a')
f_post_tableid = open(postTableidFileName, 'a')
r_list = range(startpage, endpage + 1)
counterCache = open(counterFileName, 'w')
for i in r_list:
threads = u''
posts = u''
post_tableids = u''
list = extract.getTopicAndUrlInTopicListPage(i)
for topics in extract.getTopicContentInTopicListPage(list):
poster = u"原帖作者:<b>" + topics['from'] + u" <" + topics['email'] + u"></b><br>\n发表时间:<b>" + extract.chineseDate(topics['date']) + u"</b><br />\n原帖链接:<a href=\"" + extract.rootUrl + topics['individual_link'] + "\" target=_blank>" + extract.rootUrl + topics['individual_link'] + "</a><br /><br />\n"
posts += postT.substitute(postId = k,
forumId = forumId,
threadId = j,
first = 1,
author = author,
authorId = authorId,
subject = topics['subject'],
dateline = extract.dateToTimestamp(topics['date']),
message = poster + topics['content'].replace("'", "\\\'"),
useip = u'127.0.0.1'
)
post_tableids += postTableidT.substitute(postId = k)
k += 1
lastpost = extract.dateToTimestamp(topics['date'])
if topics['replies']:
for reply in topics['replies']:
lastpost = extract.dateToTimestamp(reply['date'])
poster = u"原帖作者:<b>" + reply['from'] + u" <" + reply['email'] + u"></b><br>\n发表时间:<b>" + extract.chineseDate(reply['date']) + u"</b><br />\n原帖链接:<a href=\"" + extract.rootUrl + reply['link'] + "\" target=_blank>" + extract.rootUrl + reply['link'] + "</a><br /><br />\n"
#print reply['content'].encode('utf8')
posts += postT.substitute(postId = k,
forumId = forumId,
threadId = j,
first = 0,
author = author,
authorId = authorId,
subject = reply['subject'],
dateline = lastpost,
message = poster + reply['content'].replace("'", "\\\'"),
useip = u'127.0.0.1'
)
post_tableids += postTableidT.substitute(postId = k)
k += 1
threads += threadT.substitute(threadId = j,
forumId = forumId,
author = author,
authorId = authorId,
subject = topics['subject'],
dateline = extract.dateToTimestamp(topics['date']),
lastpost = lastpost,
lastposter = author,
replies = len(topics['replies'])
)
j += 1
f_threads.write(threads.encode('utf8'))
f_posts.write(posts.encode('utf8'))
f_post_tableid.write(post_tableids)
counterCache.write(str(j) + "\n" + str(k))
counterCache.close()
f_threads.close()
f_posts.close()
f_post_tableid.close()
CorrectSqlEnd(threadsFileName)
CorrectSqlEnd(postsFileName)
CorrectSqlEnd(postTableidFileName)
def main():
parser = OptionParser()
usage = "Usage: GoogleGroupToDiscuzSql groupname -t threadId -p postId [-s startpage -e endpage]"
parser.add_option("-s","--start", action="store", type="string", dest="startpage", help="the topic page number to start")
parser.add_option("-e","--end", action="store", type="string", dest="endpage", help="the topic page number to end")
parser.add_option("-t","--threadid", action="store", type="string", dest="threadId", help="the thread ID to start count")
parser.add_option("-p","--postid", action="store", type="string", dest="postId", help="the post ID to start count")
(options, args) = parser.parse_args()
global extract
if (len(sys.argv) == 1) or sys.argv[1] == '--help' or sys.argv[1] == '-h':
print usage
#extract all topics in a batch
elif (options.threadId != None) & (options.postId != None) & (len(sys.argv) == 6):
# call extract-google-group
extract = Extract(sys.argv[1])
threadId = int(options.threadId)
postId = int(options.postId)
stepSize = 2 # 2 pages as a batch should be a reasonable size
startpage = 0
endpage = startpage + stepSize
totalpage = extract.getTotalTopicListPageNumber(extract.getTotalTopicNumber())
if totalpage > stepSize:
startmsg = "Start to extract posts from google group.\nThe threadId to start is " + options.threadId + ", and the postId to start is " + options.postId
print startmsg
steps = totalpage / stepSize
for i in range(steps):
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
filename='extract-google-group_' + str(startpage) + '_' + str(endpage) + '.log',
filemode='w'
)
logging.info(startmsg)
ongoingmsg = "Now we are extracting page " + str(startpage) + " to " + str(endpage)
logging.info(ongoingmsg)
print ongoingmsg
Transform(startpage, endpage, threadId, postId)
success = "¼ -> ½ -> ¾ -> 1!!!!!! You have successfully extracted you google group " + sys.argv[1] + " from topic list page " + str(startpage) + " to " + str(endpage)
logging.info(success)
print success
endpage += stepSize
startpage += stepSize
else:
endpage = totalpage
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
filename='extract-google-group_' + str(startpage) + '_' + str(endpage) + '.log',
filemode='w'
)
startmsg = "Start to extract posts from google group.\nThe threadId to start is " + options.threadId + ", and the postId to start is " + options.postId
logging.info(startmsg)
print startmsg
ongoingmsg = "Now we are extracting page " + str(startpage) + " to " + str(endpage)
logging.info(ongoingmsg)
print ongoingmsg
Transform(startpage, endpage, threadId, postId)
success = "¼ -> ½ -> ¾ -> 1!!!!!! You have successfully extracted you google group " + sys.argv[1] + " from topic list page " + str(startpage) + " to " + str(endpage)
logging.info(success)
print success
#extract prefered topic pages with specified start and end page numbers
elif (options.threadId != None) & (options.postId != None) & (len(sys.argv) == 10):
startmsg = "Start to extract posts from google group.\nThe threadId to start is " + options.threadId + ", and the postId to start is " + options.postId
print startmsg
extract = Extract(sys.argv[1])
threadId = int(options.threadId)
postId = int(options.postId)
startpage = int(options.startpage)
endpage = int(options.endpage)
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
filename='extract-google-group_' + str(startpage) + '_' + str(endpage) + '.log',
filemode='w'
)
logging.info(startmsg)
ongoingmsg = "Now we are extracting page " + str(startpage) + " to " + str(endpage)
logging.info(ongoingmsg)
print ongoingmsg
Transform(startpage, endpage, threadId, postId)
success = "¼ -> ½ -> ¾ -> 1!!!!!! You have successfully extracted you google group " + sys.argv[1] + " from topic list page " + str(startpage) + " to " + str(endpage)
logging.info(success)
print success
else:
print usage
if __name__=="__main__":
try:
main()
except:
logging.exception("Unexpected error")
raise