forked from mhujer/ankiai
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stagger start of initial calls. Use dotenv-extended so it throws if e…
…nv vars are missing
- Loading branch information
Showing
9 changed files
with
110 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
OPENAI_API_KEY= | ||
OPENAI_MODEL='gpt-3.5-turbo' | ||
ANKI_DECK= | ||
ANKI_LANGUAGE= | ||
MAX_CONCURRENCY=5 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,37 @@ | ||
import * as dotenv from 'dotenv'; | ||
import { logger } from './utils/logger'; | ||
import { fetchNotesFromAnki } from './anki'; | ||
import { concurrentProcessor } from './utils/concurrent-processor'; | ||
import { primeProcessNote } from './utils/process-note'; | ||
|
||
const MAX_NOTES_PROCESSED_AT_ONCE = 4; | ||
const MAX_NOTES_PROCESSED_AT_ONE_RUN = 400; | ||
import * as dotenv from 'dotenv-extended'; | ||
|
||
let notesProcessedCount = 0; | ||
// Load environment variables from .env file | ||
dotenv.load({ | ||
path: '.env.local', | ||
errorOnMissing: true, | ||
errorOnExtra: true, | ||
}); | ||
|
||
// const testApiWithoutUpdating = async () => { | ||
// logger.info( | ||
// await Promise.all([ | ||
// fetchExample('Mandarin', { | ||
// noteId: 1, | ||
// text: '阳性', | ||
// definitions: 'none', | ||
// }), | ||
// // fetchExample('Mandarin', { | ||
// // noteId: 2, | ||
// // text: '不', | ||
// // definitions: 'none', | ||
// // }), | ||
// // fetchExample('Mandarin', { | ||
// // noteId: 3, | ||
// // text: '就', | ||
// // definitions: 'none', | ||
// // }), | ||
// // fetchExample('Mandarin', { | ||
// // noteId: 4, | ||
// // text: '下', | ||
// // definitions: 'none', | ||
// // }), | ||
// // fetchExample('Mandarin', { | ||
// // noteId: 5, | ||
// // text: '又', | ||
// // definitions: 'none', | ||
// // }), | ||
// // fetchExample('Mandarin', { | ||
// // noteId: 6, | ||
// // text: '喂', | ||
// // definitions: 'none', | ||
// // }), | ||
// ]), | ||
// ); | ||
// return; | ||
// }; | ||
// Define types for your environment variables | ||
interface EnvVariables { | ||
ANKI_DECK: string; | ||
ANKI_LANGUAGE: string; | ||
MAX_CONCURRENCY: string; | ||
} | ||
|
||
void (async function () { | ||
dotenv.config({ path: '.env.local' }); | ||
// return testApiWithoutUpdating(); | ||
const { ANKI_DECK, ANKI_LANGUAGE, MAX_CONCURRENCY }: EnvVariables = process.env as unknown as EnvVariables; | ||
|
||
const ankiDeck = process.env['ANKI_DECK']; | ||
if (ankiDeck === undefined) { | ||
throw new Error('ANKI_DECK is not configured in env'); | ||
} | ||
|
||
const ankiLanguage = process.env['ANKI_LANGUAGE']; | ||
if (ankiLanguage === undefined) { | ||
throw new Error('ANKI_LANGUAGE is not configured in env'); | ||
} | ||
|
||
logger.info(`Processing cards from Anki deck "${ankiDeck}" in language "${ankiLanguage}"...`); | ||
(async function (): Promise<void> { | ||
logger.info(`Processing cards from Anki deck "${ANKI_DECK}" in language "${ANKI_LANGUAGE}"...`); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition,no-constant-condition | ||
while (true) { | ||
const notes = await fetchNotesFromAnki(ankiDeck); | ||
const notes = await fetchNotesFromAnki(ANKI_DECK); | ||
logger.info(`Found ${notes.length} notes eligible for fetching`); | ||
if (notes.length === 0) { | ||
break; | ||
} | ||
|
||
await concurrentProcessor(notes, MAX_NOTES_PROCESSED_AT_ONCE, primeProcessNote(ankiLanguage)); | ||
|
||
// const notesForProcessing = notes.slice(0, MAX_NOTES_PROCESSED_AT_ONCE); | ||
// logger.info(`Processing batch of ${notesForProcessing.length} notes.`); | ||
|
||
// await Promise.all(notesForProcessing.map((note) => processNote(ankiLanguage, note))); | ||
|
||
// notesProcessedCount += notesForProcessing.length; | ||
// if (notesProcessedCount >= MAX_NOTES_PROCESSED_AT_ONE_RUN) { | ||
// logger.info(`Processed ${notesProcessedCount} notes, quiting.`); | ||
// console.dir(notes.map((note) => note.text).join(', ')); | ||
// break; | ||
// } | ||
await concurrentProcessor(notes, parseInt(MAX_CONCURRENCY as string), primeProcessNote(ANKI_LANGUAGE)); | ||
} | ||
})(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.