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 pathinsert_votes_topics.js
52 lines (47 loc) · 1.63 KB
/
insert_votes_topics.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
const MongoClient = require('mongodb').MongoClient;
const fs = require("fs");
// Connection URL
const url = 'mongodb://admin:[email protected]:27017';
// Database Name
const dbName = 'nodebb';
fs.readdir('F:\\scraper\\forum_posts\\', 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 => {
console.log(filename)
let forum_file = require('F:\\scraper\\forum_posts\\'+filename);
let facebook_file = require('F:\\scraper\\scrapped_v2\\'+filename);
if(!facebook_file.upvotes) return;
let a = db.collection('objects').updateOne(
{_key: 'topic:'+forum_file.data.topicData.mainPost.tid},
{
$set: { 'downvotes': 0 }
}
);
let b = db.collection('objects').updateOne(
{_key: 'topic:'+forum_file.data.topicData.mainPost.tid},
{
$set: { 'upvotes': parseInt( facebook_file.upvotes ) }
}
);
q++;
if(q>500) {
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);
}
}