Spaces:
Paused
Paused
import got from 'got'; | |
import * as cheerio from 'cheerio'; | |
import { DEFAULT_HEADERS } from '../constant.js'; | |
import { SfilemobidlArgsSchema, SfilemobidlSchema } from '../types/sfilemobi-dl-v1.js'; | |
export async function sfilemobi(url) { | |
SfilemobidlArgsSchema.parse({ url }); | |
const data = await got(url, { | |
headers: { ...DEFAULT_HEADERS } | |
}).text(); | |
const $ = cheerio.load(data); | |
const dlUrl = $('#download').attr('href') || null; | |
const filename = $('div.intro-container > img').attr('alt') || $('div.intro-container > h1').text() || null; | |
const icon = $('div.intro-container > img').attr('src') || null; | |
const typeMatch = icon ? /\/smallicon\/(.*?)\.svg/.exec(icon) : null; | |
const type = typeMatch ? typeMatch[1] : null; | |
const $list = $('div.list'); | |
const mimetype = $list.eq(0).text().split('-')[1]?.trim() || null; | |
const uploaded = $list.eq(2).text().split('Uploaded:')[1]?.trim() || null; | |
const $upload = $list.eq(1).find('a'); | |
const uploadby = $upload.eq(0).text() || null; | |
const uploadbyUrl = $upload.eq(0).attr('href') || null; | |
const uploadon = $upload.eq(1).text() || null; | |
const uploadonUrl = $upload.eq(1).attr('href') || null; | |
const downloadsText = $list.eq(3).text().split('Downloads:')[1]; | |
const downloads = downloadsText ? parseInt(downloadsText) : null; | |
const result = { | |
url: dlUrl, | |
filename, | |
icon, | |
type, | |
mimetype, | |
uploaded, | |
uploadby, | |
uploadbyUrl, | |
uploadon, | |
uploadonUrl, | |
downloads | |
}; | |
return SfilemobidlSchema.parse(result); | |
} |