-
-
Save dtmrc/722e13d586b9579eccc55d9d8eb5f3dc to your computer and use it in GitHub Desktop.
Fetch data from Magic Eden Rpc without getting cloudflare captchas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file Fetch data from Magic Eden Rpc without getting cloudflare captchas | |
* @author Pieter Spruijt <[email protected]> | |
*/ | |
/* | |
First install the package | |
npm install cloudscraper | |
*/ | |
const cloudscraper = require('cloudscraper'); | |
// Create the fetchUrl function | |
const fetchUrl = async (url) => { | |
const delay = m => new Promise((resolve, reject) => { setTimeout(_ => resolve(), m) }); | |
try { | |
const response = await cloudscraper.get(url).catch(async (err) => { | |
if (err.statusCode) return; | |
await delay(1000); | |
return fetchUrl(url); | |
}); | |
if (!response) return; | |
return JSON.parse(response); | |
} catch (e) { | |
await delay(1000); | |
return fetchUrl(url); | |
} | |
}; | |
// For non async modules | |
fetchUrl(`https://api-mainnet.magiceden.io/rpc/getNFTByMintAddress/7Y5ZoSVRxzijRocc1w1ycwFws8EcaAKbQgaW1qewfxRy`).then((data) => { | |
console.log(data); | |
}); | |
//For async modules | |
const res = await fetchUrl(`https://api-mainnet.magiceden.io/rpc/getNFTByMintAddress/7Y5ZoSVRxzijRocc1w1ycwFws8EcaAKbQgaW1qewfxRy`); | |
console.log(res); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file Fetch data from Magic Eden Rpc without getting cloudflare captchas | |
* @author Pieter Spruijt <[email protected]> | |
*/ | |
/* | |
First install the package | |
npm install cloudscraper | |
*/ | |
const cloudscraper = require('cloudscraper'); | |
// Create the fetchUrl function | |
const fetchUrl = async (url: string): Promise<{ data: object; }> => { | |
function delay(ms: number) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} | |
try { | |
const response = await cloudscraper.get(url).catch(async (err: { statusCode: number }) => { | |
if (err.statusCode) return; | |
await delay(1000); | |
return fetchUrl(url); | |
}); | |
if (!response) return { | |
data: { | |
error: 'No response' | |
} | |
}; | |
return { | |
data: JSON.parse(response) | |
}; | |
} catch (e) { | |
await delay(1000); | |
return fetchUrl(url); | |
} | |
}; | |
//For async modules | |
async function test() { | |
const res = await fetchUrl(`https://api-mainnet.magiceden.io/rpc/getCollectionEscrowStats/shukutai_dinos`); | |
console.log(res); | |
} | |
test(); | |
fetchUrl(`https://api-mainnet.magiceden.io/rpc/getNFTByMintAddress/7Y5ZoSVRxzijRocc1w1ycwFws8EcaAKbQgaW1qewfxRy`).then((data) => { | |
console.log(data); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment