Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
niemisami committed Jan 30, 2024
2 parents 898f9ce + edcb593 commit 41fcd1e
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 262 deletions.
3 changes: 1 addition & 2 deletions archiver/archive.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Show, showsToGroups } from 'contentful/client';
import { Showlist } from 'google/client';
import { Show, Showlist, showsToGroups } from 'scripts/google/showlistHelpers';

const showlistBaseUrl = process.env.ARCHIVE_SOURCE_URL;
const emptyResponse = { showsByDate: [], weekKeys: {} } as const;
Expand Down
78 changes: 2 additions & 76 deletions archiver/archiveCrawlers.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
import {
Show,
fetchContent as fetchContentfulContent,
parseQueryResultToShowlist,
} from 'contentful/client';
import {
ShowlistDocument,
ShowlistQuery,
} from 'contentful/graphql/showlist.graphql';
import { parseSheetToShowList, showsToGroups } from 'google/client';
import { GoogleConfigSheets, getSheet } from 'google/google';
import { parseSheetToShowList } from 'scripts/google/client';
import { GoogleConfigSheets, getSheet } from 'scripts/google/google';
import { mkdirSync } from 'node:fs';
import fs from 'node:fs/promises';
import path from 'node:path';
import { getImagePath } from 'utils/fileHelpers';

export const archiveOldShowlists = async () => {
// NOTE: Uncomment for archiving contentful data
// const contentfullIds = [
// { showlistId: 'syssyradio2020', name: '2020-syssy' },
// { showlistId: 'wappuradio2021', name: '2021-wappu' },
// { showlistId: 'wappuradio2022', name: '2022-wappu' },
// { showlistId: 'syssyradio-2022', name: '2022-syssy' },
// ];
// for (const { showlistId, name } of contentfullIds) {
// await archiveContentful(showlistId, name);
// }
// NOTE: Uncomment for archiving google sheet data
// const sheetConfigs = [
// { name: '2023-wappu', showStartTime: '2023-04-20T12:00:00', config: { apiKey: process.env.GA_API_KEY, spreadsheetId: '1eHDK-MYm6B3BH8rewQr04-pd2IK4iFwsPY5HKkrVNJg', range: 'Ohjelmakartta!A3:J5' } },
Expand Down Expand Up @@ -54,64 +35,9 @@ const archiveJson = async (archivePath: string, showlistData: {}) => {
await fs.writeFile(archiveFilePath, JSON.stringify(showlistData), 'utf-8');
};

const downloadFile = async (url: string, destinationPath: string) => {
const response = await fetch(url);
const arrayBuffer = await response.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);

await fs.writeFile(destinationPath, buffer);
};

/** Contentful - Pre wappu 2023 */
const ARCHIVE_SOURCE_URL = process.env.ARCHIVE_SOURCE_URL || '';

const archiveContentful = async (showlistId: string, name: string) => {
const archivePath = getArchivePath(name);
const pictureFolder = path.join(archivePath, 'pictures');
mkdirp(archivePath);
mkdirp(pictureFolder);

const showlistResponse = await fetchContentfulContent<ShowlistQuery>(
ShowlistDocument,
{ showlistId }
);
// NOTE: when data is stored locally
// const showlistResponse = raw as ShowlistQuery;
const shows = parseQueryResultToShowlist(showlistResponse);

const showsWithNewFilePaths = await reconnectShowsWithLocalFiles(
shows,
pictureFolder,
ARCHIVE_SOURCE_URL
);
const showlistData = showsToGroups(showsWithNewFilePaths);

await archiveJson(archivePath, showlistData);
};

const reconnectShowsWithLocalFiles = async (
shows: Show[],
archivePath: string,
archiveUrl: string
): Promise<Show[]> => {
const showsWithLocalFiles = [];
for (const show of shows) {
const { name, pictureUrl } = show;
if (!pictureUrl) {
showsWithLocalFiles.push(show);
continue;
}
const extension = path.parse(pictureUrl).ext;
const newFilename = getImagePath('', name, extension);
const filePath = path.join(archivePath, newFilename);
const archiveFileUrl = path.join(archiveUrl, newFilename);
await downloadFile(pictureUrl, filePath);

showsWithLocalFiles.push({ ...show, pictureUrl: archiveFileUrl });
}
return showsWithLocalFiles;
};

