-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgenerateTopics.js
More file actions
141 lines (127 loc) · 3.92 KB
/
generateTopics.js
File metadata and controls
141 lines (127 loc) · 3.92 KB
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
require('dotenv').config()
const db = require('monk')(process.env.MONGO_DB)
const posts = db.get('posts')
const tags = db.get('tags')
const topics = db.get('topics')
const Bluebird = require('bluebird');
const _ = require('lodash');
let newTopics = []
let promises = []
let i = 0
let i2 = 0
function createTopics() {
console.log("1. Get topics from posts' filterTags fields")
posts.find({ filterTags: { $exists: true }}).each(async post => {
try {
i++
if (post.filterTags.length != 0) {
let k = 0;
for (k = 0; k<post.filterTags.length; k++) {
const tag = post.filterTags[k]
try {
const tagName = tag.name;
let promise = topics.findOne({ $or: [{ name: tagName }, { slug: tag.slug }] })
.then((existingTopic) => {
if (!existingTopic) {
// create topic if is not contained in existingTopic:
// const slug = generateSlug(tagName);
const t =_.find(newTopics, x => x.name === tagName)
if (!t) {
newTopics.push({
name: tagName,
slug: tag.slug,
postCount: 1,
maintainers: [],
status: 'active'
})
}
}
});
promises.push(promise)
} catch(e) {
console.log('catch', e)
}
}
}
} catch(e) {
console.log('ERROR', e)
}
}).then(() => {
try {
return Bluebird.all(promises);
} catch(e) { console.log(e)}
}).then(async () => {
console.log(" Number of processed posts: ", i)
console.log("\n2. Add topics to the Topics table")
await addTopicsToDB()
})
.then(async () => {
console.log("\n3. Add topics to the Posts and increase topics' counters")
await addTopicsToPosts()
console.log('\n\n======================================');
console.log('4. Summary')
console.log(`Number of processed posts: ${i2}`)
process.exit();
});
}
async function addTopicsToPosts() {
let promises2 = []
async function asyncTopicToPosts(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
const postsWithTags = await posts.find({ filterTags: { $exists: true }, topicsGenerated: { $exists: false }})
await asyncTopicToPosts(postsWithTags, async (post) => {
console.log('-----------------------------------')
i2 = i2 + 1;
if (post.filterTags.length != 0) {
for (k = 0; k<post.filterTags.length; k++) {
const tag = post.filterTags[k]
const tagName = tag.name;
let existingTopic = await topics.findOne({ $or: [{ name: tagName }, { slug: tag.slug }] })
console.log(` Adding topic to the post ${i2}/${i}: ${tagName}`)
await posts.update({ _id: post._id },
{
$push: {
topics: existingTopic._id.toString()
},
$set: { topicsGenerated: true }
}, (error) => {
if (error) return;
});
console.log(` Increasing topic counter for topic: ${tagName} (${i2}/${i})`)
await topics.update({ _id: existingTopic._id }, {
$inc: { postCount: 1 }
}, (err) => {
if (err) return;
});
}
}
})
.then(() => {
return Bluebird.all(promises2);
})
}
function generateSlug(string) {
return string
.toString()
.trim()
.toLowerCase()
.replace(/\s+/g, "-")
.replace(/[^\w\-]+/g, "")
.replace(/\-\-+/g, "-")
.replace(/^-+/, "")
.replace(/-+$/, "");
}
createTopics()
async function addTopicsToDB() {
for (let k = 0; k<newTopics.length; k++) {
try {
await topics.insert(newTopics[k])
} catch (e) {
console.error(e)
}
}
console.log(" Done!")
}