From 00128eb19a921c73e20102d7d70a9704b2dd51bc Mon Sep 17 00:00:00 2001 From: jy95 Date: Sat, 20 Jul 2024 00:58:21 +0200 Subject: [PATCH] feat: filter by playlist --- findPlaylists.mjs | 37 +++++++++++++++++++++++++++++++++++++ generate-playlist-csv.mjs | 33 ++++++++++++++++++++++++++++++--- package.json | 3 ++- 3 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 findPlaylists.mjs diff --git a/findPlaylists.mjs b/findPlaylists.mjs new file mode 100644 index 00000000..76811dd6 --- /dev/null +++ b/findPlaylists.mjs @@ -0,0 +1,37 @@ +import { readFile } from "fs/promises" + +// Path to the JSON file +const filePath = "src/app/api/games/games.json"; + +// Set of years to match +const yearsToMatch = new Set(["2023"]); + +async function findMatchingPlaylists() { + try { + // Read and parse the JSON file asynchronously + const data = await readFile(filePath, 'utf8'); + const games = JSON.parse(data); + + // Create a regex pattern to match the specified years + const yearPattern = new RegExp(`^(${Array.from(yearsToMatch).join('|')})`); + + // Filter games and extract matching playlist IDs + const matchingPlaylistIds = games + .filter(game => { + const availableAt = String(game.availableAt || ''); + const endAt = String(game.endAt || ''); + return yearPattern.test(availableAt) || yearPattern.test(endAt); + }) + .filter(game => game.playlistId !== undefined) + .map(game => game.playlistId); + + // Output the matching playlist IDs + console.log(JSON.stringify(matchingPlaylistIds, null, "\t")); + + } catch (err) { + console.error('Error:', err.message); // More specific error message + } +} + +// Call the async function +await findMatchingPlaylists(); diff --git a/generate-playlist-csv.mjs b/generate-playlist-csv.mjs index bc8377e5..36471d5d 100644 --- a/generate-playlist-csv.mjs +++ b/generate-playlist-csv.mjs @@ -10,9 +10,35 @@ import { google } from "googleapis"; import input from '@inquirer/input'; const scope = ["https://www.googleapis.com/auth/youtube.readonly"]; -const START_DATE = "2023-09-01"; -const END_DATE = "2024-03-31"; -const MAX_RESULTS = 50; +const START_DATE = "2023-01-01"; +const END_DATE = "2024-12-31"; +const MAX_RESULTS = 500; + +// See "Statistics for a specific playlist" +// https://developers.google.com/youtube/analytics/sample-requests +const generateFilters = (playListIds) => `playlist==${playListIds.join(",")}`; +/* +const FILTERS = [ + "PLRfhDHeBTBJ6sm2gbS9qtdovdSGygP-3f", + "PLRfhDHeBTBJ44bSU0cT2G0m4srnTO2gZY", + "PLRfhDHeBTBJ49XIAdTq1YJ5ozgKjaUM6E", + "PLRfhDHeBTBJ6L_uo8nMekfbJRE_ixUG5H", + "PLRfhDHeBTBJ4ZxkrGCk7a8C-6jXEj4HoT", + "PLRfhDHeBTBJ5hLNQ3UBKtImQLXkR1bChZ", + "PLRfhDHeBTBJ6zkB2GAtg_k6_J799AozQz", + "PLRfhDHeBTBJ6FOWzD8YONj2LoxCxPNJzO", + "PLRfhDHeBTBJ6aLNhmxhZdrI2dz0Bavlkh", + "PLRfhDHeBTBJ4GoSaMs-BqHumiPqUHU3OX", + "PLRfhDHeBTBJ6PHF-BH9dFug86rb877o2y", + "PLRfhDHeBTBJ47XmS2_upaFjIh_wKhN0XB", + "PLRfhDHeBTBJ7dIG6eD6CYJh8xu4j_eXyq", + "PLRfhDHeBTBJ5K0xPqtndkObR9g05hHw5h", + "PLRfhDHeBTBJ54lUeJrodUPuEOohHUdCJj", + "PLRfhDHeBTBJ72gRyZBPf23ZfxSlG5h8Em", + "PLRfhDHeBTBJ5BASLY5Kgof-1fgRxNVkOX", + "PLRfhDHeBTBJ5EYj5y6ByrLTuhqyKV4dpc" +]; +*/ // Load client secrets from a local file. // https://stackoverflow.com/a/52222827/6149867 @@ -61,6 +87,7 @@ const callApi = async (auth) => { sort: "-estimatedMinutesWatched", startDate: START_DATE, endDate: END_DATE + //filters: generateFilters(FILTERS) }); const rows = response.data.rows || []; diff --git a/package.json b/package.json index 49e81304..9eadde8f 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "sample-game": "npm run generate-responsive-images -- singleGame PLRfhDHeBTBJ4KQEvirhf9p1o_xuFysva8 covers cover.jpg", "generate-json-schemas": "node generate-json-schema.cjs", "generate-playlist-data-report": "node generate-playlists-stats.mjs", - "generate-playlist-csv": "node generate-playlist-csv.mjs" + "generate-playlist-csv": "node generate-playlist-csv.mjs", + "find-playlist-ids": "node findPlaylists.mjs" }, "dependencies": { "@emotion/react": "^11.11.4",