i fucking hate regex

This commit is contained in:
SadlyNotSappho 2023-10-20 11:50:13 -07:00
parent abf66e329e
commit 09a70b39df
4 changed files with 103 additions and 2 deletions

39
Cargo.lock generated
View File

@ -17,6 +17,15 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "aho-corasick"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
dependencies = [
"memchr",
]
[[package]]
name = "android-tzdata"
version = "0.1.1"
@ -673,6 +682,7 @@ name = "music-downloader"
version = "0.1.0"
dependencies = [
"clap",
"regex",
"serde",
"serde_json",
"spotify-rs",
@ -882,6 +892,35 @@ dependencies = [
"bitflags 1.3.2",
]
[[package]]
name = "regex"
version = "1.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
[[package]]
name = "reqwest"
version = "0.11.22"

View File

@ -7,6 +7,7 @@ edition = "2021"
[dependencies]
clap = { version = "4.4.6", features = ["derive"] }
regex = "1.10.2"
serde = { version = "1.0.189", features = ["derive"] }
serde_json = "1.0.107"
spotify-rs = "0.3.9"

View File

@ -1,6 +1,7 @@
use std::process;
use clap::Parser;
use music_downloader::structs::Spotify;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
@ -10,6 +11,9 @@ struct Cli {
#[arg(short, long, default_value = "youtube")]
download_from: String,
#[arg(short, long, default_value = "~/.config/music-downloader/config.json")]
config_file: String,
}
fn main() {
@ -25,10 +29,16 @@ fn main() {
process::exit(1);
}
// handle args
let args = Cli::parse();
println!("url: {}", args.url);
println!("download from: {}", args.download_from);
// TODO: init config
// init config
music_downloader::create_config(&args.config_file[..]);
// run???
let spot = Spotify::from_link(&args.url);
// TODO: get song info
// TODO: add a way to put data in config from cli
}

View File

@ -1,7 +1,58 @@
use serde::{Serialize, Deserialize};
use std::process;
use regex::Regex;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct Config {
pub client_id: String,
pub client_secret: String,
}
pub struct Spotify {
pub mtype: SpotifyType,
pub id: String,
}
pub enum SpotifyType {
Album, // album: https://open.spotify.com/album/5MfAxS5zz8MlfROjGQVXhy?si=2BBYm0iVR3m4UlQ3xE8RRg
Playlist, // playlist: https://open.spotify.com/playlist/1XsYplOyHG528tYcSaZOhT?si=ef60c1c83a744e29
Song, // song: https://open.spotify.com/track/08QaHlMPWuO5PUxjl61bXn?si=2bf95daa4f8347f0
Artist, // artist: https://open.spotify.com/artist/06HL4z0CvFAxyc27GXpf02?si=QzRParYJT72CfTdXk-5EDg
Episode, // episode: https://open.spotify.com/episode/122w4xeQAWeFcIz3KUP52M?si=390abae7a5ae4404
Show, // show: https://open.spotify.com/show/7bVFJvj8A2ZuYVs5lS992b?si=5c18c614eb3c420
}
impl Spotify {
pub fn from_link(url: &String) -> Spotify {
println!("{url}");
let re = match Regex::new(r#"(open.spotify.com)\/(?<type>album|playlist|track|artist|episode|show)\/(?<id>[^?]*)"#) {
Ok(val) => val,
Err(why) => {
eprintln!("structs::Spotify::from_link: Couldn't compile regex: {why}");
process::exit(1);
}
};
let caps = match re.captures(url) {
Some(val) => val,
None => {
eprintln!("structs::Spotify::from_link: No matches found.");
process::exit(1);
}
};
Spotify {
mtype: match &caps["type"] {
"album" => SpotifyType::Album,
"playlist" => SpotifyType::Playlist,
"track" => SpotifyType::Song,
"artist" => SpotifyType::Artist,
"episode" => SpotifyType::Episode,
"show" => SpotifyType::Show,
_ => panic!("structs::Spotify::from_link: unknown spotify type :D")
},
id: caps["id"].to_string()
}
}
}