Skip to content

Commit

Permalink
feat: filter by playlist
Browse files Browse the repository at this point in the history
  • Loading branch information
jy95 committed Jul 19, 2024
1 parent 4b6b8f3 commit 00128eb
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
37 changes: 37 additions & 0 deletions findPlaylists.mjs
Original file line number Diff line number Diff line change
@@ -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();
33 changes: 30 additions & 3 deletions generate-playlist-csv.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -61,6 +87,7 @@ const callApi = async (auth) => {
sort: "-estimatedMinutesWatched",
startDate: START_DATE,
endDate: END_DATE
//filters: generateFilters(FILTERS)
});

const rows = response.data.rows || [];
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 00128eb

Please sign in to comment.