Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exo2 #20

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Exo2 #20

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions exo2/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Where's the cheapest gaz?

## Description
Expand All @@ -16,7 +15,7 @@ Example (top 20 sales outlets in the Pays de la Loire region): <https://data.eco
You will provide a CLI script displaying for each region and each cheapest fuel: the price and the full address of the point of sale.
The choice of language is free (within reason). Example: PHP, JS (Node), Python, Go, Shellscript, etc.

>[!NOTE]
> [!NOTE]
> Make sure that the script searches over the **9994 stations** in the dataset, and not only the 20 stations of the example.

Return example:
Expand All @@ -37,3 +36,13 @@ etc.
You will have to make a PR (Pull request) with executable code as clean as possible.

[]: # Path: exo2/README.md

## How to use it

Make sure node.js is installed on your machine.

In your terminal, naviguate to the directory of the project: "../exo2".

No dependencies needed in this project.

Run `npm start` to execute script and enjoy the tips to get lower price fuel !
74 changes: 74 additions & 0 deletions exo2/cheapestgaz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
async function cheapestGaz() {
const response = await fetch(
`https://data.economie.gouv.fr/api/explore/v2.1/catalog/datasets/prix-des-carburants-en-france-flux-instantane-v2/records?select=region&group_by=region`
);
const data = await response.json();
const regions = data.results;

const regionName = [];

// I loop through the data to get all the region_code
regions.forEach((region) => {
// If the region_code already exist in the array, I don't push it
if (regionName.includes(region.region) || region.region === null) {
return;
}
// If the region_code doesn't exist in the array, I push it
regionName.push(region.region);
return regionName.sort();
});

for (const item of regionName) {
const sp95 = "sp95_prix";
const sp98 = "sp98_prix";
const gazole = "gazole_prix";

// The filter is done with the URL. Each URL return the element where the price for each fuel is the lowest
const apiResponseForsp95 = await fetch(
`https://data.economie.gouv.fr/api/explore/v2.1/catalog/datasets/prix-des-carburants-en-france-flux-instantane-v2/records?select=adresse%2C%20ville%20%2C%20cp%20%2C%20sp95_prix%2C%20sp98_prix%20%2C%20gazole_prix&where=region%20%3D%20%22${item}%22&order_by=${sp95}&limit=1`
);

const apiResponseForsp98 = await fetch(
`https://data.economie.gouv.fr/api/explore/v2.1/catalog/datasets/prix-des-carburants-en-france-flux-instantane-v2/records?select=adresse%2C%20ville%20%2C%20cp%20%2C%20sp95_prix%2C%20sp98_prix%20%2C%20gazole_prix&where=region%20%3D%20%22${item}%22&order_by=${sp98}&limit=1`
);

const apiResponseForgazole = await fetch(
`https://data.economie.gouv.fr/api/explore/v2.1/catalog/datasets/prix-des-carburants-en-france-flux-instantane-v2/records?select=adresse%2C%20ville%20%2C%20cp%20%2C%20sp95_prix%2C%20sp98_prix%20%2C%20gazole_prix&where=region%20%3D%20%22${item}%22&order_by=${gazole}&limit=1`
);

const datasp95 = await apiResponseForsp95.json();
const datasp98 = await apiResponseForsp98.json();
const datagazole = await apiResponseForgazole.json();

const finalDataSP95 = datasp95.results[0];
const finalDataSP98 = datasp98.results[0];
const finalDataGazole = datagazole.results[0];

let logText = `
${item} :
`;

if (finalDataSP95.sp95_prix) {
logText += `SP95 : ${finalDataSP95.sp95_prix} € / ${finalDataSP95.adresse} ${finalDataSP95.cp} ${finalDataSP95.ville}\n`;
} else {
logText += "SP95 : Il n'y as pas de Sans Plomb 95 dans cette région\n";
}

if (finalDataSP98.sp98_prix) {
logText += `SP98 : ${finalDataSP98.sp98_prix} € / ${finalDataSP98.adresse} ${finalDataSP98.cp} ${finalDataSP98.ville}\n`;
} else {
logText +=
"SP98 : Il n'y as pas de Sans Plomb 98 dans cette région \n";
}

if (finalDataGazole.gazole_prix) {
logText += `Gazole : ${finalDataGazole.gazole_prix} € / ${finalDataGazole.adresse} ${finalDataGazole.cp} ${finalDataGazole.ville}\n`;
} else {
logText += "Gazole : Il n'y as pas de Gazole dans cette région \n";
}

console.log(logText);
}
}

cheapestGaz();
12 changes: 12 additions & 0 deletions exo2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "exo2",
"version": "1.0.0",
"description": "## Description",
"main": "cheapestgaz.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start" : "node cheapestgaz.js"
},
"author": "",
"license": "ISC"
}