support cookies, but with bugs

This commit is contained in:
Julian Freeman
2026-01-13 18:15:58 -04:00
parent 1554be25d2
commit bbcb10b3ca
6 changed files with 75 additions and 2 deletions

View File

@@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize};
use anyhow::{Result, anyhow}; use anyhow::{Result, anyhow};
use regex::Regex; use regex::Regex;
use crate::binary_manager; use crate::binary_manager;
use crate::storage;
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub struct VideoMetadata { pub struct VideoMetadata {
@@ -38,6 +39,7 @@ pub struct DownloadOptions {
pub quality: String, // e.g., "1080", "720", "best" pub quality: String, // e.g., "1080", "720", "best"
pub output_path: String, // Directory pub output_path: String, // Directory
pub output_format: String, // "original", "mp4", "webm", "mkv", "m4a", "aac", "opus", "vorbis", "wav", etc. pub output_format: String, // "original", "mp4", "webm", "mkv", "m4a", "aac", "opus", "vorbis", "wav", etc.
pub cookies_path: Option<String>,
} }
#[derive(Serialize, Clone, Debug)] #[derive(Serialize, Clone, Debug)]
@@ -67,6 +69,9 @@ pub async fn fetch_metadata(app: &AppHandle, url: &str, parse_mix_playlist: bool
let ytdlp_path = binary_manager::get_ytdlp_path(app)?; let ytdlp_path = binary_manager::get_ytdlp_path(app)?;
let qjs_path = binary_manager::get_qjs_path(app)?; // Get absolute path to quickjs let qjs_path = binary_manager::get_qjs_path(app)?; // Get absolute path to quickjs
// Load settings to check for cookies
let settings = storage::load_settings(app)?;
let mut cmd = Command::new(ytdlp_path); let mut cmd = Command::new(ytdlp_path);
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
cmd.creation_flags(0x08000000); cmd.creation_flags(0x08000000);
@@ -75,6 +80,12 @@ pub async fn fetch_metadata(app: &AppHandle, url: &str, parse_mix_playlist: bool
// Rust's Command automatically handles spaces in arguments, so we should NOT quote the path here. // Rust's Command automatically handles spaces in arguments, so we should NOT quote the path here.
cmd.arg("--js-runtimes").arg(format!("quickjs:{}", qjs_path.to_string_lossy())); cmd.arg("--js-runtimes").arg(format!("quickjs:{}", qjs_path.to_string_lossy()));
if let Some(cookies) = settings.cookies_path {
if !cookies.is_empty() {
cmd.arg("--cookies").arg(cookies);
}
}
cmd.arg("--dump-single-json") cmd.arg("--dump-single-json")
.arg("--flat-playlist") .arg("--flat-playlist")
.arg("--no-warnings"); .arg("--no-warnings");
@@ -185,6 +196,13 @@ pub async fn download_video(
args.push("--ffmpeg-location".to_string()); args.push("--ffmpeg-location".to_string());
args.push(ffmpeg_path.to_string_lossy().to_string()); args.push(ffmpeg_path.to_string_lossy().to_string());
if let Some(cookies) = &options.cookies_path {
if !cookies.is_empty() {
args.push("--cookies".to_string());
args.push(cookies.clone());
}
}
args.push(url); args.push(url);
// Output template // Output template

View File

@@ -9,6 +9,7 @@ use chrono::{DateTime, Utc};
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Settings { pub struct Settings {
pub download_path: String, pub download_path: String,
pub cookies_path: Option<String>,
pub theme: String, // 'light', 'dark', 'system' pub theme: String, // 'light', 'dark', 'system'
pub last_updated: Option<DateTime<Utc>>, pub last_updated: Option<DateTime<Utc>>,
} }
@@ -19,6 +20,7 @@ impl Default for Settings {
// but for default struct we can keep it empty or a placeholder. // but for default struct we can keep it empty or a placeholder.
Self { Self {
download_path: "".to_string(), download_path: "".to_string(),
cookies_path: None,
theme: "system".to_string(), theme: "system".to_string(),
last_updated: None, last_updated: None,
} }

View File

@@ -19,7 +19,8 @@ export const useAnalysisStore = defineStore('analysis', () => {
is_audio_only: false, is_audio_only: false,
quality: 'best', quality: 'best',
output_path: '', output_path: '',
output_format: 'original' output_format: 'original',
cookies_path: ''
}) })
function toggleEntry(id: string) { function toggleEntry(id: string) {

View File

@@ -5,6 +5,7 @@ import { ref } from 'vue'
export interface Settings { export interface Settings {
download_path: string download_path: string
cookies_path?: string
theme: 'light' | 'dark' | 'system' theme: 'light' | 'dark' | 'system'
last_updated: string | null last_updated: string | null
} }
@@ -12,6 +13,7 @@ export interface Settings {
export const useSettingsStore = defineStore('settings', () => { export const useSettingsStore = defineStore('settings', () => {
const settings = ref<Settings>({ const settings = ref<Settings>({
download_path: '', download_path: '',
cookies_path: '',
theme: 'system', theme: 'system',
last_updated: null last_updated: null
}) })

View File

@@ -214,6 +214,9 @@ async function startDownload() {
analysisStore.options.output_path = settingsStore.settings.download_path analysisStore.options.output_path = settingsStore.settings.download_path
} }
// Ensure cookies path is set from settings
analysisStore.options.cookies_path = settingsStore.settings.cookies_path || ''
try { try {
if (analysisStore.metadata.entries) { if (analysisStore.metadata.entries) {
// Playlist Download // Playlist Download

View File

@@ -3,7 +3,7 @@ import { ref } from 'vue'
import { useSettingsStore } from '../stores/settings' import { useSettingsStore } from '../stores/settings'
import { invoke } from '@tauri-apps/api/core' import { invoke } from '@tauri-apps/api/core'
import { open } from '@tauri-apps/plugin-dialog' import { open } from '@tauri-apps/plugin-dialog'
import { Folder, RefreshCw, Monitor, Sun, Moon, Terminal } from 'lucide-vue-next' import { Folder, RefreshCw, Monitor, Sun, Moon, Terminal, Download, FileText, X } from 'lucide-vue-next'
import { format } from 'date-fns' // Import format import { format } from 'date-fns' // Import format
const settingsStore = useSettingsStore() const settingsStore = useSettingsStore()
@@ -25,6 +25,25 @@ async function browsePath() {
} }
} }
async function browseCookies() {
const selected = await open({
multiple: false,
directory: false,
filters: [{ name: 'Text Files', extensions: ['txt', 'cookies'] }, { name: 'All Files', extensions: ['*'] }],
defaultPath: settingsStore.settings.cookies_path || undefined
})
if (selected) {
settingsStore.settings.cookies_path = selected as string
await settingsStore.save()
}
}
async function clearCookies() {
settingsStore.settings.cookies_path = ''
await settingsStore.save()
}
async function updateYtdlp() { async function updateYtdlp() {
updatingYtdlp.value = true updatingYtdlp.value = true
updateStatus.value = '正在更新 yt-dlp...' updateStatus.value = '正在更新 yt-dlp...'
@@ -98,6 +117,34 @@ function setTheme(theme: 'light' | 'dark' | 'system') {
</div> </div>
</section> </section>
<!-- Cookies Path -->
<section class="bg-white dark:bg-zinc-900 p-6 rounded-2xl shadow-sm border border-gray-200 dark:border-zinc-800">
<h2 class="text-lg font-bold mb-4 text-zinc-900 dark:text-white">Cookies 文件</h2>
<div class="flex gap-3">
<div class="flex-1 bg-gray-50 dark:bg-zinc-800 rounded-xl px-4 py-3 text-sm text-gray-600 dark:text-gray-300 font-mono border border-transparent focus-within:border-blue-500 transition-colors flex items-center justify-between group min-w-0">
<div class="truncate mr-2">
{{ settingsStore.settings.cookies_path || '未设置 (默认空)' }}
</div>
<button
v-if="settingsStore.settings.cookies_path"
@click="clearCookies"
class="text-gray-400 hover:text-red-500 opacity-0 group-hover:opacity-100 transition-all p-1 rounded-md hover:bg-gray-200 dark:hover:bg-zinc-700 shrink-0"
title="清除 Cookies 路径"
>
<X class="w-4 h-4" />
</button>
</div>
<button
@click="browseCookies"
class="bg-gray-100 hover:bg-gray-200 dark:bg-zinc-800 dark:hover:bg-zinc-700 text-zinc-900 dark:text-white px-4 py-3 rounded-xl font-medium transition-colors flex items-center gap-2 shrink-0"
>
<FileText class="w-5 h-5" />
选择
</button>
</div>
<p class="text-xs text-gray-400 mt-2">选填如果遇到需要登录的视频如会员专享请指定包含 cookies 的文本文件Netscape 格式</p>
</section>
<!-- Theme --> <!-- Theme -->
<section class="bg-white dark:bg-zinc-900 p-6 rounded-2xl shadow-sm border border-gray-200 dark:border-zinc-800"> <section class="bg-white dark:bg-zinc-900 p-6 rounded-2xl shadow-sm border border-gray-200 dark:border-zinc-800">
<h2 class="text-lg font-bold mb-4 text-zinc-900 dark:text-white">外观</h2> <h2 class="text-lg font-bold mb-4 text-zinc-900 dark:text-white">外观</h2>