-
Notifications
You must be signed in to change notification settings - Fork 0
/
disqus2duoshuo.js
53 lines (48 loc) · 2.06 KB
/
disqus2duoshuo.js
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
var fs = require('fs');
var filePath = process.argv[2];
fs.readFile(filePath, function (err, buffer) {
if (err)
throw err;
var data = buffer.toString('utf-8');
var outputAll = new Object();
// threads
var inputThread = data.match(/\<thread[\s\S]*?\<\/thread\>/g);
var outputThread = new Array();
for (var i in inputThread) {
var thread = new Object();
thread.author_key = inputThread[i].match(/name\>(.*)\</)[1];
thread.thread_key = inputThread[i].match(/thread dsq:id=\"(.*)\"/)[1];
thread.title = inputThread[i].match(/title\>(.*)\</)[1];
thread.url = inputThread[i].match(/link\>(.*)\</)[1];
outputThread[i] = thread;
}
outputAll.threads = outputThread;
// posts
var inputPost = data.match(/\<post[\s\S]*?\<\/post\>/g);
var outputPost = new Array();
for (var j in inputPost) {
var post = new Object();
post.author_key = inputPost[j].match(/username\>(.*)\</)[1];
post.post_key = inputPost[j].match(/post dsq:id=\"(.*)\"/)[1];
post.thread_key = inputPost[j].match(/thread dsq:id=\"(.*)\"/)[1];
post.author_name = inputPost[j].match(/name\>(.*)\</)[1];
post.author_email = inputPost[j].match(/email\>(.*)\</)[1];
post.ip = inputPost[j].match(/ipAddress\>(.*)\</)[1];
post.message = inputPost[j].match(/p\>(.*)\<\/p/)[1].replace('<br>', '\n');
post.created_at = inputPost[j].match(/createdAt\>(.*)\</)[1].split('T')[0] + ' '
+ inputPost[j].match(/createdAt\>(.*)\</)[1].split('T')[1].split('Z')[0];
if (inputPost[j].match(/parent dsq:id=\"(.*)\"/))
post.parent_key = inputPost[j].match(/parent dsq:id=\"(.*)\"/)[1];
else
post.parent_key = '';
outputPost[j] = post;
}
outputAll.posts = outputPost;
// to JSON
var ouputJSON = JSON.stringify(outputAll);
fs.writeFile('import.json', ouputJSON, function (err) {
if (err)
throw err;
console.log('It\'s saved! Please import this file to Duoshuo!!!');
});
});