import { $fetch } from 'ofetch'
import * as cheerio from 'cheerio'
import { LRUCache } from 'lru-cache'
import { getEnv } from '../env'
const cache = new LRUCache({
ttl: 1000 * 60 * 5, // 5 minutes
maxSize: 50 * 1024 * 1024, // 50MB
sizeCalculation: (item) => {
return JSON.stringify(item).length
},
})
function getImages($, item, { staticProxy, id, index, title }) {
return $(item).find('.tgme_widget_message_photo_wrap')?.map((_index, photo) => {
const url = $(photo).attr('style').match(/url\(["'](.*?)["']/)?.[1]
const popoverId = `modal-${id}-${_index}`
return `
`
})?.get()?.join('')
}
function getVideo($, item, { staticProxy, index }) {
const video = $(item).find('.tgme_widget_message_video_wrap video')
video?.attr('src', staticProxy + video?.attr('src'))
?.attr('controls', true)
?.attr('preload', index > 15 ? 'auto' : 'metadata')
?.attr('playsinline', true).attr('webkit-playsinline', true)
const roundVideo = $(item).find('.tgme_widget_message_roundvideo_wrap video')
roundVideo?.attr('src', staticProxy + roundVideo?.attr('src'))
?.attr('controls', true)
?.attr('preload', index > 15 ? 'auto' : 'metadata')
?.attr('playsinline', true).attr('webkit-playsinline', true)
return $.html(video) + $.html(roundVideo)
}
function getLinkPreview($, item, { staticProxy, index }) {
const link = $(item).find('.tgme_widget_message_link_preview')
const title = $(item).find('.link_preview_title')?.text() || $(item).find('.link_preview_site_name')?.text()
const description = $(item).find('.link_preview_description')?.text()
link?.attr('target', '_blank').attr('rel', 'noopener').attr('title', description)
const image = $(item).find('.link_preview_image')
const src = image?.attr('style')?.match(/url\(["'](.*?)["']/i)?.[1]
const imageSrc = src ? staticProxy + src : ''
image?.replaceWith(``)
return $.html(link)
}
function getPost($, item, { channel, staticProxy, index = 0 }) {
item = item ? $(item).find('.tgme_widget_message') : $('.tgme_widget_message')
const content = $(item).find('.tgme_widget_message_bubble > .tgme_widget_message_text')
const title = content?.text()?.match(/[^。\n]*(?=[。\n]|http)/g)?.[0] ?? content?.text() ?? ''
const id = $(item).attr('data-post')?.replace(`${channel}/`, '')
$(content).find('a')?.each((_index, a) => {
$(a)?.attr('title', $(a)?.text())
})
$(content).find('.emoji')?.attr('style', '')
const tags = $(content).find('a[href^="?q="]')?.each((_index, a) => {
$(a)?.attr('href', `/search/${encodeURIComponent($(a)?.text())}`)
})?.map((_index, a) => $(a)?.text()?.replace('#', ''))?.get()
return {
id,
title,
type: $(item).attr('class')?.includes('service_message') ? 'service' : 'text',
datetime: $(item).find('.tgme_widget_message_date time')?.attr('datetime'),
tags,
text: content?.text(),
content: [
$.html($(item).find('.tgme_widget_message_reply')?.wrapInner('')?.wrapInner('