Create sfilemobi/sfilemobi.js
Browse files- lib/sfilemobi/sfilemobi.js +47 -0
lib/sfilemobi/sfilemobi.js
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import got from 'got';
|
2 |
+
import * as cheerio from 'cheerio';
|
3 |
+
import { parseFileSize } from './util.js';
|
4 |
+
import { DEFAULT_HEADERS } from './constant.js';
|
5 |
+
import { SfilemobiSearchArgsSchema, SfilemobiSearchSchema } from '../types/sfilemobie-v1.js';
|
6 |
+
|
7 |
+
export default async function sfilemobiSearch(query, page = 1) {
|
8 |
+
SfilemobiSearchArgsSchema.parse([query, page]);
|
9 |
+
|
10 |
+
const data = await got('https://sfile.mobi/search.php', {
|
11 |
+
searchParams: {
|
12 |
+
q: query,
|
13 |
+
page
|
14 |
+
},
|
15 |
+
headers: {
|
16 |
+
...DEFAULT_HEADERS
|
17 |
+
}
|
18 |
+
}).text();
|
19 |
+
|
20 |
+
const $ = cheerio.load(data);
|
21 |
+
const results = [];
|
22 |
+
|
23 |
+
$('div > div > div.list').each((_, el) => {
|
24 |
+
const $el = $(el);
|
25 |
+
const url = $el.find('a').attr('href') || null;
|
26 |
+
const filename = $el.find('a').text() || null;
|
27 |
+
const icon = $el.find('img').attr('src') || null;
|
28 |
+
const typeMatch = icon ? /\/smallicon\/(.*?)\.svg/.exec(icon) : null;
|
29 |
+
const type = typeMatch ? typeMatch[1] : null;
|
30 |
+
const filesizeHMatch = /\((.*?)\)/.exec($el.text());
|
31 |
+
const filesizeH = filesizeHMatch ? filesizeHMatch[1] : null;
|
32 |
+
const filesize = filesizeH ? parseFileSize(filesizeH) : null;
|
33 |
+
|
34 |
+
if (filename && url) {
|
35 |
+
results.push({
|
36 |
+
url,
|
37 |
+
filename,
|
38 |
+
icon,
|
39 |
+
type,
|
40 |
+
filesizeH,
|
41 |
+
filesize
|
42 |
+
});
|
43 |
+
}
|
44 |
+
});
|
45 |
+
|
46 |
+
return SfilemobiSearchSchema.parse(results);
|
47 |
+
}
|