-
Notifications
You must be signed in to change notification settings - Fork 37
/
rss.ts
84 lines (74 loc) · 2.4 KB
/
rss.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
76
77
78
79
80
81
82
83
84
import type { TextResponse } from '../download.ts'
import type { OriginPost } from '../post.ts'
import { createPostsList } from '../posts-list.ts'
import type { Loader } from './index.ts'
import {
findAnchorHrefs,
findImageByAttr,
findLinksByType,
isHTML,
toTime,
unique
} from './utils.ts'
const MEDIA_NS_URI = 'http://search.yahoo.com/mrss/'
function parsePosts(text: TextResponse): OriginPost[] {
let document = text.parseXml()
if (!document) return []
return [...document.querySelectorAll('item')]
.filter(
item =>
item.querySelector('guid')?.textContent ??
item.querySelector('link')?.textContent
)
.map(item => {
let description = item.querySelector('description')
let descriptionImageElements = description?.querySelectorAll('img')
let descriptionImages = findImageByAttr('src', descriptionImageElements)
let mediaImageElements = [
...item.getElementsByTagNameNS(MEDIA_NS_URI, 'content')
].filter(element => element.getAttribute('medium') === 'image')
let mediaImages = findImageByAttr('url', mediaImageElements)
return {
full: description?.textContent ?? undefined,
media: unique([...descriptionImages, ...mediaImages]),
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) {
if (!isHTML(text)) return []
return [
...findLinksByType(text, 'application/rss+xml'),
...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]
})
}
},
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
}
}