Skip to content

Commit

Permalink
add log
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsekut committed Aug 14, 2024
1 parent 4870a2e commit 1a9e97c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as dotenv from 'dotenv';
import cp from 'cli-progress';

import { sleep } from 'app/utils/utils.js';
import { Path, getImageDirs, getImages, getPDFs } from 'app/utils/file.js';
import { pdfs2images } from 'app/pdf.js';
import * as Gyazo from './gyazo.js';
import { renderPage, saveJson } from 'app/renderScrapboxPage.js';
import { ProfilePath, createProfilePage } from 'app/profilePage.js';
import Bottleneck from 'bottleneck';

dotenv.config();

Expand All @@ -16,7 +18,6 @@ type Config = {
profile?: ProfilePath;
};

// TODO: log
export async function main(config: Config) {
const pdfPaths = await getPDFs(config.workspace);
await pdfs2images(pdfPaths, config.workspace);
Expand All @@ -30,11 +31,20 @@ async function dirs2Cosense(config: Config, dirPaths: Path[]) {
}

async function dir2Cosense(config: Config, dirPath: Path) {
const limiter = new Bottleneck({ maxConcurrent: 30, minTime: 1000 });
const progressBar = new cp.SingleBar({}, cp.Presets.shades_classic);

console.log(`imgs→cosense: start ${dirPath}\n`);
const images = await getImages(dirPath);
progressBar.start(images.length, 0);

const pages = await Promise.all(
images.map((img, index) => {
return generatePage(img, index, images.length, config);
}),
images.map((img, index) =>
limiter.schedule(() => {
progressBar.increment();
return generatePage(img, index, images.length, config);
}),
),
);

const pagesWithProfile = await (async () => {
Expand All @@ -45,6 +55,9 @@ async function dir2Cosense(config: Config, dirPath: Path) {
})();

await saveJson(`${dirPath}-ocr.json`, { pages: pagesWithProfile });

progressBar.stop();
console.log(`imgs→cosense: end ${dirPath}\n`);
}

const generatePage = async (
Expand Down
3 changes: 3 additions & 0 deletions src/pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export async function pdfs2images(
* - save to outDir
*/
async function pdf2images(pdf: Path, outDir: string): Promise<Path> {
console.log(`pdf→images: start ${pdf}\n`);

const { filename } = fileInfo(pdf);
const outPath = np.join(outDir, filename);
const imgs = await pdfToImages(pdf);
Expand All @@ -28,6 +30,7 @@ async function pdf2images(pdf: Path, outDir: string): Promise<Path> {
imgs.map((img, index) => saveFiles(img, `${outPath}/${index}.jpg`)),
);

console.log(`pdf→images: end ${pdf}\n`);
return outPath;
}

Expand Down

0 comments on commit 1a9e97c

Please sign in to comment.