import got from 'got'; | |
import * as cheerio from 'cheerio'; | |
import { parseFileSize } from './util.js'; | |
import { DEFAULT_HEADERS } from '../constant.js'; | |
import { SfilemobiSearchArgsSchema, SfilemobiSearchSchema } from '../types/sfilemobiesearch-v1.js'; | |
export async function sfilemobiSearch(query, page = 1) { | |
SfilemobiSearchArgsSchema.parse([query, page]); | |
const data = await got('https://sfile.mobi/search.php', { | |
searchParams: { | |
q: query, | |
page | |
}, | |
headers: { | |
...DEFAULT_HEADERS | |
} | |
}).text(); | |
const $ = cheerio.load(data); | |
const results = []; | |
$('div > div > div.list').each((_, el) => { | |
const $el = $(el); | |
const url = $el.find('a').attr('href') || null; | |
const filename = $el.find('a').text() || null; | |
const icon = $el.find('img').attr('src') || null; | |
const typeMatch = icon ? /\/smallicon\/(.*?)\.svg/.exec(icon) : null; | |
const type = typeMatch ? typeMatch[1] : null; | |
const filesizeHMatch = /\((.*?)\)/.exec($el.text()); | |
const filesizeH = filesizeHMatch ? filesizeHMatch[1] : null; | |
const filesize = filesizeH ? parseFileSize(filesizeH) : null; | |
if (filename && url) { | |
results.push({ | |
url, | |
filename, | |
icon, | |
type, | |
filesizeH, | |
filesize | |
}); | |
} | |
}); | |
return SfilemobiSearchSchema.parse(results); | |
} |