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