termusiclib/invidious.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
/**
* MIT License
*
* termusic - Copyright (c) 2021 Larry Hao
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
use anyhow::{anyhow, bail, Result};
use rand::seq::SliceRandom;
use serde_json::Value;
// left for debug
// use std::io::Write;
use reqwest::{Client, ClientBuilder, StatusCode};
use std::time::Duration;
const INVIDIOUS_INSTANCE_LIST: [&str; 7] = [
"https://vid.puffyan.us",
"https://inv.riverside.rocks",
"https://invidious.osi.kr",
"https://youtube.076.ne.jp",
"https://y.com.sb",
"https://yt.artemislena.eu",
"https://invidious.tiekoetter.com",
// Below lines are left for testing
// "https://www.google.com",
// "https://www.google.com",
// "https://www.google.com",
// "https://www.google.com",
// "https://www.google.com",
// "https://www.google.com",
// "https://www.google.com",
];
const INVIDIOUS_DOMAINS: &str = "https://api.invidious.io/instances.json?sort_by=type,users";
#[derive(Clone, Debug)]
pub struct Instance {
pub domain: Option<String>,
client: Client,
query: Option<String>,
}
impl PartialEq for Instance {
fn eq(&self, other: &Self) -> bool {
self.domain == other.domain
}
}
impl Eq for Instance {}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct YoutubeVideo {
pub title: String,
pub length_seconds: u64,
pub video_id: String,
}
impl Default for Instance {
fn default() -> Self {
let client = Client::new();
let domain = Some(String::new());
let query = Some(String::new());
Self {
domain,
client,
query,
}
}
}
#[allow(unused)]
impl Instance {
pub async fn new(query: &str) -> Result<(Self, Vec<YoutubeVideo>)> {
let client = ClientBuilder::new()
.timeout(Duration::from_secs(10))
.build()?;
let mut domain = String::new();
let mut domains = vec![];
// prefor fetch invidious instance from website, but will provide 7 backups
if let Ok(domain_list) = Self::get_invidious_instance_list(&client).await {
domains = domain_list;
} else {
for item in &INVIDIOUS_INSTANCE_LIST {
domains.push((*item).to_string());
}
}
domains.shuffle(&mut rand::thread_rng());
let mut video_result: Vec<YoutubeVideo> = Vec::new();
for v in domains {
let url = format!("{v}/api/v1/search");
let query_vec = vec![
("q", query),
("page", "1"),
("type", "video"),
("sort_by", "relevance"),
];
if let Ok(result) = client.get(&url).query(&query_vec).send().await {
if result.status() == 200 {
if let Ok(text) = result.text().await {
if let Some(vr) = Self::parse_youtube_options(&text) {
video_result = vr;
domain = v;
break;
}
}
}
}
}
if domain.len() < 2 {
bail!("Something is wrong with your connection or all 7 invidious servers are down.");
}
let domain = Some(domain);
Ok((
Self {
domain,
client,
query: Some(query.to_string()),
},
video_result,
))
}
// GetSearchQuery fetches query result from an Invidious instance.
pub async fn get_search_query(&self, page: u32) -> Result<Vec<YoutubeVideo>> {
if self.domain.is_none() {
bail!("No server available");
}
let url = format!(
"{}/api/v1/search",
self.domain
.as_ref()
.ok_or(anyhow!("error in domain name"))?
);
let Some(query) = &self.query else {
bail!("No query string found")
};
let result = self
.client
.get(url)
.query(&[("q", query), ("page", &page.to_string())])
.send()
.await?;
match result.status() {
StatusCode::OK => match result.text().await {
Ok(text) => Self::parse_youtube_options(&text).ok_or_else(|| anyhow!("None Error")),
Err(e) => bail!("Error during search: {}", e),
},
_ => bail!("Error during search"),
}
}
// GetSuggestions returns video suggestions based on prefix strings. This is the
// same result as youtube search autocomplete.
pub async fn get_suggestions(&self, prefix: &str) -> Result<Vec<YoutubeVideo>> {
let url = format!(
"http://suggestqueries.google.com/complete/search?client=firefox&ds=yt&q={prefix}"
);
let result = self.client.get(url).send().await?;
match result.status() {
StatusCode::OK => match result.text().await {
Ok(text) => Self::parse_youtube_options(&text).ok_or_else(|| anyhow!("None Error")),
Err(e) => bail!("Error during search: {}", e),
},
_ => bail!("Error during search"),
}
}
// GetTrendingMusic fetch music trending based on region.
// Region (ISO 3166 country code) can be provided in the argument.
pub async fn get_trending_music(&self, region: &str) -> Result<Vec<YoutubeVideo>> {
if self.domain.is_none() {
bail!("No server available");
}
let url = format!(
"{}/api/v1/trending?type=music®ion={region}",
self.domain
.as_ref()
.ok_or(anyhow!("error in domain names"))?
);
let result = self.client.get(url).send().await?;
match result.status() {
StatusCode::OK => match result.text().await {
Ok(text) => Self::parse_youtube_options(&text).ok_or_else(|| anyhow!("None Error")),
_ => bail!("Error during search"),
},
_ => bail!("Error during search"),
}
}
fn parse_youtube_options(data: &str) -> Option<Vec<YoutubeVideo>> {
if let Ok(value) = serde_json::from_str::<Value>(data) {
let mut vec: Vec<YoutubeVideo> = Vec::new();
// below two lines are left for debug purpose
// let mut file = std::fs::File::create("data.txt").expect("create failed");
// file.write_all(data.as_bytes()).expect("write failed");
if let Some(array) = value.as_array() {
for v in array {
if let Some((title, video_id, length_seconds)) = Self::parse_youtube_item(v) {
vec.push(YoutubeVideo {
title,
length_seconds,
video_id,
});
}
}
return Some(vec);
}
}
None
}
fn parse_youtube_item(value: &Value) -> Option<(String, String, u64)> {
let title = value.get("title")?.as_str()?.to_owned();
let video_id = value.get("videoId")?.as_str()?.to_owned();
let length_seconds = value.get("lengthSeconds")?.as_u64()?;
Some((title, video_id, length_seconds))
}
async fn get_invidious_instance_list(client: &Client) -> Result<Vec<String>> {
let result = client.get(INVIDIOUS_DOMAINS).send().await?.text().await?;
// Left here for debug
// let mut file = std::fs::File::create("data.txt").expect("create failed");
// file.write_all(result.as_bytes()).expect("write failed");
if let Some(vec) = Self::parse_invidious_instance_list(&result) {
return Ok(vec);
}
bail!("no instance list fetched")
}
fn parse_invidious_instance_list(data: &str) -> Option<Vec<String>> {
if let Ok(value) = serde_json::from_str::<Value>(data) {
let mut vec = Vec::new();
if let Some(array) = value.as_array() {
for inner_value in array {
if let Some((uri, health)) = Self::parse_instance(inner_value) {
if health > 95.0 {
vec.push(uri);
}
}
}
}
if !vec.is_empty() {
return Some(vec);
}
}
None
}
fn parse_instance(value: &Value) -> Option<(String, f64)> {
let obj = value.get(1)?.as_object()?;
if obj.get("api")?.as_bool()? {
let uri = obj.get("uri")?.as_str()?.to_owned();
let health = obj
.get("monitor")?
.as_object()?
.get("30dRatio")?
.get("ratio")?
.as_str()?
.to_owned()
.parse::<f64>()
.ok();
health.map(|health| (uri, health))
} else {
None
}
}
}