about summary refs log tree commit diff stats
path: root/src/constants.rs
blob: 00919cee98e8b6a1c68a993306e354a0fa66810b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// yt - A fully featured command line YouTube client
//
// Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of Yt.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.

use std::{env::temp_dir, fs, path::PathBuf};

use anyhow::{Context, Result};

pub const HELP_STR: &str = include_str!("./select/selection_file/help.str");
pub const LOCAL_COMMENTS_LENGTH: usize = 1000;

// NOTE: KEEP THIS IN SYNC WITH THE `mpv_playback_speed` in `cli.rs` <2024-08-20>
pub const DEFAULT_MPV_PLAYBACK_SPEED: f64 = 2.7;
pub const DEFAULT_SUBTITLE_LANGS: &str = "en";

pub const CONCURRENT_DOWNLOADS: u32 = 5;
// We download to the temp dir to avoid taxing the disk
pub fn download_dir(create: bool) -> Result<PathBuf> {
    const DOWNLOAD_DIR: &str = "/tmp/yt";
    let dir = PathBuf::from(DOWNLOAD_DIR);

    if !dir.exists() && create {
        fs::create_dir_all(&dir).context("Failed to create the download directory")?
    }

    Ok(dir)
}

const PREFIX: &str = "yt";
fn get_runtime_path(name: &'static str) -> anyhow::Result<PathBuf> {
    let xdg_dirs = xdg::BaseDirectories::with_prefix(PREFIX)?;
    xdg_dirs
        .place_runtime_file(name)
        .with_context(|| format!("Failed to place runtime file: '{}'", name))
}
fn get_data_path(name: &'static str) -> anyhow::Result<PathBuf> {
    let xdg_dirs = xdg::BaseDirectories::with_prefix(PREFIX)?;
    xdg_dirs
        .place_data_file(name)
        .with_context(|| format!("Failed to place data file: '{}'", name))
}
fn get_config_path(name: &'static str) -> anyhow::Result<PathBuf> {
    let xdg_dirs = xdg::BaseDirectories::with_prefix(PREFIX)?;
    xdg_dirs
        .place_config_file(name)
        .with_context(|| format!("Failed to place config file: '{}'", name))
}

pub fn mpv_config_path() -> anyhow::Result<PathBuf> {
    const MPV_CONFIG_PATH: &str = "mpv.conf";
    get_config_path(MPV_CONFIG_PATH)
}
pub fn mpv_input_path() -> anyhow::Result<PathBuf> {
    const MPV_INPUT_CONFIG_PATH: &str = "mpv.input.conf";
    get_config_path(MPV_INPUT_CONFIG_PATH)
}

pub fn status_path() -> anyhow::Result<PathBuf> {
    const STATUS_PATH: &str = "running.info.json";
    get_runtime_path(STATUS_PATH)
}
pub fn last_select() -> anyhow::Result<PathBuf> {
    const LAST_SELECT: &str = "selected.yts";
    get_runtime_path(LAST_SELECT)
}

pub fn database() -> anyhow::Result<PathBuf> {
    const DATABASE: &str = "videos.sqlite";
    get_data_path(DATABASE)
}
pub fn subscriptions() -> anyhow::Result<PathBuf> {
    const SUBSCRIPTIONS: &str = "subscriptions.json";
    get_data_path(SUBSCRIPTIONS)
}

pub fn cache_path() -> PathBuf {
    let temp_dir = temp_dir();
    temp_dir.join("ytc")
}