Skip to content

Commit

Permalink
- better windows support
Browse files Browse the repository at this point in the history
- rename now supports paths (so you can set /path/to/music/%(album)/%(song) as a template)
- yt-dlp-args option are now used as extra arguments instead of replacing the default arguments
- module config is done under module.name in the toml now
FriederHannenheim committed Jul 8, 2023
1 parent 3df6b94 commit 200e2af
Showing 10 changed files with 223 additions and 73 deletions.
96 changes: 86 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -23,3 +23,4 @@ infer = "0.13.0"
log = "0.4.17"
fs_extra = "1.3.0"
simplelog = "0.12.1"
home = "0.5.5"
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -3,8 +3,6 @@ Musicfetch is a tool for downloading music from Youtube and other platforms. It

![GitHub](https://img.shields.io/github/license/FriederHannenheim/Musicfetch?logo=gnu)

This is the code for the rework. Versions < v1.0 can be found on the branch `old`

## Supported Song metadata:
- Title
- Album Title
@@ -18,9 +16,22 @@ This is the code for the rework. Versions < v1.0 can be found on the branch `old
Select the nightly Rust toolchain and enter `cargo build --release`. To install musicfetch enter `cargo install --path .`

## Usage
This is the branch for musicfetch >= v1.0. v1.0 was rebuilt from the ground up with massive changes under the hood. Right now it is in it's alpha stage, the core functionality is there but there are still things to be done to make it actually usable.

You can try it now by placing the `config_example.toml` under `/etc/musicfetch.toml` and invoking musicfetch with a link to an Album on youtube.
```
musicfetch
Usage:
musicfetch <url>...
musicfetch (-c | --cover_url) <cover_url> <url>
musicfetch (-o | --output_dir) <output_dir> <url>
musicfetch -? | -h | --help
Options:
-? -h --help Show this help
-v --version Print version and exit
-c --cover_url Specify the url of the cover that should be added to the songs
-o --output_dir Specify the directory the songs should be downloaded to
-C --config Use the config with this name
```

### UI
The UI for entering Metadata has been designed to need as few key presses as possible to get to where you want.
@@ -30,6 +41,9 @@ On the left you can select the song you want to edit. In front of the song title

When you change the track number of a song, they will be reordered in the selectview to reflect that change. Use Shift+Up or Shift+Down to increase or decrease the track number for a song. Alternatively, use the number keys 1-9 to set it directly.

### Configuration
Under $XDG_CONFIG_HOME/musicfetch or $HOME/.config/musicfetch you can find and place .toml files with configuration. `default.toml` is the default and is documented well. Look in there for a list of options and explainations.

## Dependencies
- [yt-dlp](https://github.com/yt-dlp/yt-dlp)

45 changes: 45 additions & 0 deletions config/default.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

# This specifies the stages and modules musicfetch will run.
# Stages are run in parallel so be careful not to introduce any
# race conditions by including too many modules in one stage.
[stages]
stage1 = ["fetch_song_info"]
stage2 = ["infocopy"]
stage3 = [
"albumui",
"trackcounter"
]
stage4 = [
"tagui",
"download"
]
stage5 = [
"tag_files",
"albumcover"
]
stage6 = ["rename"]


# Infocopy copies values from the yt-dlp json to the songinfo
[module.infocopy]
title = "track"
album = "album"
artist = "artist"

[module.rename]
# Template for filepaths. Can include paths to folders. These should be absolute, environment variables and '~' are not parsed.
# Variables can be entered like this %(name)
# Available variables:
# %(title) - Song Title
# %(album) - Album Name
# %(artist) - Artist Name
# %(genre) - Genre
# %(year) - Release year
# %(track_no) - Track Number
# %(total_tracks) - Total Tracks in Album
template = "%(title).%(ext)"

[module.download]
# Extra arguments to give to yt-dlp. For example ['--audio-format', 'mp3'] if you want to download everything as mp3
yt_dlp_args = []

28 changes: 0 additions & 28 deletions config_example.toml

This file was deleted.

43 changes: 37 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,50 @@
use std::{env, path::PathBuf, fs};
use std::{env, path::PathBuf, fs::{self, create_dir_all, File}, io::Write};

use home::home_dir;
use log::info;
use serde_json::Value;
use anyhow::{Result, bail, Context};


const DEFAULT_CONFIG: &'static[u8] = include_bytes!("../config/default.toml");

pub fn get_config(name: &str) -> Result<Value> {
let Some(dir) = get_config_dir() else {
bail!("Failed finding configuration directory");
let dir = match get_config_dir() {
Some(dir) => dir,
None => {
eprintln!("Failed to find musicfetch config dir. Creating default config...");
create_default_config()?
}
};
get_config_by_name(name, dir)
}

fn create_default_config() -> Result<PathBuf> {
let mut config_dir = match env::var("XDG_CONFIG_HOME") {
Ok(dir) => PathBuf::from(dir),
Err(_) => {
match home_dir() {
Some(mut dir) => {
dir.push(".config");
dir
},
None => bail!("Failed to find config directory")
}
}
};
config_dir.push("musicfetch");

create_dir_all(&config_dir)?;

let mut config_path = config_dir.clone();
config_path.push("default.toml");

let mut config_file = File::create(&config_path)?;

config_file.write(DEFAULT_CONFIG)?;

Ok(config_dir)
}

fn get_config_by_name(name: &str, dir: PathBuf) -> Result<Value> {
let mut file_path = dir;
file_path.push(format!("{}.toml", name));
@@ -41,8 +73,7 @@ fn get_user_config_dir() -> Option<PathBuf> {
}
}

if let Ok(home_dir) = env::var("HOME") {
let mut path = PathBuf::from(home_dir);
if let Some(mut path) = home_dir() {
path.push(".config");
path.push("musicfetch");

9 changes: 6 additions & 3 deletions src/modules/download.rs
Original file line number Diff line number Diff line change
@@ -84,7 +84,8 @@ fn download(yt_dlp_json: &str, args: &Vec<String>) -> Result<()> {
}

fn get_yt_dlp_args(module_config: Option<Value>) -> Vec<String> {
match module_config.and_then(|v| v["yt_dlp_args"].as_array().map(|v| v.to_owned())) {
let mut args = YT_DLP_ARGS.map(|s| s.to_owned()).to_vec();
let mut extra_args = match module_config.and_then(|v| v["yt_dlp_args"].as_array().map(|v| v.to_owned())) {
Some(v) => v
.into_iter()
.map(|v| {
@@ -93,8 +94,10 @@ fn get_yt_dlp_args(module_config: Option<Value>) -> Vec<String> {
.to_owned()
})
.collect(),
None => YT_DLP_ARGS.map(|s| s.to_owned()).to_vec(),
}
None => vec![],
};
args.append(&mut extra_args);
args
}

fn get_downloaded_filename(yt_dlp_json: &str, args: &Vec<String>) -> Result<String> {
Loading

0 comments on commit 200e2af

Please sign in to comment.