This repository has been archived by the owner on Aug 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange_attachment_posts.js
57 lines (50 loc) · 1.94 KB
/
change_attachment_posts.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
54
55
56
57
const MongoClient = require('mongodb').MongoClient;
const fs = require("fs");
// Connection URL
const url = 'mongodb://admin:[email protected]:27017';
const { sanitazeMessage , sanitazeAttachment } = require('./sanitaze')
// Database Name
const dbName = 'nodebb';
fs.readdir('F:\\scraper\\attachments\\', function(err, filenames) {
if (err) {
onError(err);
return;
}
let q=0;
MongoClient.connect(url, async (err, client) => {
if(err == null) console.log(err)
console.log("Connected successfully to server");
const db = client.db(dbName);
await asyncForEach(filenames, async filename => {
let attachments = require('F:\\scraper\\attachments\\'+filename);
if(attachments.length != 1 || !attachments[0].match("/posts/")) {
return;
}
console.log(filename)
let forum_file = require('F:\\scraper\\forum_posts\\'+filename);
let temp = require('F:\\scraper\\scrapped_v2\\'+filename);
//let b = await db.collection('objects').findOne({_key:`post:${forum_file.data.topicData.mainPid}`})
//console.log(b.content)
let { messageHTML , imgs1 } = sanitazeMessage(temp.userContentHTML);
let a = db.collection('objects').updateOne(
{_key: 'post:'+forum_file.data.topicData.mainPid},
{
$set: { 'content': messageHTML
+ "\r\n **Adjuntos:** \r\n"+ attachments[0] + "\r\n"
+ ("\r\n *fb_postid:* \r\n"+ temp.id) }
}
);
q++;
if(q>1000) {
await new Promise(r => setTimeout(r, 2000));
q=0;
}
});
client.close();
});
});
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}