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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
use anyhow::Context;
/*
This file is part of jellything (https://codeberg.org/metamuffin/jellything)
which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
Copyright (C) 2023 metamuffin <metamuffin.org>
*/
use clap::{Parser, Subcommand};
use jellycommon::ItemInfo;
use jellymatroska::read::EbmlReader;
use jellyremuxer::import::import_read;
use jellytools::tmdb::{tmdb_details, tmdb_image, tmdb_search};
use log::{info, warn};
use std::{
fs::File,
io::{stdin, Write},
path::PathBuf,
process::exit,
};
#[derive(Parser)]
struct Args {
#[arg(short = 'N', long)]
dry: bool,
#[clap(subcommand)]
action: Action,
}
#[derive(Subcommand)]
enum Action {
Create {
path: PathBuf,
title: Option<String>,
#[arg(short = 'T', long)]
tmdb: Option<String>,
#[arg(short = 'i', long)]
input: PathBuf,
},
Set {
#[arg(short = 'I', long)]
item: PathBuf,
#[arg(short, long)]
banner: Option<PathBuf>,
#[arg(short, long)]
title: Option<String>,
#[arg(short = 'D', long)]
description_head: Option<String>,
#[arg(short = 'd', long)]
description: Option<String>,
#[arg(short = 'c', long)]
clear_inputs: bool,
#[arg(short = 'i', long, num_args(0..))]
input: Vec<PathBuf>,
},
}
fn main() -> anyhow::Result<()> {
env_logger::builder()
.filter_level(log::LevelFilter::Info)
.parse_env("LOG")
.init();
let args = Args::parse();
match args.action {
Action::Create {
path,
title,
tmdb: id,
input,
} => {
let key = std::env::var("TMDB_API_KEY").context("tmdb api key required")?;
let id = if let Some(id) = id {
id.parse().unwrap()
} else {
let title = title.unwrap();
let results = tmdb_search(&title, &key)?;
info!("results:");
for (i, r) in results.results.iter().enumerate() {
info!(
"\t[{i}] {}: {} ({})",
r.id,
r.title,
r.overview.chars().take(100).collect::<String>()
);
}
let res_index = if results.results.len() != 1 {
stdin()
.lines()
.next()
.unwrap()
.unwrap()
.parse::<usize>()
.unwrap()
} else {
0
};
results.results[res_index].id
};
let details = tmdb_details(id, &key).context("fetching details")?;
let ident = make_ident(&details.title);
let path = path.join(&ident);
std::fs::create_dir_all(&path)?;
let poster = details
.poster_path
.map(|p| {
let pu = path.join("poster.jpeg");
let mut f = File::create(&pu)?;
tmdb_image(&p, &mut f)?;
Ok::<_, anyhow::Error>(pu)
})
.transpose()?;
let backdrop = details
.backdrop_path
.map(|p| {
let pu = path.join("backdrop.jpeg");
let mut f = File::create(&pu)?;
tmdb_image(&p, &mut f)?;
Ok::<_, anyhow::Error>(pu)
})
.transpose()?;
let mut iteminfo = ItemInfo {
poster,
backdrop,
description: details.overview,
description_head: details.tagline.unwrap_or_default(),
title: details.title,
duration: Default::default(),
tracks: Default::default(),
};
info!("{iteminfo:#?}");
info!("is this correct? [y/n]");
if stdin().lines().next().unwrap().unwrap() != "y" {
exit(0)
}
let source_path = path.join(format!("source.mkv"));
// std::fs::rename(&input, &source_path)?;
// std::os::unix::fs::symlink(&input, &source_path)?;
std::fs::copy(&input, &source_path)?;
let input = File::open(&source_path).unwrap();
let mut input = EbmlReader::new(input);
import_read(&source_path, &mut input, &mut iteminfo)?;
let k = serde_json::to_string_pretty(&iteminfo)?;
if args.dry {
println!("{k}")
} else {
let mut f = File::create(path.join(format!("{ident}.jelly")))?;
f.write_all(k.as_bytes())?;
}
Ok(())
}
Action::Set {
banner,
clear_inputs,
description,
description_head,
input,
item,
title,
} => {
let mut iteminfo: ItemInfo = match File::open(item.clone()) {
Ok(f) => serde_json::from_reader(f)?,
Err(e) => {
warn!("could not load item info: {e}");
warn!("using the default instead");
ItemInfo {
duration: 0.0,
poster: None,
backdrop: None,
description_head: "Lorem ipsum!".to_string(),
description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.".to_string(),
title: item.to_str().unwrap().to_string(),
tracks: Default::default(),
}
}
};
if let Some(title) = title {
iteminfo.title = title;
}
if let Some(banner) = banner {
iteminfo.poster = Some(banner);
}
if let Some(d) = description {
iteminfo.description = d;
}
if let Some(d) = description_head {
iteminfo.description_head = d;
}
if clear_inputs {
iteminfo.tracks = Default::default()
}
for input_path in input {
let input = File::open(input_path.clone()).unwrap();
let mut input = EbmlReader::new(input);
import_read(&input_path, &mut input, &mut iteminfo)?;
}
let k = serde_json::to_string_pretty(&iteminfo)?;
if args.dry {
println!("{k}")
} else {
let mut f = File::create(item)?;
f.write_all(k.as_bytes())?;
}
Ok(())
}
}
}
fn make_ident(s: &str) -> String {
let mut out = String::new();
for s in s.chars() {
match s {
'a'..='z' => out.push(s),
'A'..='Z' => out.push(s.to_ascii_lowercase()),
'-' | ' ' | '_' => out.push('-'),
_ => (),
}
}
out
}
|