Skip to content

Commit

Permalink
Fix showlist json wasn't saved along images
Browse files Browse the repository at this point in the history
  • Loading branch information
niemisami committed Apr 11, 2024
1 parent ab330ea commit 44b013b
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions scripts/google/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Color, Show, ShowsByDate, showsToGroups } from './showlistHelpers';
import { sheets_v4 } from '@googleapis/sheets';
import { addMilliseconds, formatISO, getHours } from 'date-fns';

Expand All @@ -8,7 +7,9 @@ import {
ensureDirectoryExists,
getImagePath,
saveArrayBufferToFile,
writeFile,
} from '@/utils/fileHelpers';
import { Color, Show, ShowsByDate, showsToGroups } from './showlistHelpers';

const NEXT_URL = '/showlist' as const;
const FILE_URL = `./public${NEXT_URL}` as const;
Expand Down Expand Up @@ -72,6 +73,13 @@ export const parseSheetToShowList = async (
return acc;
}
const shows = await acc;
if (index > 0 && !shows[index - 1]) {
throw new Error(
`Failed to parse showlist sheet. Invalid data before ${JSON.stringify(
sheetRow
)}`
);
}

const previousEndTime = index ? shows[index - 1].end : showStartTime;
const startDate = new Date(previousEndTime);
Expand Down Expand Up @@ -116,6 +124,10 @@ export const parseSheetToShowList = async (
return showList;
};

export const saveShowlistJson = async (data: Record<string, unknown>) => {
await writeFile(`${FILE_URL}/ohjelmakartta.json`, JSON.stringify(data));
};

export const fetchShowlist = async (): Promise<ShowsByDate> => {
const data = await getSheet({
apiKey: process.env.GA_API_KEY,
Expand All @@ -128,7 +140,10 @@ export const fetchShowlist = async (): Promise<ShowsByDate> => {
const shows = data
? await parseSheetToShowList(data, { apiKey: process.env.GA_API_KEY })
: [];
return showsToGroups(shows);

const showsByDate = showsToGroups(shows);
await saveShowlistJson(showsByDate);
return showsByDate;
};

const downloadShowFile = async (
Expand Down

0 comments on commit 44b013b

Please sign in to comment.