forked from hplush/slowreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss.ts
More file actions
102 lines (92 loc) · 2.9 KB
/
rss.ts
File metadata and controls
102 lines (92 loc) · 2.9 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
import { createDownloadTask, type TextResponse } from '../lib/download.ts'
import { type OriginPost, type PostMedia, stringifyMedia } from '../post.ts'
import { createPostsList } from '../posts-list.ts'
import { findMRSS } from './atom.ts'
import {
findAnchorHrefs,
findDocumentLinks,
findHeaderLinks,
findMediaInText,
isHTML,
type Loader,
toTime
} from './common.ts'
function parsePostSources(text: TextResponse): Element[] {
let document = text.parseXml()
if (!document) return []
return [...document.querySelectorAll('item')].filter(
item =>
item.querySelector('guid')?.textContent ??
item.querySelector('link')?.textContent
)
}
function parsePosts(text: TextResponse): OriginPost[] {
return parsePostSources(text).map(item => {
let description = item.querySelector('description')
let textMedia = findMediaInText(description?.textContent)
let postMedia: PostMedia[] = []
let enclosures = item.querySelectorAll('enclosure')
for (let enclosure of enclosures) {
let url = enclosure.getAttribute('url')
let type = enclosure.getAttribute('type')
if (url && type) {
postMedia.push({ type, url })
}
}
postMedia = postMedia.concat(findMRSS(item))
return {
full: description?.textContent ?? undefined,
media: stringifyMedia([...postMedia, ...textMedia]),
originId:
item.querySelector('guid')?.textContent ??
item.querySelector('link')!.textContent,
publishedAt: toTime(item.querySelector('pubDate')?.textContent),
title: item.querySelector('title')?.textContent ?? undefined,
url: item.querySelector('link')?.textContent ?? undefined
}
})
}
export const rss: Loader = {
getMineLinksFromText(text) {
let type = 'application/rss+xml'
let headerLinks = findHeaderLinks(text, type)
if (!isHTML(text)) return headerLinks
return [
...headerLinks,
...findDocumentLinks(text, type),
...findAnchorHrefs(text, /\.rss|\/rss/i, /rss/i)
]
},
getPosts(task, url, text) {
if (text) {
return createPostsList(parsePosts(text), undefined)
} else {
return createPostsList(undefined, async () => {
return [parsePosts(await task.text(url)), undefined]
})
}
},
async getPostSource(feed, originId) {
let xml = await createDownloadTask().text(feed.url)
return parsePostSources(xml).find(i => {
return (
i.querySelector('guid')?.textContent === originId ||
i.querySelector('link')?.textContent === originId
)
})?.outerHTML
},
getSuggestedLinksFromText(text) {
return [new URL('/rss', new URL(text.url).origin).href]
},
isMineText(text) {
let document = text.parseXml()
if (document?.firstElementChild?.nodeName === 'rss') {
return document.querySelector('channel > title')?.textContent ?? ''
} else {
return false
}
},
isMineUrl() {
return undefined
}
}