Skip to content

Commit

Permalink
Merge pull request #23 from jjmaldonis/transcript-audio-urls-using-de…
Browse files Browse the repository at this point in the history
…epgram

added ability to submit audio URL to Deepgram
  • Loading branch information
jjmaldonis authored Mar 15, 2023
2 parents 793c1c5 + b38eded commit 07de435
Show file tree
Hide file tree
Showing 10 changed files with 1,851 additions and 1,494 deletions.
44 changes: 19 additions & 25 deletions src/AudioNotesSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,17 @@ export class AudioNotesSettingsTab extends PluginSettingTab {
})
);
new Setting(containerEl)
.setName("Show Deepgram Logo")
.setDesc(
"Show the Deepgram logo on the bottom of the note. (requires restart)"
)
.addToggle((toggle: ToggleComponent) => {
toggle.onChange(async (value: boolean) => {
this.plugin.settings.showDeepgramLogo = value;
await this.plugin.saveSettings();
});
toggle.setValue(this.plugin.settings.showDeepgramLogo);
});
.setName("Deepgram Transcript Folder")
.setDesc("The folder your transcripts will be saved in when transcribing audio files.")
.addText((text) =>
text
.setPlaceholder("transcripts/")
.setValue(this.plugin.settings.DGTranscriptFolder)
.onChange(async (value) => {
this.plugin.settings.DGTranscriptFolder = value;
await this.plugin.saveSettings();
})
);

containerEl.createEl("hr");
containerEl.createDiv(
Expand Down Expand Up @@ -230,7 +230,7 @@ export interface StringifiedAudioNotesSettings {
audioNotesApiKey: string;
debugMode: boolean;
DGApiKey: string;
showDeepgramLogo: boolean;
DGTranscriptFolder: string;
}

const DEFAULT_SETTINGS: StringifiedAudioNotesSettings = {
Expand All @@ -241,7 +241,7 @@ const DEFAULT_SETTINGS: StringifiedAudioNotesSettings = {
audioNotesApiKey: "",
debugMode: false,
DGApiKey: "",
showDeepgramLogo: true,
DGTranscriptFolder: "transcripts/",
};

export class AudioNotesSettings {
Expand All @@ -253,7 +253,7 @@ export class AudioNotesSettings {
private _audioNotesApiKey: string,
private _debugMode: boolean,
private _DGApiKey: string,
private _showDeepgramLogo: boolean
private _DGTranscriptFolder: string,
) {}

static fromDefaultSettings(): AudioNotesSettings {
Expand All @@ -265,7 +265,7 @@ export class AudioNotesSettings {
DEFAULT_SETTINGS.audioNotesApiKey,
DEFAULT_SETTINGS.debugMode,
DEFAULT_SETTINGS.DGApiKey,
DEFAULT_SETTINGS.showDeepgramLogo
DEFAULT_SETTINGS.DGTranscriptFolder,
);
}

Expand Down Expand Up @@ -306,12 +306,6 @@ export class AudioNotesSettings {
if (data.DGApiKey !== null && data.DGApiKey !== undefined) {
settings.DGApiKey = data.DGApiKey!;
}
if (
data.showDeepgramLogo !== null &&
data.showDeepgramLogo !== undefined
) {
settings.showDeepgramLogo = data.showDeepgramLogo!;
}
return settings;
}

Expand Down Expand Up @@ -383,12 +377,12 @@ export class AudioNotesSettings {
this._DGApiKey = value;
}

get showDeepgramLogo(): boolean {
return this._showDeepgramLogo;
get DGTranscriptFolder(): string {
return this._DGTranscriptFolder;
}

set showDeepgramLogo(value: boolean) {
this._showDeepgramLogo = value;
set DGTranscriptFolder(value: string) {
this._DGTranscriptFolder = value;
}

async getInfoByApiKey(): Promise<ApiKeyInfo | undefined> {
Expand Down
82 changes: 82 additions & 0 deletions src/AudioNotesUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { MarkdownView, Notice, TFile, type App } from "obsidian";

export const createNewAudioNoteFile = async (app: App, audioFilename: string, transcriptFilename: string | undefined, newNoteFilename: string, title: string) => {
if (transcriptFilename === undefined) {
transcriptFilename = audioFilename;
const testTranscriptFilename = transcriptFilename.split(".").slice(0, transcriptFilename.split(".").length - 1).join(".") + ".json";
if (await app.vault.adapter.exists(testTranscriptFilename)) {
transcriptFilename = testTranscriptFilename;
}
}
const newNoteContents = `\`\`\`audio-note
audio: ${audioFilename}
transcript: ${transcriptFilename}
title: ${title}
\`\`\`
`;
const numberOfLines = 5;
app.vault.create(newNoteFilename, newNoteContents).then((newNote: TFile) => {
// Create the file and open it in the active leaf
const leaf = app.workspace.getLeaf(false);
leaf.openFile(newNote).then(() => {
const view = leaf.view;
if (view && view instanceof MarkdownView) {
view.editor.setCursor(numberOfLines);
}
});
}).catch((error: any) => {
new Notice(`Could not create new audio note file: ${newNoteFilename}`);
new Notice(`${error}`);
});
}

export const createAudioNoteTitleFromUrl = (url: string): string => {
const urlParts = url.split("/");
const lastPart = urlParts[urlParts.length - 1];
let title = lastPart.split("?")[0];
if (title.includes(".mp3")) {
title = title.replace(/.mp3/g, "");
} else if (title.includes(".m4b")) {
title = title.replace(/.m4b/g, "");
} else if (title.includes(".m4a")) {
title = title.replace(/.m4a/g, "");
}
return title;
}

export const createAudioNoteFilenameFromUrl = (url: string): string => {
const title = createAudioNoteTitleFromUrl(url);
const newNoteFilename = (title.replace(/[|&\/\\#,+()$~%'":*?<>{}]/g, "-")) + ".md";
return newNoteFilename;
}

export const createDeepgramQueryParams = (language: string): any => {
const DGoptions = {
language: language,
modelTier: "base",
punctuation: true,
numbers: true,
profanity: true,
keywords: "",
};
const options = {
language: DGoptions.language,
tier: DGoptions.modelTier,
punctuate: DGoptions.punctuation,
numbers: DGoptions.numbers,
profanity_filter: DGoptions.profanity,
keywords: DGoptions.keywords
.split(",")
.map((keyword: string) => keyword.trim()),
}
let optionsWithValue = Object.keys(options).filter(function (x) {
// @ts-ignore
return options[x] !== false && options[x] !== "";
});
let optionsToPass = {};
optionsWithValue.forEach((key) => {
// @ts-ignore
optionsToPass[key] = options[key];
});
return optionsToPass;
}
Loading

0 comments on commit 07de435

Please sign in to comment.