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'; const proxies = [ '176.112.80.253:12323:14ae0dc1e2459:4364101eab', '176.112.83.69:12323:14ae0dc1e2459:4364101eab', '176.112.83.203:12323:14ae0dc1e2459:4364101eab', '176.112.80.150:12323:14ae0dc1e2459:4364101eab', '176.112.82.121:12323:14ae0dc1e2459:4364101eab' ].map((proxy_str) => { const [ip, port, user, password] = proxy_str.split(':'); return { ip, port, user, password }; }); const random_int = (max, min) => Math.floor(Math.random() * (max - min) + min); export const do_request = async (doc_id, variables, parse = true) => { const params = new URLSearchParams(); const { ip, port, user, password } = proxies[random_int(0, 4)]; let proxyOpts = url.parse(`http://${ip}:${port}`); proxyOpts.auth = `${user}:${password}`; 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; };