-
Notifications
You must be signed in to change notification settings - Fork 37
/
check-opml.ts
75 lines (66 loc) · 1.76 KB
/
check-opml.ts
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
import { createTextResponse } from '@slowreader/core'
import {
completeTasks,
createCLI,
enableTestClient,
error,
fetchAndParsePosts,
findRSSfromHome,
finish,
initializeProgressBar,
isString,
type LoaderTestFeed as OpmlFeed,
readText
} from './utils.ts'
async function parseFeedsFromFile(path: string): Promise<OpmlFeed[]> {
if (!path.endsWith('.opml') && !path.endsWith('.xml')) {
error(`Unsupported file extension found on ${path}`)
process.exit(1)
}
let text = createTextResponse(await readText(path))
return [...text.parseXml()!.querySelectorAll('[type="rss"]')]
.filter(feed => isString(feed.getAttribute('xmlUrl')))
.map(
f =>
({
homeUrl: f.getAttribute('htmlUrl')!,
title: f.getAttribute('title') || '',
url: f.getAttribute('xmlUrl')!
}) as OpmlFeed
)
}
let cli = createCLI(
'Test all feeds from user OPML',
'$ pnpm check-opml PATH_TO_YOUR_FILE.opml\n' +
'$ pnpm check-opml PATH_TO_YOUR_FILE.opml --home'
)
cli.run(async args => {
enableTestClient()
let opmlFile: string | undefined
let home = false
for (let arg of args) {
if (arg === '--home') {
home = true
} else if (!opmlFile) {
opmlFile = arg
} else {
cli.wrongArg('Unknown argument: ' + arg)
return
}
}
if (!opmlFile) {
cli.wrongArg('Please provide a path to the OPML file')
return
}
let feeds = await parseFeedsFromFile(opmlFile)
initializeProgressBar(home ? feeds.length * 2 : feeds.length)
await completeTasks(
feeds.map(feed => () => fetchAndParsePosts(feed.url, true))
)
if (home) {
for (let feed of feeds) {
await findRSSfromHome(feed)
}
}
finish(`${feeds.length} ${feeds.length === 1 ? 'feed' : 'feeds'} checked`)
})