// Name: Oil prices // Description: Prices of oil stations in Spain // Author: Alfredo de la Calle // Github: https://github.com/bytelovers import "@johnlindquist/kit"; import dayjs from "dayjs"; const baseUrl = "https://sedeaplicaciones.minetur.gob.es"; const basePath = `${baseUrl}/ServiciosRESTCarburantes/PreciosCarburantes`; const listadosEndpoint = `${basePath}/Listados`; const preciosEndpoint = `${basePath}/EstacionesTerrestresHist`; const previewEESS = async (municipalities) => { const EESSbyMunicipios = async (date = null, municipios) => { const _data = []; const prs = municipios.map(async (municipio) => { try { const { data } = await get( `${preciosEndpoint}/FiltroMunicipio/${date}/${municipio}` ); return data; } catch (e) { return { ListaEESSPrecio: [] }; } }); await Promise.all(prs).then((results) => results.forEach((result) => { _data.push(...result.ListaEESSPrecio); }) ); return { ListaEESSPrecio: _data }; }; const stations = await EESSbyMunicipios( dayjs().subtract(1, "day").format("DD-MM-YYYY"), municipalities ); const results = stations.ListaEESSPrecio .map((station) => { const { Rótulo: name, Dirección: address, "Precio Gasolina 95": gasolina95, "Precio Gasolina 98": gasolina98, "Precio Gasoleo A": gasoleoA, "Precio Gasoleo Premium": gasoleoPremium, "Precio Gasoleo B": gasoleoB, "Precio Gasolina 95 E5": gasolina95E5, "Precio Gasolina 98 E5": gasolina98E5, "Fecha Suministro": fechaSuministro, } = station; return `|${name}|${address}|${gasolina95E5 ?? ""}|${gasolina98E5 ?? ""}|${ gasoleoA ?? "" }|${gasoleoPremium ?? ""}|${gasoleoB ?? ""}|${fechaSuministro ?? ""}|`; }) .join("\n"); return results; }; let provinceSelected = await arg({ placeholder: "Select a province", choices: async () => { const { data } = await get(`${listadosEndpoint}/Provincias`); return data?.map((p) => ({ name: p.Provincia, value: p.IDPovincia })); }, }); await arg({ placeholder: "Select a locality", choices: async () => { const { data } = await get( `${listadosEndpoint}/MunicipiosPorProvincia/${provinceSelected}` ); return data?.map((p) => ({ name: p.Municipio, value: p.IDMunicipio, preview: async () => { const options = await previewEESS([p.IDMunicipio]); return md(` |Estación|Dirección|Precio Gasolina 95|Precio Gasolina 98|Precio Gasoleo A|Precio Gasoleo Premium|Precio Gasoleo B|Fecha Suministro| |---|---|---|---|---|---|---|---| ${options} `); }, })); }, });