-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.rs
605 lines (501 loc) · 19.3 KB
/
main.rs
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
use std::collections::HashSet;
use std::env::temp_dir;
use std::path::PathBuf;
use std::string::ToString;
use std::sync::Arc;
use clap::{arg, Parser, Subcommand};
use console::Term;
use dialoguer::Confirm;
use indicatif::{MultiProgress, ProgressBar, ProgressDrawTarget, ProgressStyle};
use lazy_static::lazy_static;
use redacted::util::create_description;
use regex::Regex;
use strum::IntoEnumIterator;
use tags::util::valid_tags;
use tokio::fs::File;
use tokio::io::AsyncReadExt;
use tokio::task::JoinSet;
use transcode::transcode::transcode_release;
use crate::config::models::RedOxideConfig;
use crate::fs::util::count_files_with_extension;
use crate::redacted::api::client::RedactedApi;
use crate::redacted::api::constants::TRACKER_URL;
use crate::redacted::models::{Category, Media, ReleaseType};
use crate::redacted::upload::TorrentUploadData;
use crate::redacted::util::perma_link;
use crate::transcode::util::copy_other_allowed_files;
mod config;
mod fs;
mod imdl;
mod redacted;
mod tags;
mod transcode;
pub mod built_info {
// The file has been placed there by the build script.
include!(concat!(env!("OUT_DIR"), "/built.rs"));
}
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand, Debug)]
enum Commands {
/// Transcode FLACs to other co-existing formats
Transcode(TranscodeCommand),
}
#[derive(Parser, Debug)]
struct TranscodeCommand {
/// If debug logs should be shown
#[arg(long, default_value = "false")]
debug: bool,
/// If the upload should be done automatically
#[arg(long, short, default_value = "false")]
automatic_upload: bool,
/// The number of THREADS to use, defaults to the amount of cores available
#[arg(long, short)]
threads: Option<u32>,
/// If multiple formats should be transcoded in parallel
#[arg(long, default_value = "false")]
transcode_in_parallel: bool,
/// The Api key from Redacted to use there API with
#[arg(long)]
api_key: Option<String>,
/// The path to the directory where the downloaded torrents are stored
#[arg(long)]
content_directory: PathBuf,
/// The path to the directory where the transcoded torrents should be stored
#[arg(long)]
transcode_directory: PathBuf,
/// The path to the directory where the torrents should be stored
#[arg(long)]
torrent_directory: PathBuf,
/// The path to the config file
#[arg(long, short)]
config_file: Option<PathBuf>,
/// If the transcode should be moved to the content directory, useful when you want to start seeding right after you upload
#[arg(long, default_value = "false")]
move_transcode_to_content: bool,
/// If the hash check of the original torrent should be skipped, defaults to false, not recommended and if enabled done at own risk!
#[arg(long, default_value = "false")]
skip_hash_check: bool,
/// If this is a dry run, no files will be uploaded to Redacted
#[arg(long, short, default_value = "false")]
dry_run: bool,
/// The url of torrents to transcode
urls: Vec<String>,
}
const SUCCESS: &str = "[✅]";
const WARNING: &str = "[⚠️]";
const ERROR: &str = "[❌]";
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
match cli.command {
Commands::Transcode(cmd) => transcode(cmd).await?,
}
Ok(())
}
async fn transcode(mut cmd: TranscodeCommand) -> anyhow::Result<()> {
let term = Term::stdout();
if let Some(config_path) = &cmd.config_file {
let mut file = File::open(config_path).await?;
let mut contents = vec![];
file.read_to_end(&mut contents).await?;
let config: RedOxideConfig = serde_json::from_slice(&*contents)?;
if cmd.api_key.is_none() {
cmd.api_key = Some(config.api_key);
}
}
if cmd.api_key.is_none() {
term.write_line(&format!(
"{} You have to specify API key either as argument or in the config file",
ERROR
))?;
return Ok(());
}
if cmd.threads.is_none() {
cmd.threads = Some(num_cpus::get() as u32);
}
let mut api = RedactedApi::new(cmd.api_key.clone().unwrap());
let index_response = api.index().await?.response;
term.write_line(&format!(
"{} Logged in as {} on the Redacted API",
SUCCESS, index_response.username
))?;
for url in cmd.urls.clone() {
handle_url(
url.as_str(),
&term,
&mut api,
&mut cmd,
index_response.passkey.clone(),
)
.await?;
}
Ok(())
}
async fn handle_url(
url: &str,
term: &Term,
api: &mut RedactedApi,
cmd: &mut TranscodeCommand,
passkey: String,
) -> anyhow::Result<()> {
lazy_static! {
static ref RE: Regex = regex::Regex::new(
r"(https://|http://)?redacted\.ch/torrents\.php\?id=(\d+)&torrentid=(\d+)"
)
.unwrap();
}
let captures = RE.captures(url).unwrap();
let group_id = captures.get(2).unwrap().as_str().parse::<i64>().unwrap();
let torrent_id = captures.get(3).unwrap().as_str().parse::<i64>().unwrap();
term.write_line(&format!(
"{} Got torrent {} from group {}",
SUCCESS, torrent_id, group_id
))?;
let group_info = api.get_torrent_group(group_id).await?;
let group_torrents = group_info.response.torrents;
let group = group_info.response.group;
let torrent_opt = group_torrents
.iter()
.find(|torrent| torrent.id == torrent_id);
let torrent = match torrent_opt {
None => {
term.write_line(&format!(
"{} Could not find torrent {} in group {}, this shouldn't happen...",
ERROR, torrent_id, group_id
))?;
return Ok(());
}
Some(t) => t,
};
let mut existing_formats = HashSet::new();
group_torrents
.iter()
.filter(|t| t.remaster_title == torrent.remaster_title
&& t.remaster_record_label == torrent.remaster_record_label
&& t.media == torrent.media
&& t.remaster_catalogue_number == torrent.remaster_catalogue_number)
.for_each(|t| {
match t.format.as_str() {
"FLAC" => match t.encoding.as_str() {
"Lossless" => {
existing_formats.insert(ReleaseType::Flac);
}
"24bit Lossless" => {
existing_formats.insert(ReleaseType::Flac24);
},
_ => {
term.write_line(&format!(
"{} Unknown encoding {} for torrent {} in group {}, this shouldn't happen...",
ERROR, t.encoding, t.id, group_id
)).unwrap();
}
},
"MP3" => {
match t.encoding.as_str() {
"320" => {
existing_formats.insert(ReleaseType::Mp3320);
}
"V0 (VBR)" => {
existing_formats.insert(ReleaseType::Mp3V0);
}
_ => {
term.write_line(&format!(
"{} Unknown encoding {} for torrent {} in group {}, this shouldn't happen...",
ERROR, t.encoding, t.id, group_id
)).unwrap();
}
}
}
_ => {
term.write_line(&format!(
"{} Unknown format {} for torrent {} in group {}, this shouldn't happen...",
ERROR, t.format, t.id, group_id
)).unwrap();
}
}
});
if !existing_formats.contains(&ReleaseType::Flac)
&& !existing_formats.contains(&ReleaseType::Flac24)
{
term.write_line(&format!(
"{} Torrent {} in group {} has no FLAC base to transcode from... skipping",
WARNING, torrent_id, group_id
))?;
return Ok(());
}
let mut transcode_formats = Vec::new();
ReleaseType::iter().for_each(|release_type| {
if !existing_formats.contains(&release_type) && release_type != ReleaseType::Flac24 {
transcode_formats.push(release_type);
}
});
if transcode_formats.is_empty() {
term.write_line(&format!(
"{} Torrent {} in group {} has all formats already... skipping",
WARNING, torrent_id, group_id
))?;
return Ok(());
}
term.write_line(&format!(
"{} Found missing format(s) {} for torrent {} in group {}",
SUCCESS,
transcode_formats
.iter()
.map(|f| f.to_string())
.fold(String::new(), |acc, s| acc + &s + ","),
torrent_id,
group_id
))?;
let artist = if group.music_info.artists.len() > 1 {
"Various Artists".to_string()
} else {
group.music_info.artists[0].name.clone()
};
let mut year = torrent.remaster_year;
// Fixes edge case where remaster year is 0 (likely unintentional)
if year == 0 {
year = group.year;
}
let group_name = group.name.replace(":", "");
let base_name = if torrent.remaster_title.len() > 1 {
format!(
"{} - {} ({}) [{}]",
artist, group_name, torrent.remaster_title, year
)
} else {
format!("{} - {} [{}]", artist, group_name, year)
};
let flac_path = cmd.content_directory.join(torrent.file_path.clone());
if transcode::util::is_multichannel(&flac_path).await? {
term.write_line(&format!(
"{} Torrent {} in group {} is a multichannel release which is unsupported, skipping",
WARNING, torrent_id, group_id
))?;
return Ok(());
}
let media = Media::from(&*torrent.media);
let (valid, invalid_track_number_vinyl) = valid_tags(&flac_path, &media).await?;
if !valid && invalid_track_number_vinyl {
term.write_line(&format!(
"{} Release is Vinyl and has either no set track number or in a non standard format (e.g. A1, A2 etc), you will be prompted once transcode is done to manually check & adjust the transcode tags as needed!", WARNING
))?;
cmd.automatic_upload = false;
} else if !valid {
term.write_line(&format!(
"{} Torrent {} in group {} has FLAC files with invalid tags, skipping...\n You might be able to trump it.",
WARNING, torrent_id, group_id
))?;
return Ok(());
}
if !cmd.skip_hash_check {
let downloaded_torrent = api.download_torrent(torrent.id).await?;
let mut tmp = temp_dir();
tmp.push(format!("red_oxide-torrent-{}", torrent_id));
tokio::fs::write(&tmp, downloaded_torrent).await?;
let result = imdl::hash::verify_torrent_hash(
flac_path.as_path().to_str().unwrap(),
tmp.to_str().unwrap(),
)
.await?;
if result {
term.write_line(&format!(
"{} Local file torrent hash check succeeded for torrent {} in group {}",
SUCCESS, torrent_id, group_id
))?;
} else {
term.write_line(&format!(
"{} Local file torrent hash check failed for torrent {} in group {}",
ERROR, torrent_id, group_id
))?;
return Ok(());
}
tokio::fs::remove_file(&tmp).await?;
}
if transcode::util::is_multichannel(&flac_path).await? {
term.write_line(&format!(
"{} Torrent {} in group {} is a multichannel release which is unsupported, skipping",
WARNING, torrent_id, group_id
))?;
return Ok(());
}
let m = MultiProgress::with_draw_target(ProgressDrawTarget::stdout());
let sty = ProgressStyle::with_template(
"[{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {msg}",
)
.unwrap()
.progress_chars("##-");
let mut join_set = JoinSet::new();
let files = count_files_with_extension(&flac_path, ".flac").await?;
m.println("[➡️] Transcoding...").unwrap();
for format in &transcode_formats {
let pb = m.add(ProgressBar::new(files));
pb.set_style(sty.clone());
let transcode_format_str = match format {
ReleaseType::Flac24 => "FLAC 24bit",
ReleaseType::Flac => "FLAC",
ReleaseType::Mp3320 => "MP3 - 320",
ReleaseType::Mp3V0 => "MP3 - V0",
};
let transcode_release_name = format!(
"{} ({} - {})",
base_name,
torrent.media.to_uppercase(),
transcode_format_str
);
let flac_path_clone = flac_path.clone();
let threads = cmd.threads.clone().unwrap();
let torrent_id_clone = torrent_id.clone();
let term = Arc::new(term.clone());
let mut output_dir = cmd.transcode_directory.clone();
let format = format.clone();
join_set.spawn(tokio::spawn(async move {
let (folder_path, command) = transcode_release(
&flac_path_clone,
&mut output_dir,
transcode_release_name.clone(),
format,
threads,
term,
torrent_id_clone,
pb,
)
.await?;
let transcode_folder_path = output_dir.join(&folder_path);
copy_other_allowed_files(&flac_path_clone, &flac_path_clone, &transcode_folder_path)
.await?;
return Ok::<(PathBuf, ReleaseType, String), anyhow::Error>((
folder_path,
format,
command,
));
}));
}
let mut path_format_command_triple = Vec::new();
while let Some(res) = join_set.join_next().await {
let transcode_folder = res???;
path_format_command_triple.push(transcode_folder);
}
m.println(format!("{} Transcoding Done!", SUCCESS))?;
m.clear()?;
if invalid_track_number_vinyl {
let mut prompt = Confirm::new();
prompt
.with_prompt(format!("{} Please check tags of trancoded media and adjust as needed (release is vinyl and has either no track number or in an non standard format e.g. A1, A2 etc which the audiotags library used can't parse), continue?", WARNING))
.default(true);
prompt.interact()?;
}
for (path, format, command) in &path_format_command_triple {
let release_name = path.file_name().unwrap().to_str().unwrap();
let torrent_path = cmd
.torrent_directory
.join(release_name.to_owned() + ".torrent");
imdl::torrent::create_torrent(
path,
&torrent_path,
format!("{}/{}/announce", TRACKER_URL, passkey),
)
.await?;
term.write_line(&format!(
"{} Created .torrent files for format {}",
SUCCESS, format
))?;
let torrent_file_data = tokio::fs::read(&torrent_path).await?;
let perma_link = perma_link(group_id, torrent_id);
let description = create_description(perma_link.clone(), command.clone());
let format_red = match format {
ReleaseType::Flac24 => "FLAC",
ReleaseType::Flac => "FLAC",
ReleaseType::Mp3320 => "MP3",
ReleaseType::Mp3V0 => "MP3",
};
let bitrate = match format {
ReleaseType::Flac24 => "24bit Lossless".to_string(),
ReleaseType::Flac => "Lossless".to_string(),
ReleaseType::Mp3320 => "320".to_string(),
ReleaseType::Mp3V0 => "V0 (VBR)".to_string(),
};
if cmd.move_transcode_to_content {
tokio::fs::rename(
&path,
&cmd.content_directory.join(path.file_name().unwrap()),
)
.await?;
term.write_line(&format!(
"{} Moved transcode release to content directory",
SUCCESS,
))?;
}
if !cmd.automatic_upload {
term.write_line("[⏸️] Manual mode enabled, skipping automatic upload")?;
let scene = if torrent.scene { "Yes" } else { "No" };
let format = match format {
ReleaseType::Flac24 => "FLAC",
ReleaseType::Flac => "FLAC",
ReleaseType::Mp3320 => "MP3",
ReleaseType::Mp3V0 => "MP3",
};
term.write_line(&*("Link: ".to_owned() + &*perma_link))?;
term.write_line(&*("Name: ".to_owned() + &*group.name.clone()))?;
term.write_line(
&*("Artist(s): ".to_owned()
+ &group
.music_info
.artists
.iter()
.map(|a| a.name.clone())
.collect::<Vec<String>>()
.join(", ")),
)?;
term.write_line(&*("Edition Year: ".to_owned() + &*torrent.remaster_year.to_string()))?;
term.write_line(&*("Edition Title: ".to_owned() + &torrent.remaster_title))?;
term.write_line(&*("Record Label: ".to_owned() + &torrent.remaster_record_label))?;
term.write_line(
&*("Catalogue Number: ".to_owned() + &torrent.remaster_catalogue_number),
)?;
term.write_line(&*("Scene: ".to_owned() + scene))?;
term.write_line(&*("Format: ".to_owned() + format))?;
term.write_line(&*("Bitrate: ".to_owned() + &bitrate))?;
term.write_line(&*("Media: ".to_owned() + &torrent.media))?;
term.write_line("Release Description:")?;
term.write_line(&description)?;
let mut prompt = Confirm::new();
prompt
.with_prompt("Confirm once you are done uploading...")
.default(true);
prompt.interact()?;
} else if !cmd.dry_run {
let year = if torrent.remaster_year == 0 {
group.year
} else {
torrent.remaster_year
};
let upload_data = TorrentUploadData {
torrent: torrent_file_data,
torrent_name: torrent_path
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_string(),
r#type: Category::from(&*group.category_name),
remaster_year: year,
remaster_title: torrent.remaster_title.clone(),
remaster_record_label: torrent.remaster_record_label.clone(),
remaster_catalogue_number: torrent.remaster_catalogue_number.clone(),
format: format_red.to_string(),
bitrate: bitrate.clone(),
media: torrent.media.clone(),
release_desc: description.clone(),
group_id: group.id as u64,
};
let res = api.upload_torrent(upload_data).await?;
term.write_line(&format!("[🔼] Uploaded {} release to REDacted https://redacted.ch/torrents.php?id={}&torrentid={}", format, group_id, res.response.torrent_id))?;
}
}
Ok(())
}