Repository navigation
Expand file tree
/
Copy pathserver.mjs
More file actions
133 lines (125 loc) · 4.69 KB
/
Copy pathserver.mjs
File metadata and controls
133 lines (125 loc) · 4.69 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
import dotenv from 'dotenv';
import amqp from 'amqplib';
import ArticleBuilder from '@frontender-magazine/builder';
dotenv.config();
const { RABIITMQ_HOST, PORT = 3000 } = process.env;
let connection;
let channel;
const queue = 'bus';
let retries = 3;
const interval = 10000;
const buildRabbitMQConnector = () => new Promise((resolve, reject) => connectToRabbitMQ(resolve, reject));
const connectToRabbitMQ = async (resolve, reject) => {
try {
connection = await amqp.connect(`amqp://${RABIITMQ_HOST}`);
channel = await connection.createChannel();
await channel.assertQueue(queue, {
durable: false
});
console.log('RabbitMQ is connected.')
resolve();
} catch (error) {
console.log(error);
if (retries > 0) {
console.log(`Reconnect in ${interval/1000} seconds. ${retries} attempts left.`)
setTimeout(connectToRabbitMQ.bind(this, resolve, reject), interval);
} else {
reject();
}
retries--;
}
};
(async () => {
try {
// connecting to the rebbitmq bus
console.log('trying to connect');
await buildRabbitMQConnector();
console.log("[*] Waiting for messages in %s. To exit press CTRL+C", queue);
channel.consume(queue, async (msg) => {
console.log('msg: ');
console.log(JSON.parse(msg.content.toString()));
const event = JSON.parse(msg.content.toString());
if (event.name === "ARTICLE_LINKS") {
let index = event.payload.length;
while(index--) {
const link = event.payload[index];
const builder = new ArticleBuilder();
builder.skip.stages = [
'github:before',
'github',
'github:after',
];
builder.skip.plugins = [
{ name: 'codepenTransform' },
{ name: 'codepenTransformIFrame' },
{ name: 'createREADME' },
{ name: 'downloadImages' },
{ name: 'writeMarkdown' },
{ name: 'TMPDir' },
{ name: 'initGithub' },
{ name: 'uploadToRepo' },
{ name: 'createRepo' },
{ name: 'createREADME' },
{ name: 'createCard' },
];
try {
const result = await builder.create(link);
const description = (
(result.schema && result.schema.description) ||
(result.openGraph && result.openGraph.ogDescription) ||
null
);
const author = (
result.mercury &&
result.mercury.length > 0 &&
result.mercury[0].author &&
result.mercury[0].author.replace(/[\r\n]+/gm, '').trim()
) ||
(result.schema && result.schema.author);
const article = {
url: link,
domain: result.domain,
author: author && [author],
tags: result.tags,
title:
(result.schema && result.schema.title) ||
(result.mercury && result.mercury.length > 0 && result.mercury[0].title) ||
(result.openGraph && result.openGraph.ogTitle),
description: description && description.replace(/[\r\n]+/gm, '').trim(),
lang:
(result.openGraph && result.openGraph.ogLocale) ||
result.language && result.language.length>0 && result.language[0].isReliable && result.language[0].language ||
'en',
published:
(result.openGraph && result.openGraph.article && result.openGraph.article.published_time) ||
(result.mercury && result.mercury.length > 0 && result.mercury[0].date_published) ||
(result.openGraph && result.openGraph.article && result.openGraph.article.modified_time) ||
(result.schema && result.schema.modified),
updated:
(result.openGraph && result.openGraph.article && result.openGraph.article.modified_time) ||
(result.mercury && result.mercury.length > 0 && result.mercury[0].date_published) ||
(result.schema && result.schema.modified),
};
console.log(article);
const msg = JSON.stringify({
name: "CREATE_ARTICLE",
payload: article,
});
await channel.sendToQueue(queue, Buffer.from(msg));
console.log(" [x] Sent %s", msg);
} catch (error) {
console.log(error);
}
}
}
}, {
noAck: true
});
} catch (error) {
console.log(error);
// shutting down rabbitmq connection
if (channel) await channel.close();
if (connection) await connection.close();
process.exit(1);
}
})();