You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.2 KiB

4 years ago
import fetch from 'node-fetch';
const graphql_endpoint = 'https://www.facebook.com/api/graphql/';
4 years ago
import * as url from 'url';
import https_proxy_agent from 'https-proxy-agent';
5 years ago
4 years ago
export const do_request = async (doc_id, variables, parse = true) => {
const params = new URLSearchParams();
4 years ago
const ip = '127.0.0.1';
const port = '24000';
let proxyOpts = url.parse(`http://${ip}:${port}`);
4 years ago
const agent = new https_proxy_agent(proxyOpts);
params.append('doc_id', doc_id);
params.append('variables', JSON.stringify(variables));
const fetch_options = {
headers: {
4 years ago
'Content-Type': 'application/x-www-form-urlencoded'
},
body: params,
method: 'POST',
4 years ago
agent
};
let res = null;
try {
res = await fetch(graphql_endpoint, fetch_options);
} catch (e) {
console.error(e);
return null;
}
4 years ago
const txt = await res.text();
if (!res.ok) {
console.error(txt);
return null;
}
4 years ago
if (parse) {
try {
res = JSON.parse(txt);
if (res.errors != null) {
console.error(JSON.stringify(res.errors));
}
4 years ago
} catch (e) {
console.error(e);
return null;
}
} else {
if (txt.length == 0) {
return null;
} else {
return txt;
}
}
return res;
};