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
|
// 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::str::FromStr;
use anyhow::{bail, Context, Result};
use futures::FutureExt;
use log::warn;
use serde_json::{json, Value};
use tokio::io::{AsyncBufRead, AsyncBufReadExt};
use url::Url;
use yt_dlp::wrapper::info_json::InfoType;
use crate::{
app::App,
storage::subscriptions::{
add_subscription, check_url, get_subscriptions, remove_all_subscriptions,
remove_subscription, Subscription,
},
};
pub async fn unsubscribe(app: &App, name: String) -> Result<()> {
let present_subscriptions = get_subscriptions(app).await?;
if let Some(subscription) = present_subscriptions.0.get(&name) {
remove_subscription(app, subscription).await?;
} else {
bail!("Couldn't find subscription: '{}'", &name);
}
Ok(())
}
pub async fn import<W: AsyncBufRead + AsyncBufReadExt + Unpin>(
app: &App,
reader: W,
force: bool,
) -> Result<()> {
if force {
remove_all_subscriptions(app).await?;
}
let mut lines = reader.lines();
while let Some(line) = lines.next_line().await? {
let url =
Url::from_str(&line).with_context(|| format!("Failed to parse '{}' as url", line))?;
match subscribe(app, None, url)
.await
.with_context(|| format!("Failed to subscribe to: '{}'", line))
{
Ok(_) => (),
Err(err) => eprintln!(
"Error while subscribing to '{}': '{}'",
line,
err.source().expect("Should have a source")
),
}
}
Ok(())
}
pub async fn subscribe(app: &App, name: Option<String>, url: Url) -> Result<()> {
if !(url.as_str().ends_with("videos")
|| url.as_str().ends_with("streams")
|| url.as_str().ends_with("shorts")
|| url.as_str().ends_with("videos/")
|| url.as_str().ends_with("streams/")
|| url.as_str().ends_with("shorts/"))
&& url.as_str().contains("youtube.com")
{
warn!("Your youtbe url does not seem like it actually tracks a channels playlist (videos, streams, shorts). Adding subscriptions for each of them...");
let url = Url::parse(&(url.as_str().to_owned() + "/"))
.expect("This was an url, it should stay one");
if let Some(name) = name {
let out: Result<()> = async move {
actual_subscribe(
app,
Some(name.clone() + " {Videos}"),
url.join("videos/").expect("Works"),
)
.await
.with_context(|| {
format!("Failed to subscribe to '{}'", name.clone() + " {Videos}")
})?;
actual_subscribe(
app,
Some(name.clone() + " {Streams}"),
url.join("streams/").expect("Works"),
)
.await
.with_context(|| {
format!("Failed to subscribe to '{}'", name.clone() + " {Streams}")
})?;
actual_subscribe(
app,
Some(name.clone() + " {Shorts}"),
url.join("shorts/").expect("Works"),
)
.await
.with_context(|| format!("Failed to subscribe to '{}'", name + " {Shorts}"))?;
Ok(())
}
.boxed()
.await;
out?
} else {
actual_subscribe(app, None, url.join("videos/").expect("Works"))
.await
.with_context(|| format!("Failed to subscribe to the '{}' variant", "{Videos}"))?;
actual_subscribe(app, None, url.join("streams/").expect("Works"))
.await
.with_context(|| format!("Failed to subscribe to the '{}' variant", "{Streams}"))?;
actual_subscribe(app, None, url.join("shorts/").expect("Works"))
.await
.with_context(|| format!("Failed to subscribe to the '{}' variant", "{Shorts}"))?;
}
} else {
actual_subscribe(app, name, url).await?;
}
Ok(())
}
async fn actual_subscribe(app: &App, name: Option<String>, url: Url) -> Result<()> {
if !check_url(&url).await? {
bail!("The url ('{}') does not represent a playlist!", &url)
};
let name = if let Some(name) = name {
name
} else {
let yt_opts = match json!( {
"playliststart": 1,
"playlistend": 10,
"noplaylist": false,
"extract_flat": "in_playlist",
}) {
Value::Object(map) => map,
_ => unreachable!("This is hardcoded"),
};
let info = yt_dlp::extract_info(&yt_opts, &url, false, false).await?;
if info._type == Some(InfoType::Playlist) {
info.title.expect("This should be some for a playlist")
} else {
bail!("The url ('{}') does not represent a playlist!", &url)
}
};
let present_subscriptions = get_subscriptions(app).await?;
if let Some(subs) = present_subscriptions.0.get(&name) {
bail!(
"The subscription '{}' could not be added, \
as another one with the same name ('{}') already exists. It links to the Url: '{}'",
name,
name,
subs.url
);
}
let sub = Subscription { name, url };
add_subscription(app, &sub).await?;
Ok(())
}
|