/** Google Sheets - Post wappu 2023 */

const archiveGogleSheet = async (
Expand Down
2 changes: 1 addition & 1 deletion components/player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import Image from 'next/image';
import { useEffect, useState } from 'react';
import { format } from 'date-fns';

import { Show } from 'contentful/client';
import testcard from '../public/testcard.webp';
import placeholderImage from '../public/kuva_puuttuu_v2.jpeg';
import useShoutBoxAndVideo from 'hooks/useShoutboxAndVideo';
import { FiPause, FiPlay, FiMessageSquare, FiVideo } from 'react-icons/fi';
import { Show } from 'scripts/google/showlistHelpers';

const SHOW_REFRESH_TIME = 10000; // 10 seconds

Expand Down
2 changes: 1 addition & 1 deletion components/responsiveShowlist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Dispatch, SetStateAction, useState } from 'react';
import { BsArrowLeft, BsArrowRight } from 'react-icons/bs';

import { ShowCard } from 'components/showcard';
import { Show } from 'contentful/client';
import { Show } from 'scripts/google/showlistHelpers';

interface NavButton {
value: string | null;
Expand Down
2 changes: 1 addition & 1 deletion components/showcard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Image from 'next/image';
import { useState } from 'react';

import placeholderImage from '../public/kuva_puuttuu_v2.jpeg';
import { Show } from 'contentful/client';
import { Show } from 'scripts/google/showlistHelpers';

interface ShowCard {
show: Show;
Expand Down
2 changes: 1 addition & 1 deletion components/showlist.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState } from 'react';

import { Show } from 'contentful/client';
import { Show } from 'scripts/google/showlistHelpers';
import ResponsiveShowlist from './responsiveShowlist';
import ShowlistMap from './showlistMap';
import { ModeButton } from './button';
Expand Down
2 changes: 1 addition & 1 deletion components/showlistMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { differenceInMinutes, format, parse } from 'date-fns';
import fi from 'date-fns/locale/fi';
import { head, keys } from 'ramda';

import { Show } from 'contentful/client';
import { Show } from 'scripts/google/showlistHelpers';
import { ModeButton } from './button';
import { ShowCard } from './showcard';
import { WideScreencard } from './widescreen-card';
Expand Down
2 changes: 1 addition & 1 deletion components/widescreen-card.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Color } from 'contentful/client';
import { Color } from 'scripts/google/showlistHelpers';

