Skip to content

Instantly share code, notes, and snippets.

@victor141516
Created May 2, 2022 12:52
Show Gist options
  • Save victor141516/982f015cefc2e08d49ca371cb21e2a0a to your computer and use it in GitHub Desktop.
Save victor141516/982f015cefc2e08d49ca371cb21e2a0a to your computer and use it in GitHub Desktop.
get purchases items from carrefour spanish web
import fetch from "node-fetch";
import { writeFileSync } from "fs";
const HEADERS = {/* copy from chrome */};
const getPurchaseItems = async (id) => {
console.log("getting purchase:", id);
const prom = fetch(
`https://api.carrefour.es/marketing-digital/prod/tickets/${id}`,
{
insecureHTTPParser: true,
"headers": HEADERS,
"body": null,
"method": "GET",
},
).then((r) => r.json());
try {
const res = await prom;
console.log({ res });
return res.items;
} catch (err) {
console.log("error getting purchase:", id);
}
};
const listPurchases = async () => {
const allPurchases = new Set();
let offset = 0;
let prevSize = 0;
while (true) {
const { purchases } = await fetch(
`https://api.carrefour.es/marketing-digital/prod/purchases?from=2000-05-02T12:10:33.276Z&to=2200-05-03T12:10:33.276Z&atgfOffset=1&atgnfOffset=0&currentAtgfOrders=0&currentAtgnfOrders=0&currentTickets=10&ticketOffset=${offset}&count=10&getEcommerceSession=false`,
{
insecureHTTPParser: true,
headers: HEADERS,
},
).then((r) => r.json());
purchases.map(({ purchaseId }) => purchaseId).forEach((e) =>
allPurchases.add(e)
);
console.log(allPurchases);
if (allPurchases.size !== prevSize) {
prevSize = allPurchases.size;
offset += 10;
} else break;
}
return Array.from(allPurchases);
};
(async () => {
const allPurchases = await listPurchases();
const allItems = await Promise.all(allPurchases.map(async (p) => {
const a = await getPurchaseItems(p);
console.log({ a });
return a;
}));
console.log(JSON.stringify(allItems));
writeFileSync("./items.json", JSON.stringify(allItems));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment