Skip to content

Commit

Permalink
changed download method to new api from wave
Browse files Browse the repository at this point in the history
  • Loading branch information
0PandaDEV committed May 24, 2024
1 parent a3af007 commit cbdde84
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 17 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"devDependencies": {
"@tauri-apps/api": "2.0.0-beta.7",
"nuxt": "^3.11.2",
"sass": "1.77.1",
"sass": "1.77.2",
"vue": "3.4.27"
},
"dependencies": {
Expand All @@ -23,7 +23,7 @@
"@tauri-apps/plugin-global-shortcut": "^2.0.0-beta.3",
"@tauri-apps/plugin-os": "2.0.0-beta.3",
"@tauri-apps/plugin-sql": "2.0.0-beta.3",
"axios": "^1.6.8",
"axios": "^1.7.2",
"pinia": "^2.1.7",
"uuid": "^9.0.1",
"vue-router": "^4.3.2"
Expand Down
4 changes: 2 additions & 2 deletions pages/search.vue
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async function handleSongClick(song: MusicSearchResponseItem) {
}
try {
await invoke('download', { url: "https://youtube.com" + song.url, name: videoId + ".webm" });
await invoke('download', { id: videoId });
const response = await axios.get(song.thumbnail.replace("w120-h120", "w500-h500"), { responseType: 'arraybuffer' });
const data = new Uint8Array(response.data);
Expand All @@ -111,7 +111,7 @@ async function handleSongClick(song: MusicSearchResponseItem) {
await $music.setSong(videoId)
$music.play()
} catch (error) {
console.error('Error downloading video as MP3:', error);
console.error('Error downloading video as WAV:', error);
}
} catch (error) {
console.error("Failed to handle song click:", error);
Expand Down
14 changes: 11 additions & 3 deletions plugins/music.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,17 @@ export default defineNuxtPlugin(async (nuxtApp) => {
return URL.createObjectURL(new Blob([contents]));
},
async setSong(id: string) {
const contents = await readFile(`Vleer/Songs/${id}.webm`, {
baseDir: BaseDirectory.Audio,
});
let contents;
try {
contents = await readFile(`Vleer/Songs/${id}.wav`, {
baseDir: BaseDirectory.Audio,
});
} catch (error) {
console.warn(`.wav file not found for id ${id}, trying .webm`);
contents = await readFile(`Vleer/Songs/${id}.webm`, {
baseDir: BaseDirectory.Audio,
});
}
await musicStore.setSong(id, contents);
},
play() {
Expand Down
21 changes: 13 additions & 8 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
use anyhow::{anyhow, Result as AnyhowResult};
use reqwest::Client;
use rusty_ytdl::Video;
use std::path::PathBuf;
use std::fs;
use tauri::{AppHandle, async_runtime, Result as TauriResult};
use tauri_plugin_dialog::{DialogExt, MessageDialogKind};
use tauri_plugin_updater::UpdaterExt;
use tokio::time::Instant;
use tokio::task::JoinHandle;
use std::fs::File;
use std::io::copy;

#[tauri::command]
pub async fn download(url: String, name: String) -> TauriResult<()> {
let video = Video::new(url.clone()).map_err(|e| anyhow!(e.to_string()))?;
pub async fn download(id: String) -> TauriResult<()> {
let client = Client::new();
let response = client
.get(format!("https://api.wireway.ch/wave/audioStream/{}", id))
.send()
.await
.map_err(|e| anyhow!(e.to_string()))?;

let base_path = get_music_path();

let mut path = base_path.clone();
path.push("Songs");
path.push(&name);
path.push(id + ".wav");

video
.download(&path)
.await
.map_err(|e| anyhow!(e.to_string()))?;
let mut file = File::create(&path).map_err(|e| anyhow!(e.to_string()))?;
let content = response.bytes().await.map_err(|e| anyhow!(e.to_string()))?;
copy(&mut content.as_ref(), &mut file).map_err(|e| anyhow!(e.to_string()))?;

println!("Downloaded: {}", path.display());
Ok(())
Expand Down
5 changes: 3 additions & 2 deletions stores/music.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ export const useMusicStore = defineStore("musicStore", {
getSongsData(): SongsConfig {
return this.songsConfig;
},
async setSongFromBuffer(buffer: any) {
const blob = new Blob([buffer], { type: "audio/webm" });
async setSongFromBuffer(buffer: any, format: 'wav' | 'webm' = 'wav') {
const mimeType = format === 'wav' ? 'audio/wav' : 'audio/webm';
const blob = new Blob([buffer], { type: mimeType });
const url = URL.createObjectURL(blob);
this.player.audio.currentTime = 0;
this.player.audio.src = url;
Expand Down

0 comments on commit cbdde84

Please sign in to comment.