Skip to content

Commit

Permalink
Add new file importSongsFromRcByIds.ts and run configuration for impo…
Browse files Browse the repository at this point in the history
…rting songs from resource center by IDs.
  • Loading branch information
ioanlucut committed Sep 19, 2023
1 parent cf1cab1 commit af921e7
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 79 additions & 0 deletions temp-runners/importSongsFromRcByIds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import fs from 'fs';
import path from 'path';
import * as process from 'process';
import fsExtra from 'fs-extra';
import dotenv from 'dotenv';
import recursive from 'recursive-readdir';
import pMap from 'p-map';
import { flatten } from 'lodash-es';
import { fileURLToPath } from 'url';
import { parse } from '../src/songParser.js';
import { print } from '../src/songPrinter.js';
import {
logFileWithLinkInConsole,
logProcessingFile,
NEW_LINE,
} from '../src/index.js';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

dotenv.config();

const IN_LYRICS_PARSER = '../bes-lyrics-parser';
const IN_LYRICS_PARSER_GENERATED_RC_SONGS = `${IN_LYRICS_PARSER}/out/resurse_crestine`;
const OUT_CANDIDATES_RC_DIR = './candidates/resurse_crestine_raw';

const RC_IDS_TO_PROCESS = fsExtra
.readFileSync(`${__dirname}/rc_ids_to_process.txt`)
.toString()
.split(NEW_LINE)
.filter(Boolean);

const RC_INDEX = JSON.parse(
fsExtra
.readFileSync(`${IN_LYRICS_PARSER_GENERATED_RC_SONGS}/index.json`)
.toString(),
);

const readFiles = async (dir: string) =>
(await recursive(dir)).map((filePath) => {
return {
contentAsString: fs.readFileSync(filePath).toString(),
fileName: path.basename(filePath),
filePath,
};
});

const runFor = async (songsDirs: string[]) => {
const allSongsInRepo = flatten(
await Promise.all(songsDirs.map(readFiles)),
).map(({ contentAsString }) => parse(contentAsString));
const allExistingRcIds = allSongsInRepo
.map(({ rcId }) => rcId)
.filter(Boolean);

await pMap(RC_IDS_TO_PROCESS, async (rcSongIdToImport) => {
const filePath = RC_INDEX[rcSongIdToImport] as string;
const contentAsString = fsExtra
.readFileSync(filePath.replace('./', `${IN_LYRICS_PARSER}/`))
.toString();
const fileName = path.basename(filePath);

const rcSongAST = parse(contentAsString);
logProcessingFile(fileName, `Import from RC with ID ${rcSongIdToImport}.`);
logFileWithLinkInConsole(filePath);

if (allExistingRcIds.includes(rcSongAST.rcId)) {
console.log(
`Skip processing the song with RC ID ${rcSongAST.rcId} as we have it in our system.`,
);
console.log(NEW_LINE);

return;
}

fs.writeFileSync(`${OUT_CANDIDATES_RC_DIR}/${fileName}`, print(rcSongAST));
});
};

await runFor([process.env.VERIFIED_DIR, process.env.CANDIDATES_DIR]);
1 change: 1 addition & 0 deletions temp-runners/rc_ids_to_process.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
212152

0 comments on commit af921e7

Please sign in to comment.