This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
160 lines (132 loc) · 3.95 KB
/
index.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import fs from 'fs'
const args = process.argv.slice(2);
const blackList = JSON.parse(fs.readFileSync(args[0], { encoding: 'utf8' }))
const whiteList = JSON.parse(fs.readFileSync(args[1], { encoding: 'utf8' }))
const blackestList = JSON.parse(fs.readFileSync(args[2], { encoding: 'utf8' }))
const baseUrl = 'https://cloud.feedly.com'
const wordSeparatorRegex = /[\s"'‘’“”`\^\~_\.,;\:¡!¿?+\-\/*=\\\(\)\[\]{}|@#$%&<>]+/
const processedTitles = []
let continuation = null
let unreadEntries = []
while (true) {
const streamsParams = (new URLSearchParams({
streamId: process.env.COLLECTION,
count: 250,
ranked: 'oldest',
continuation: continuation,
unreadOnly: true
})).toString()
const response = await fetch(
`${baseUrl}/v3/streams/contents?${streamsParams}`,
{
method: 'GET',
headers: {
Authorization: `Bearer ${process.env.CLIENT_SECRET}`
}
}
)
if (response.status === 401) {
console.warn(`
------------------------------------------------------------------
| IMPORTANTE |
------------------------------------------------------------------
| Debe actualizarse el token de Feedly. Dirijirse a: |
| https://feedly.com/v3/auth/dev |
| |
| Una vez que obtenga el token reemplazelo en el archivo .env |
| o en la variable de entorno: CLIENT_SECRET |
------------------------------------------------------------------
`)
throw 'Invalid Token'
}
const responseData = JSON.parse(
normalizeString(
await response.text()
)
)
if (response.status !== 200) {
console.error('Status:', response.status)
console.error('Response:', responseData)
break;
}
unreadEntries = unreadEntries.concat(responseData.items)
if (typeof responseData.continuation === 'undefined') {
break
}
continuation = responseData.continuation
}
function normalizeString(string) {
return string.normalize('NFD').replace(/\p{Diacritic}/gu, '')
}
const entriesToMarkAsRead = unreadEntries.filter(filterEntry)
function filterEntry(entry) {
return (
filterDuplicate(entry) ||
filterTitle(entry) ||
filterContent(entry)
)
}
function filterDuplicate(entry) {
const isDuplicate = processedTitles.includes(entry.title)
processedTitles.push(entry.title)
return isDuplicate
}
function filterTitle(entry) {
const title = entry.title.toLowerCase().trim()
return (
hasMatchingTokens(title, blackestList) || // Title in blackestList
( // Title in blackList but not in whiteList
!hasMatchingTokens(title, whiteList) &&
hasMatchingTokens(title, blackList)
)
)
}
function filterContent(entry) {
const content = ((entry.content?.content || '') + ' ' + (entry.summary?.content || ''))
.trim()
.toLowerCase()
return (
content === '' || // No content
hasMatchingTokens(content, blackestList) || // Content in blackestList
( // Content in blackList but not in whiteList
!hasMatchingTokens(content, whiteList) &&
hasMatchingTokens(content, blackList)
)
)
}
function hasMatchingTokens(string, checkTokens) {
const tokens = string.split(wordSeparatorRegex)
for (const checkToken of checkTokens) {
if (wordSeparatorRegex.test(checkToken)) {
if (string.includes(checkToken)) {
return true;
}
} else {
if (tokens.includes(checkToken)) {
return true;
}
}
}
return false
}
await fetch(
`${baseUrl}/v3/markers`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.CLIENT_SECRET}`
},
body: JSON.stringify({
action: 'markAsRead',
type: 'entries',
entryIds: entriesToMarkAsRead.map(entry => entry.id)
})
}
)
console.log(
'Entries filtered:',
entriesToMarkAsRead.map(entry => `${entry.title} - ${entry.canonicalUrl} `)
)
console.log('----------------------------------------------------------------------------------')
console.log(`${entriesToMarkAsRead.length} entries marked as read`)