stream-api/backend/seafile.js

64 lines
2.1 KiB
JavaScript

#!/usr/bin/env node
const esprima = require('esprima');
const cheerio = require('cheerio');
const axios = require('axios');
let url = null;
for(let i=0; i<process.argv.length; i++) {
const arg = process.argv[i];
if(arg.startsWith("http://") || arg.startsWith("https://")) {
url = arg;
}
}
if(url !== null) {
const search = new URLSearchParams();
search.set("dl", "1");
const direct = new URL(url);
direct.search = search.toString();
axios.get(url).then((resp) => {
const dom = cheerio.load(resp.data, {xmlMode: false});
const tags = dom('script').get();
const data = {};
let props = [];
for(let i=0; i<tags.length; i++) {
const children = tags[i].children;
for(let j=0; j<children.length; j++) {
let tdata = {};
let assign = true;
try {
tdata = children[j].data;
} catch(err) {
assign = false;
console.log(err);
}
if(assign) {
const script = esprima.parseScript(tdata);
for(let k=0; k<script.body.length; k++) {
const prop = script.body[k].expression.right.properties;
for(let l=0; l<prop.length; l++) {
props = [...props, ...(prop[l].value.properties)];
}
}
}
}
}
for(let i=0; i<props.length; i++) {
const prop = props[i];
if((prop.key.type === "Identifier") && (prop.value.type === "Literal")) {
data[prop.key.name] = prop.value.value;
}
}
for(let i=0; i<props.length; i++) {
const prop = props[i];
const valid = (new Boolean(prop.value.value)).valueOf();
if((prop.key.type === "Identifier") && (prop.value.type === "Literal") && valid) {
data[prop.key.name] = prop.value.value;
}
}
data["rawPath"] = direct.href;
console.log(JSON.stringify(data));
});
}