interface WideScreenCardProps {
text: string;
Expand Down
89 changes: 1 addition & 88 deletions contentful/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,10 @@ import {
InMemoryCache,
NormalizedCacheObject,
} from '@apollo/client';
import {
addDays,
eachDayOfInterval,
eachWeekOfInterval,
format,
getISOWeek,
parse,
} from 'date-fns';
import { groupBy, head, keys, last } from 'ramda';
import {
NavigationItemsDocument,
NavigationItemsQuery,
} from './graphql/navigation.graphql';
import { ShowlistDocument, ShowlistQuery } from './graphql/showlist.graphql';

const CONTENTFUL_SPACE_ID = process.env.CONTENTFUL_SPACE_ID;
const CONTENTFUL_ACCESS_TOKEN = process.env.CONTENTFUL_ACCESS_TOKEN;
Expand All @@ -36,83 +26,6 @@ const fetchNavigationItems = async (): Promise<NavigationItem[]> => {
return navigationItems.navigationCollection.items[0].pagesCollection.items;
};

export enum Color {
Night = 'night',
Promote = 'promote',
}
export interface Show {
name?: string;
start?: string;
end?: string;
description?: null | string;
pictureUrl?: string | null;
hosts?: null | string;
producer?: null | string;
color?: Color | null;
}

export const parseQueryResultToShowlist = (data: ShowlistQuery) => {
const showsCollection =
data.programmeCollection.items[0].showsCollection.items;

const shows = showsCollection.map((item: any) => ({
name: item.name,
start: item.start,
end: item.end,
description: item.description,
pictureUrl: item.picture?.url || null,
hosts: item.hosts,
producer: item.producer,
color: item.color,
}));
return shows;
};

export const showsToGroups = (shows: Show[]) => {
const showsByDate = groupBy(
(day: any) => format(new Date(day.start), 'y.M.dd'),
shows
);
const weekKeys = generateWeekObj(showsByDate);
return { showsByDate, weekKeys };
};

const fetchShowlist = async (
showlistId: string | string[]
): Promise<{
showsByDate: Record<string, Show[]>;
weekKeys: Record<string, string[]>;
}> => {
const data = await fetchContent<ShowlistQuery>(ShowlistDocument, {
showlistId,
});

const shows = parseQueryResultToShowlist(data);
return showsToGroups(shows);
};

// Generate a nicely formatted object to use as keys.
const generateWeekObj = (showsByDate: Record<string, Show[]>) => {
const start = parse(head(keys(showsByDate)), 'y.M.dd', new Date());
const end = parse(last(keys(showsByDate)), 'y.M.dd', new Date());
const weeks = eachWeekOfInterval({ start, end }, { weekStartsOn: 1 });

const weekObj = weeks.reduce(
(acc: Record<string, string[]>, weekStart: Date) => {
const weekKey = getISOWeek(weekStart).toString();
const days = eachDayOfInterval({
start: weekStart,
end: addDays(new Date(weekStart), 6),
}).map((day: Date) => format(day, 'y.M.dd'));
acc[weekKey] = days;
return acc;
},
{}
);

return weekObj;
};

const fetchContent = async <T>(
query: DocumentNode,
variables?: any
Expand Down Expand Up @@ -141,4 +54,4 @@ const createApolloClient = () => {
});
};

export { fetchContent, fetchNavigationItems, fetchShowlist };
export { fetchContent, fetchNavigationItems };
3 changes: 2 additions & 1 deletion contentful/graphql/archivePage.graphql
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
query archivePage {
programmeCollection {
programmeCollection(order: [showOrder_DESC]) {
items {
id
name
coverImage {
url
}
shortDescription
showOrder
}
}
}
20 changes: 0 additions & 20 deletions contentful/graphql/showlist.graphql

This file was deleted.

2 changes: 1 addition & 1 deletion pages/arkisto/[showlistId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
fetchContent,
fetchNavigationItems,
NavigationItem,
Show,
} from 'contentful/client';
import { ShowlistPathsQuery } from 'contentful/graphql/showlistPaths.graphql';
import {
Expand All @@ -18,6 +17,7 @@ import { Showlist } from 'components/showlist';
import { BsArrowLeft } from 'react-icons/bs';
import Link from 'next/link';
import { fetchArchivedShowlist } from 'archiver/archive';
import { Show } from 'scripts/google/showlistHelpers';

interface ShowListPageProps {
name: string;
Expand Down
1 change: 1 addition & 0 deletions pages/arkisto/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface ShowList {
id?: string;
name?: string;
shortDescription?: string;
showOrder?: number;
coverImage?: {
url?: string;
};
Expand Down
5 changes: 2 additions & 3 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
fetchContent,
fetchNavigationItems,
NavigationItem,
Show,
} from 'contentful/client';
import { contentfulImageLoader } from 'contentful/contentfulImageLoader';
import RichText from 'components/richtext';
Expand All @@ -16,8 +15,8 @@ import Image from 'next/image';
import Calendar from 'components/calendar';
import Sponsors, { ISponsorData } from 'components/sponsors';
// import { Showlist } from 'components/showlist';
import Player from 'components/player';
import { fetchShowlist } from 'google/client';
// import Player from 'components/player';
// import { fetchShowlist } from 'scripts/google/client';

const isPlayerLive = process.env.NEXT_PUBLIC_PLAYER_MODE === 'live';

Expand Down
Loading

0 comments on commit 41fcd1e

Please sign in to comment.