rssssing/platforms/nitter.js

82 lines
2.3 KiB
JavaScript

import Path from "path"
import { createSource, createSourceOptions, extractImages, downloadImages, getPostIdFromPathname, fetchChannel } from "../lib.js"
let nitter = {}
export const fetchChannelFromInstances = async (source) => {
let cachedLinkIndex = source.instances.indexOf(source.cache.link)
let index = 0
let instances
let channel
if(cachedLinkIndex !== -1) {
instances = source.instances.toSpliced(cachedLinkIndex, 1)
instances.unshift(source.instances[cachedLinkIndex])
} else {
instances = [ ...source.instances ]
}
while(!channel && index != instances.length) {
source.hostname = instances[index]
channel = await fetchChannel(source)
if(source.errored) {
console.error(`Failed to fetch ${source.name} from ${source.hostname}: `, source.error)
index++
} else {
break
}
}
return channel
}
nitter.createSource = (user, options, instances, postReducerCallback, cache) => {
let source = {
type: 'nitter',
description: `Aggregate feed for @${user} on twitter.com`,
instances,
pathname: user + '/rss',
name: `nitter-${user}`,
displayName: user,
user,
...createSourceOptions(options)
}
return createSource(source, fetchChannelFromInstances, postReducerCallback, cache)
}
nitter.isRepost = (post) => {
let creator = post.item.getElementsByTagName('dc:creator')[0]
return creator.innerHTML.slice(1) !== post.source.user
}
nitter.pullImages = async (post, view, imageMirrorDomain, discardPostIfNoImages = false, getPostId = getPostIdFromPathname) => {
let images = extractImages(post)
const mirrorImage = nitter.createImageMirrorer(post, imageMirrorDomain)
if(!discardPostIfNoImages || images.length > 0) {
post.images = await downloadImages(
images.map(mirrorImage),
post.source,
getPostId(post),
view
)
return post
}
}
nitter.createImageMirrorer =(post, imageMirrorDomain) => {
let mirrorUrl = new URL(imageMirrorDomain)
let basePathname = new URL(post.guid).pathname
return (image, index, images) => {
mirrorUrl.pathname = Path.join(basePathname, 'photo', (index + 1).toString())
return mirrorUrl.href
}
}
export default nitter