11const FUEL_FINDER_CSV_URL = 'https://www.fuel-finder.service.gov.uk/internal/v1.0.2/csv/get-latest-fuel-prices-csv' ;
2+ const FUEL_FINDER_ARCHIVE_URL = 'https://raw.githubusercontent.com/matthewgall/fuelfinder-archive/refs/heads/main/data.csv' ;
23const FUEL_FINDER_HEADERS = {
34 'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36' ,
45 'Accept' : 'text/csv,application/octet-stream;q=0.9,*/*;q=0.8' ,
@@ -270,10 +271,29 @@ async function fetchFuelFinderCsv(url: string, maxRedirects: number = 3) {
270271}
271272
272273export async function updateFuelFinderSnapshot ( env : any ) {
273- const { response, finalUrl } = await fetchFuelFinderCsv ( FUEL_FINDER_CSV_URL ) ;
274-
275- if ( ! response . ok ) {
276- throw new Error ( `Fuel Finder CSV download failed with status ${ response . status } ` ) ;
274+ let response : Response | undefined ;
275+ let finalUrl = FUEL_FINDER_CSV_URL ;
276+ let usedFallback = false ;
277+
278+ try {
279+ const result = await fetchFuelFinderCsv ( FUEL_FINDER_CSV_URL ) ;
280+ response = result . response ;
281+ finalUrl = result . finalUrl ;
282+ if ( ! response . ok ) {
283+ throw new Error ( `Fuel Finder CSV download failed with status ${ response . status } ` ) ;
284+ }
285+ } catch ( error ) {
286+ usedFallback = true ;
287+ const fallbackResponse = await fetch ( FUEL_FINDER_ARCHIVE_URL , {
288+ headers : FUEL_FINDER_HEADERS
289+ } ) ;
290+ if ( ! fallbackResponse . ok ) {
291+ throw new Error (
292+ `Fuel Finder CSV fallback failed with status ${ fallbackResponse . status } `
293+ ) ;
294+ }
295+ response = fallbackResponse ;
296+ finalUrl = FUEL_FINDER_ARCHIVE_URL ;
277297 }
278298
279299 const csvText = await response . text ( ) ;
@@ -290,7 +310,8 @@ export async function updateFuelFinderSnapshot(env: any) {
290310 lastUpdated : data . last_updated ,
291311 capturedAt : new Date ( ) . toISOString ( ) ,
292312 sourceUrl : FUEL_FINDER_CSV_URL ,
293- finalUrl
313+ finalUrl,
314+ usedFallback
294315 } ) ) ;
295316
296317 return { stats, lastUpdated : data . last_updated } ;
0 commit comments