Skip to content

Commit

Permalink
fix: allow scheduled sync and set a frequency in minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
sywhb committed May 17, 2023
1 parent 1a67d16 commit 8800d38
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
53 changes: 49 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ export default class OmnivorePlugin extends Plugin {

// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new OmnivoreSettingTab(this.app, this));

this.scheduleSync();
}

onunload() {}
Expand All @@ -106,6 +108,25 @@ export default class OmnivorePlugin extends Plugin {
await this.saveData(this.settings);
}

scheduleSync() {
const frequency = this.settings.frequency;
if (frequency > 0) {
// clear previous interval
if (this.settings.intervalId > 0) {
window.clearInterval(this.settings.intervalId);
}
// schedule new interval
const intervalId = window.setInterval(async () => {
await this.fetchOmnivore(false);
}, frequency * 60 * 1000);
// save new interval id
this.settings.intervalId = intervalId;
this.saveSettings();
// clear interval when plugin is unloaded
this.registerInterval(intervalId);
}
}

async downloadFileAsAttachment(article: Article): Promise<string> {
// download pdf from the URL to the attachment folder
const url = article.url;
Expand Down Expand Up @@ -155,7 +176,7 @@ export default class OmnivorePlugin extends Plugin {
return parseYaml(frontMatter[1]);
}

async fetchOmnivore() {
async fetchOmnivore(manualSync = true) {
const {
syncAt,
apiKey,
Expand All @@ -177,7 +198,6 @@ export default class OmnivorePlugin extends Plugin {

if (!apiKey) {
new Notice("Missing Omnivore api key");

return;
}

Expand All @@ -187,7 +207,7 @@ export default class OmnivorePlugin extends Plugin {
try {
console.log(`obsidian-omnivore starting sync since: '${syncAt}'`);

new Notice("🚀 Fetching articles ...");
manualSync && new Notice("🚀 Fetching articles ...");

// pre-parse template
preParseTemplate(template);
Expand Down Expand Up @@ -335,7 +355,7 @@ export default class OmnivorePlugin extends Plugin {
}
}

new Notice("🔖 Articles fetched");
manualSync && new Notice("🔖 Articles fetched");
this.settings.syncAt = DateTime.local().toFormat(DATE_FORMAT);
} catch (e) {
new Notice("Failed to fetch articles");
Expand All @@ -348,6 +368,7 @@ export default class OmnivorePlugin extends Plugin {

private async resetSyncingStateSetting() {
this.settings.syncing = false;
this.settings.intervalId = 0;
await this.saveSettings();
}
}
Expand Down Expand Up @@ -495,6 +516,30 @@ class OmnivoreSettingTab extends PluginSettingTab {
text.inputEl.setAttr("cols", 60);
});

new Setting(generalSettings)
.setName("Frequency")
.setDesc(
"Enter the frequency in minutes to sync with Omnivore automatically. 0 means manual sync"
)
.addText((text) =>
text
.setPlaceholder("Enter the frequency")
.setValue(this.plugin.settings.frequency.toString())
.onChange(async (value) => {
// validate frequency
const frequency = parseInt(value);
if (isNaN(frequency)) {
new Notice("Frequency must be a positive integer");
return;
}
// save frequency
this.plugin.settings.frequency = frequency;
await this.plugin.saveSettings();

this.plugin.scheduleSync();
})
);

new Setting(generalSettings)
.setName("Folder")
.setDesc(
Expand Down
4 changes: 4 additions & 0 deletions src/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export const DEFAULT_SETTINGS: OmnivoreSettings = {
attachmentFolder: "Omnivore/attachments",
version: "0.0.0",
isSingleFile: false,
frequency: 0,
intervalId: 0,
};

export enum Filter {
Expand Down Expand Up @@ -47,4 +49,6 @@ export interface OmnivoreSettings {
attachmentFolder: string;
version: string;
isSingleFile: boolean;
frequency: number;
intervalId: number;
}

0 comments on commit 8800d38

Please sign in to comment.