Skip to content

Instantly share code, notes, and snippets.

@felinae98
Created February 17, 2021 11:45
Show Gist options
  • Save felinae98/aeedffb2cd0fe4201bfed02d11ed6461 to your computer and use it in GitHub Desktop.
Save felinae98/aeedffb2cd0fe4201bfed02d11ed6461 to your computer and use it in GitHub Desktop.
arknights-setu-rss-proxy
import { FetchRouter } from 'cf-worker-router'
const DOMParser = require('xmldom').DOMParser
const xmlserializer = require('xmlserializer')
// const JSDOM = require('jsdom').JSDOM
const DOMAIN = 'https://arknights-setu.bitnp-rhode.workers.dev/'
const router = new FetchRouter()
addEventListener('fetch', event => {
// event.respondWith(handleRequest(event.request))
router.onFetch(event)
})
router.route('/proxy/', async (event) => {
const request = event.request
let originUrl = new URL(request.url)
let subdomain = originUrl.searchParams.get('domain')
if (subdomain.search(/^[\w\d]+$/) === -1) {
return new Response('Forbiden', {status: 400})
}
let fileName = originUrl.searchParams.get('file')
let targetUrl = `https://${subdomain}.telesco.pe/file/${fileName}`
// console.log(targetUrl)
let req = new Request(targetUrl)
let res = await fetch(req)
return res
})
// function cdata ()
router.route('/feed/', async (event) => {
const transImgUrl = function (rawUrl) {
const match = rawUrl.match(/https:\/\/([\w\d]+)\.telesco.pe\/file\/(.+)/)
if (match.length != 3) {
console.log('img format gg')
return false
}
const parsedImg = `${DOMAIN}proxy/?domain=${match[1]}&file=${match[2]}`
return parsedImg
}
const rawXML = await fetch('https://rsshub.app/telegram/channel/arknights_anime').then(res => res.text())
const xmlParser = new DOMParser()
const XMLDom = xmlParser.parseFromString(rawXML, 'text/xml')
// console.log(XMLDom.getElementsByTagName('description').$$length)
const icon = XMLDom.getElementsByTagName('url')[0]
icon.textContent = transImgUrl(icon.textContent)
const descs = XMLDom.getElementsByTagName('description')
// console.log(descs.$$length)
for(let index = 1; index < descs.$$length; index ++) {
// for(let index = 1; index < 2; index ++) {
const des = descs[index]
const htmlDom = xmlParser.parseFromString(des.textContent, 'text/xml')
const imgs = htmlDom.getElementsByTagName('img')
for (let imgIndex = 0; imgIndex < imgs.$$length; imgIndex ++) {
const img = imgs[imgIndex]
const parsedImg = transImgUrl(img.getAttribute('src'))
img.setAttribute('src', parsedImg)
console.log(parsedImg)
}
// const htmlRes = xmlserializer.serializeToString(htmlDom)
// console.log(htmlDom.toString())
des.textContent = htmlDom.toString()
}
// console.log(XMLDom.toString())
return new Response(XMLDom.toString(), { headers: {'content-type': 'application/xml'}})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment