generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move context menu handler and universal podcast link creator
- Loading branch information
Showing
3 changed files
with
126 additions
and
129 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,64 @@ | ||
import { EventRef, Menu, TAbstractFile, TFile } from "obsidian"; | ||
import { get } from "svelte/store"; | ||
import { | ||
downloadedEpisodes, | ||
localFiles, | ||
playedEpisodes, | ||
currentEpisode, | ||
viewState, | ||
} from "./store"; | ||
import { LocalEpisode } from "./types/LocalEpisode"; | ||
import { ViewState } from "./types/ViewState"; | ||
import { createMediaUrlObjectFromFilePath } from "./utility/createMediaUrlObjectFromFilePath"; | ||
|
||
export default function getContextMenuHandler(): EventRef { | ||
return this.app.workspace.on( | ||
"file-menu", | ||
(menu: Menu, file: TAbstractFile) => { | ||
if (!(file instanceof TFile)) return; | ||
if (!file.extension.match(/mp3|mp4|wma|aac|wav|webm|aac|flac|m4a|/)) | ||
return; | ||
|
||
menu.addItem((item) => | ||
item | ||
.setIcon("play") | ||
.setTitle("Play with PodNotes") | ||
.onClick(async () => { | ||
const localEpisode: LocalEpisode = { | ||
title: file.basename, | ||
description: "", | ||
content: "", | ||
podcastName: "local file", | ||
url: app.fileManager.generateMarkdownLink(file, ""), | ||
streamUrl: await createMediaUrlObjectFromFilePath( | ||
file.path | ||
), | ||
episodeDate: new Date(file.stat.ctime), | ||
}; | ||
|
||
if ( | ||
!downloadedEpisodes.isEpisodeDownloaded( | ||
localEpisode | ||
) | ||
) { | ||
downloadedEpisodes.addEpisode( | ||
localEpisode, | ||
file.path, | ||
file.stat.size | ||
); | ||
|
||
localFiles.addEpisode(localEpisode); | ||
} | ||
|
||
// Fixes where the episode won't play if it has been played. | ||
if (get(playedEpisodes)[file.basename]?.finished) { | ||
playedEpisodes.markAsUnplayed(localEpisode); | ||
} | ||
|
||
currentEpisode.set(localEpisode); | ||
viewState.set(ViewState.Player); | ||
}) | ||
); | ||
} | ||
); | ||
} |
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,57 @@ | ||
import { requestUrl, Notice } from "obsidian"; | ||
import { IAPI } from "./API/IAPI"; | ||
import { queryiTunesPodcasts } from "./iTunesAPIConsumer"; | ||
|
||
export default async function getUniversalPodcastLink(api: IAPI) { | ||
const { title, itunesTitle, podcastName, feedUrl } = api.podcast; | ||
|
||
try { | ||
const iTunesResponse = await queryiTunesPodcasts( | ||
api.podcast.podcastName | ||
); | ||
const podcast = iTunesResponse.find( | ||
(pod) => pod.title === podcastName && pod.url === feedUrl | ||
); | ||
|
||
if (!podcast || !podcast.collectionId) { | ||
throw new Error("Failed to get podcast from iTunes."); | ||
} | ||
|
||
const podLinkUrl = `https://pod.link/${podcast.collectionId}.json?limit=1000`; | ||
const res = await requestUrl({ | ||
url: podLinkUrl, | ||
}); | ||
|
||
if (res.status !== 200) { | ||
throw new Error( | ||
`Failed to get response from pod.link: ${podLinkUrl}` | ||
); | ||
} | ||
|
||
const targetTitle = itunesTitle ?? title; | ||
|
||
const ep = res.json.episodes.find( | ||
(episode: { | ||
episodeId: string; | ||
title: string; | ||
[key: string]: string; | ||
}) => episode.title === targetTitle | ||
); | ||
if (!ep) { | ||
throw new Error( | ||
`Failed to find episode "${targetTitle}" on pod.link. URL: ${podLinkUrl}` | ||
); | ||
} | ||
|
||
window.navigator.clipboard.writeText( | ||
`https://pod.link/${podcast.collectionId}/episode/${ep.episodeId}` | ||
); | ||
|
||
new Notice("Universal episode link copied to clipboard."); | ||
} catch (error) { | ||
new Notice("Could not get podcast link."); | ||
console.error(error); | ||
|
||
return; | ||
} | ||
} |
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