Last active
October 22, 2022 15:45
-
-
Save PieterSpruijt/265a474e5c4cde21e3204d7d76bea5dd 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 <pieter@spruijtstudios.com> | |
*/ | |
/* | |
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 <pieter@spruijtstudios.com> | |
*/ | |
/* | |
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
Thank you very much! This works fine locally, but when running from a managed server, I'm hitting some blocks.
It works fine for:
https://api-mainnet.magiceden.dev/all_collections
But fails for:
https://api-mainnet.magiceden.io/rpc/getListedNftsByCollectionSymbol?collectionSymbol=bet3&direction=2&field=1&limit=3&offset=0
The one where it fails doesn't have a .dev equivalent.
Any idea?
Thanks a lot!