Skip to content

Instantly share code, notes, and snippets.

@PieterSpruijt
Last active October 22, 2022 15:45
Show Gist options
  • Save PieterSpruijt/265a474e5c4cde21e3204d7d76bea5dd to your computer and use it in GitHub Desktop.
Save PieterSpruijt/265a474e5c4cde21e3204d7d76bea5dd to your computer and use it in GitHub Desktop.
Fetch data from Magic Eden Rpc without getting cloudflare captchas
/**
* @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);
/**
* @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);
});
@PieterSpruijt
Copy link
Author

PieterSpruijt commented Mar 25, 2022

Donations via Solana: 6fJaA7WTLT1Zj5DdPvDMcRjkLoQc7uXAeVzMQtJzUbR4
or https://solmate.spruijtstudios.com/donate/

@endustar
Copy link

does this script still work? just ran it and it gets stuck

@PieterSpruijt
Copy link
Author

PieterSpruijt commented May 10, 2022

**endustar ** commented 3 minuten geleden

Yes this does still work, I suggest just using axios and changing all endpoints with a .io link in it to .dev, that will fix the problem aswell
so
api-mainnet.magiceden.io/rpc/getNFTByMintAddress/7Y5ZoSVRxzijRocc1w1ycwFws8EcaAKbQgaW1qewfxRy
to
api-mainnet.magiceden.dev/rpc/getNFTByMintAddress/7Y5ZoSVRxzijRocc1w1ycwFws8EcaAKbQgaW1qewfxRy

@endustar
Copy link

when I change it to .dev is there even cloudflare protection? seems like I can hit .dev with just a fetch and nothing else

@PieterSpruijt
Copy link
Author

when I change it to .dev is there even cloudflare protection? seems like I can hit .dev with just a fetch and nothing else

.dev is unprotected to use fetch, axios or node-fetch, that will just work!!

@arturferreira
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment