-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
103 lines (96 loc) · 4.13 KB
/
worker.js
File metadata and controls
103 lines (96 loc) · 4.13 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
const Feed = require('@models/feed');
const User = require('@models/user');
const Article = require('@models/article');
const parseRss = require('@/services/parseRss');
const emailSends = require('@services/email');
const { parser: parseHtml } = require('html-metadata-parser');
const { exposeArticle } = require('@utils/exposes');
const ms = require('ms');
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function main () {
const feeds = await Feed.find({ });
if (!feeds.length) return sleep(ms('1m'));
for (const feed of feeds) {
const usersForEmail = await User.find({
_id: { $in: feed.subscribers },
enableEmailNotifications: true
});
let feedContent;
try {
feedContent = await parseRss(feed.url);
} catch (error) {
console.log(`Error accessing to feed page (${decodeURI(feed.url)}):\n${error}`);
continue;
}
const { items } = feedContent;
const articles = items.reverse().filter((article) => {
const published = new Date(article.published);
const now = new Date();
const diff = now - published;
if (diff > ms('30 days')) {
return false;
}
if (/\[מקודם\]/gm.test(article.description) ||
article.category.includes('דביק - פנים האתר') ||
article.category.includes('דביק - עמוד הבית') ||
(/^https:\/\/www\.jdn\.co\.il/.test(feed.url) && />><\/strong><\/a><\/p>/m.test(article.content))
) return false;
return true;
});
if (!articles.length) continue;
for (const article of articles) {
const articleExists = await Article.findOne({ url: article.link });
const articleRelatedToFeed = articleExists ? articleExists.feeds.includes(feed._id) : false;
if (articleExists && articleRelatedToFeed) continue;
if (!articleRelatedToFeed) {
if (articleExists) {
await Article.updateOne({ _id: articleExists._id }, { $push: { feeds: feed._id } });
} else {
if (!article.thumbnail) {
try {
const { og } = await parseHtml(article.link);
article.thumbnail = og.image;
} catch (error) {
delete article.thumbnail;
}
}
try {
await Article.create({
title: article.title,
url: article.link,
feeds: feed._id,
published: article.published,
author: article.author,
description: article.description,
content: article.content,
tags: article.category,
image: article.thumbnail
});
} catch (error) {
console.log(`Error in creating article: ${error}`);
continue;
}
}
if (usersForEmail.length) {
try {
await emailSends.sendArticle({
article: exposeArticle(article),
feedTitle: feed.title,
feedUrl: feed.url,
toAddresses: usersForEmail.map(user => {
return {
address: user.emailFront,
allowAttachmentsInEmail: user.allowAttachmentsInEmail
};
})
});
} catch (error) {
console.log(`Error in sending email: ${error}`);
}
}
}
}
}
return sleep(ms('1m'));
}
module.exports = main;