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
|
use std::{str::FromStr, time::Duration};
use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use futures::{FutureExt, future::BoxFuture};
use log::{error, warn};
use serde_json::json;
use tokio::{fs, io};
use url::Url;
use yt_dlp::{
YoutubeDL, info_json::InfoJson, json_cast, json_get, json_try_get, options::YoutubeDLOptions,
};
use crate::{
app::App,
select::duration::MaybeDuration,
shared::bytes::Bytes,
storage::db::{
extractor_hash::ExtractorHash,
subscription::Subscription,
video::{Priority, TimeStamp, Video, VideoStatus},
},
};
pub(crate) fn yt_dlp_opts_updating(max_backlog: usize) -> Result<YoutubeDL> {
Ok(YoutubeDLOptions::new()
.set("playliststart", 1)
.set("playlistend", max_backlog)
.set("noplaylist", false)
.set(
"extractor_args",
json! {{"youtubetab": {"approximate_date": [""]}}},
)
// // TODO: This also removes unlisted and other stuff. Find a good way to remove the
// // members-only videos from the feed. <2025-04-17>
// .set("match-filter", "availability=public")
.build()?)
}
impl Video {
pub(crate) fn get_approx_size(&self) -> Result<u64> {
let yt_dlp = {
YoutubeDLOptions::new()
.set("prefer_free_formats", true)
.set("format", "bestvideo[height<=?1080]+bestaudio/best")
.set("fragment_retries", 10)
.set("retries", 10)
.set("getcomments", false)
.set("ignoreerrors", false)
.build()
.context("Failed to instanciate get approx size yt_dlp")
}?;
let result = yt_dlp
.extract_info(&self.url, false, true)
.with_context(|| format!("Failed to extract video information: '{}'", self.title))?;
let size = if let Some(filesize) = json_try_get!(result, "filesize", as_u64) {
filesize
} else if let Some(num) = json_try_get!(result, "filesize_approx", as_u64) {
num
} else if let Some(duration) = json_try_get!(result, "duration", as_f64)
&& let Some(tbr) = json_try_get!(result, "tbr", as_f64)
{
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
let duration = duration.ceil() as u64;
// TODO: yt_dlp gets this from the format
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
let tbr = tbr.ceil() as u64;
duration * tbr * (1000 / 8)
} else {
let hardcoded_default = Bytes::from_str("250 MiB").expect("This is hardcoded");
error!(
"Failed to find a filesize for video: {:?} (Using hardcoded value of {})",
self.title, hardcoded_default
);
hardcoded_default.as_u64()
};
Ok(size)
}
}
impl Video {
#[allow(clippy::too_many_lines)]
pub(crate) fn from_info_json(entry: &InfoJson, sub: Option<&Subscription>) -> Result<Video> {
fn fmt_context(date: &str, extended: Option<&str>) -> String {
let f = format!(
"Failed to parse the `upload_date` of the entry ('{date}'). \
Expected `YYYY-MM-DD`, has the format changed?"
);
if let Some(date_string) = extended {
format!("{f}\nThe parsed '{date_string}' can't be turned to a valid UTC date.'")
} else {
f
}
}
let publish_date = if let Some(date) = json_try_get!(entry, "upload_date", as_str) {
let year: u32 = date
.chars()
.take(4)
.collect::<String>()
.parse()
.with_context(|| fmt_context(date, None))?;
let month: u32 = date
.chars()
.skip(4)
.take(2)
.collect::<String>()
.parse()
.with_context(|| fmt_context(date, None))?;
let day: u32 = date
.chars()
.skip(4 + 2)
.take(2)
.collect::<String>()
.parse()
.with_context(|| fmt_context(date, None))?;
let date_string = format!("{year:04}-{month:02}-{day:02}T00:00:00Z");
Some(
DateTime::<Utc>::from_str(&date_string)
.with_context(|| fmt_context(date, Some(&date_string)))?
.timestamp(),
)
} else {
warn!(
"The video '{}' lacks it's upload date!",
json_get!(entry, "title", as_str)
);
None
};
let thumbnail_url = match (
&json_try_get!(entry, "thumbnails", as_array),
&json_try_get!(entry, "thumbnail", as_str),
) {
(None, None) => None,
(None, Some(thumbnail)) => Some(Url::from_str(thumbnail)?),
// TODO: The algorithm is not exactly the best <2024-05-28>
(Some(thumbnails), None) => {
if let Some(thumbnail) = thumbnails.first() {
Some(Url::from_str(json_get!(
json_cast!(thumbnail, as_object),
"url",
as_str
))?)
} else {
None
}
}
(Some(_), Some(thumnail)) => Some(Url::from_str(thumnail)?),
};
let url = {
let smug_url: Url = json_get!(entry, "webpage_url", as_str).parse()?;
// TODO(@bpeetz): We should probably add this? <2025-06-14>
// if '#__youtubedl_smuggle' not in smug_url:
// return smug_url, default
// url, _, sdata = smug_url.rpartition('#')
// jsond = urllib.parse.parse_qs(sdata)['__youtubedl_smuggle'][0]
// data = json.loads(jsond)
// return url, data
smug_url
};
let extractor_hash = ExtractorHash::from_info_json(entry);
let subscription_name = if let Some(sub) = sub {
Some(sub.name.clone())
} else if let Some(uploader) = json_try_get!(entry, "uploader", as_str) {
if json_try_get!(entry, "webpage_url_domain", as_str) == Some("youtube.com") {
Some(format!("{uploader} - Videos"))
} else {
Some(uploader.to_owned())
}
} else {
None
};
let video = Video {
description: json_try_get!(entry, "description", as_str).map(ToOwned::to_owned),
duration: MaybeDuration::from_maybe_secs_f64(json_try_get!(entry, "duration", as_f64)),
extractor_hash,
last_status_change: TimeStamp::from_now(),
parent_subscription_name: subscription_name,
priority: Priority::default(),
publish_date: publish_date.map(TimeStamp::from_secs),
status: VideoStatus::Pick,
thumbnail_url,
title: json_get!(entry, "title", as_str).to_owned(),
url,
watch_progress: Duration::default(),
playback_speed: None,
subtitle_langs: None,
};
Ok(video)
}
}
pub(crate) async fn get_current_cache_allocation(app: &App) -> Result<Bytes> {
fn dir_size(mut dir: fs::ReadDir) -> BoxFuture<'static, Result<Bytes>> {
async move {
let mut acc = 0;
while let Some(entry) = dir.next_entry().await? {
let size = match entry.metadata().await? {
data if data.is_dir() => {
let path = entry.path();
let read_dir = fs::read_dir(path).await?;
dir_size(read_dir).await?.as_u64()
}
data => data.len(),
};
acc += size;
}
Ok(Bytes::new(acc))
}
.boxed()
}
let read_dir_result = match fs::read_dir(&app.config.paths.download_dir).await {
Ok(ok) => ok,
Err(err) => match err.kind() {
io::ErrorKind::NotFound => {
unreachable!("The download dir should always be created in the config finalizers.");
}
err => Err(io::Error::from(err)).with_context(|| {
format!(
"Failed to get dir size of download dir at: '{}'",
&app.config.paths.download_dir.display()
)
})?,
},
};
dir_size(read_dir_result).await
}
|