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
|
// 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::path::PathBuf;
use chrono::DateTime;
use format_video::FormatVideo;
use owo_colors::OwoColorize;
use url::Url;
use crate::{
app::App,
select::selection_file::duration::Duration,
storage::video_database::{getters::get_video_opts, Video},
};
use anyhow::{Context, Result};
pub mod format_video;
macro_rules! get {
($value:expr, $key:ident, $name:expr, $code:tt) => {
if let Some(value) = &$value.$key {
$code(value)
} else {
concat!("[No ", $name, "]").to_owned()
}
};
}
/// This is identical to a [`FormattedVideo`], but has colorized fields.
pub struct ColorizedFormattedVideo(FormattedVideo);
impl FormattedVideo {
pub fn colorize(self) -> ColorizedFormattedVideo {
let Self {
cache_path,
description,
duration,
extractor_hash,
last_status_change,
parent_subscription_name,
priority,
publish_date,
status,
status_change,
thumbnail_url,
title,
url,
video_options,
} = self;
ColorizedFormattedVideo(Self {
cache_path: cache_path.blue().bold().to_string(),
description,
duration: duration.cyan().bold().to_string(),
extractor_hash: extractor_hash.bright_purple().italic().to_string(),
last_status_change: last_status_change.bright_cyan().to_string(),
parent_subscription_name: parent_subscription_name.bright_magenta().to_string(),
priority,
publish_date: publish_date.bright_white().bold().to_string(),
status: status.red().bold().to_string(),
status_change,
thumbnail_url,
title: title.green().bold().to_string(),
url: url.italic().to_string(),
video_options: video_options.bright_green().to_string(),
})
}
}
/// This is a version of [`Video`] that has all the fields of the original [`Video`] structure
/// turned to [`String`]s to facilitate displaying it.
///
/// This structure provides a way to display a [`Video`] in a coherent way, as it enforces to
/// always use the same colors for one field.
#[derive(Debug)]
pub struct FormattedVideo {
cache_path: String,
description: String,
duration: String,
extractor_hash: String,
last_status_change: String,
parent_subscription_name: String,
priority: String,
publish_date: String,
status: String,
status_change: String,
thumbnail_url: String,
title: String,
url: String,
/// This string contains the video options (speed, subtitle_languages, etc.).
/// It already starts with an extra whitespace, when these are not empty.
video_options: String,
}
impl Video {
pub async fn to_formatted_video_owned(self, app: &App) -> Result<FormattedVideo> {
Self::to_formatted_video(&self, app).await
}
pub async fn to_formatted_video(&self, app: &App) -> Result<FormattedVideo> {
fn date_from_stamp(stamp: i64) -> String {
DateTime::from_timestamp(stamp, 0)
.expect("The timestamps should always be valid")
.format("%Y-%m-%d")
.to_string()
}
let cache_path: String = get!(
self,
cache_path,
"Cache Path",
(|value: &PathBuf| value.to_string_lossy().to_string())
);
let description = get!(
self,
description,
"Description",
(|value: &str| value.to_owned())
);
let duration = Duration::from(self.duration);
let extractor_hash = self
.extractor_hash
.into_short_hash(app)
.await
.with_context(|| {
format!(
"Failed to format extractor hash, whilst formatting video: '{}'",
self.title
)
})?;
let last_status_change = date_from_stamp(self.last_status_change);
let parent_subscription_name = get!(
self,
parent_subscription_name,
"author",
(|sub: &str| sub.replace('"', "'"))
);
let priority = self.priority;
let publish_date = get!(
self,
publish_date,
"release date",
(|date: &i64| date_from_stamp(*date))
);
// TODO: We might support `.trim()`ing that, as the extra whitespace could be bad in the
// selection file. <2024-10-07>
let status = self.status.as_command();
let status_change = self.status_change;
let thumbnail_url = get!(
self,
thumbnail_url,
"thumbnail URL",
(|url: &Url| url.to_string())
);
let title = self.title.replace(['"', '„', '”'], "'");
let url = self.url.as_str().replace('"', "\\\"");
let video_options = {
let opts = get_video_opts(app, &self.extractor_hash)
.await
.with_context(|| {
format!("Failed to get video options for video: '{}'", self.title)
})?
.to_cli_flags(app);
let opts_white = if !opts.is_empty() { " " } else { "" };
format!("{}{}", opts_white, opts)
};
Ok(FormattedVideo {
cache_path,
description,
duration: duration.to_string(),
extractor_hash: extractor_hash.to_string(),
last_status_change,
parent_subscription_name,
priority: priority.to_string(),
publish_date,
status: status.to_string(),
status_change: status_change.to_string(),
thumbnail_url,
title,
url,
video_options,
})
}
}
impl<'a> FormatVideo for &'a FormattedVideo {
type Output = &'a str;
fn cache_path(&self) -> Self::Output {
&self.cache_path
}
fn description(&self) -> Self::Output {
&self.description
}
fn duration(&self) -> Self::Output {
&self.duration
}
fn extractor_hash(&self) -> Self::Output {
&self.extractor_hash
}
fn last_status_change(&self) -> Self::Output {
&self.last_status_change
}
fn parent_subscription_name(&self) -> Self::Output {
&self.parent_subscription_name
}
fn priority(&self) -> Self::Output {
&self.priority
}
fn publish_date(&self) -> Self::Output {
&self.publish_date
}
fn status(&self) -> Self::Output {
&self.status
}
fn status_change(&self) -> Self::Output {
&self.status_change
}
fn thumbnail_url(&self) -> Self::Output {
&self.thumbnail_url
}
fn title(&self) -> Self::Output {
&self.title
}
fn url(&self) -> Self::Output {
&self.url
}
fn video_options(&self) -> Self::Output {
&self.video_options
}
}
impl<'a> FormatVideo for &'a ColorizedFormattedVideo {
type Output = &'a str;
fn cache_path(&self) -> Self::Output {
&self.0.cache_path
}
fn description(&self) -> Self::Output {
&self.0.description
}
fn duration(&self) -> Self::Output {
&self.0.duration
}
fn extractor_hash(&self) -> Self::Output {
&self.0.extractor_hash
}
fn last_status_change(&self) -> Self::Output {
&self.0.last_status_change
}
fn parent_subscription_name(&self) -> Self::Output {
&self.0.parent_subscription_name
}
fn priority(&self) -> Self::Output {
&self.0.priority
}
fn publish_date(&self) -> Self::Output {
&self.0.publish_date
}
fn status(&self) -> Self::Output {
&self.0.status
}
fn status_change(&self) -> Self::Output {
&self.0.status_change
}
fn thumbnail_url(&self) -> Self::Output {
&self.0.thumbnail_url
}
fn title(&self) -> Self::Output {
&self.0.title
}
fn url(&self) -> Self::Output {
&self.0.url
}
fn video_options(&self) -> Self::Output {
&self.0.video_options
}
}
|