Compare commits

...
3 Commits
4 changed files with 57 additions and 14 deletions

No files matched your search

+12
View File
@@ -168,6 +168,14 @@ auto_derived!(
impl WebsiteMetadata { impl WebsiteMetadata {
/// Truncate strings in metadata /// Truncate strings in metadata
pub fn truncate(&mut self) { pub fn truncate(&mut self) {
if let Some(s) = self.url.as_mut() {
s.truncate(256);
}
if let Some(s) = self.original_url.as_mut() {
s.truncate(256);
}
if let Some(s) = self.title.as_mut() { if let Some(s) = self.title.as_mut() {
s.truncate(100); s.truncate(100);
} }
@@ -180,6 +188,10 @@ impl WebsiteMetadata {
s.truncate(32); s.truncate(32);
} }
if let Some(s) = self.icon_url.as_mut() {
s.truncate(256);
}
if let Some(s) = self.colour.as_mut() { if let Some(s) = self.colour.as_mut() {
s.truncate(32); s.truncate(32);
} }
+2
View File
@@ -66,6 +66,8 @@ async fn main() -> Result<(), std::io::Error> {
.nest("/", api::router().await); .nest("/", api::router().await);
// Configure TCP listener and bind // Configure TCP listener and bind
tracing::info!("Listening on 0.0.0.0:14705");
tracing::info!("Play around with the API: http://localhost:14705/scalar");
let address = SocketAddr::from((Ipv4Addr::UNSPECIFIED, 14705)); let address = SocketAddr::from((Ipv4Addr::UNSPECIFIED, 14705));
let listener = TcpListener::bind(&address).await?; let listener = TcpListener::bind(&address).await?;
axum::serve(listener, app.into_make_service()).await axum::serve(listener, app.into_make_service()).await
+11 -1
View File
@@ -156,7 +156,7 @@ impl Request {
let response = if let Some(Request { response, .. }) = request { let response = if let Some(Request { response, .. }) = request {
response response
} else { } else {
let Request { response, mime } = Request::new(&url).await?; let Request { response, mime } = Request::new(url).await?;
if matches!(mime.type_(), mime::VIDEO) { if matches!(mime.type_(), mime::VIDEO) {
response response
} else { } else {
@@ -240,6 +240,7 @@ impl Request {
"Mozilla/5.0 (compatible; January/2.0; +https://github.com/revoltchat/backend)" "Mozilla/5.0 (compatible; January/2.0; +https://github.com/revoltchat/backend)"
}, },
) )
.header("Accept-Language", "en-US,en;q=0.5")
.send() .send()
.await .await
.map_err(|_| create_error!(ProxyError))?; .map_err(|_| create_error!(ProxyError))?;
@@ -262,4 +263,13 @@ impl Request {
Ok(Request { response, mime }) Ok(Request { response, mime })
} }
/// Check if something exists
pub async fn exists(url: &str) -> bool {
if let Ok(response) = CLIENT.head(url).send().await {
response.status().is_success()
} else {
false
}
}
} }
+32 -13
View File
@@ -207,24 +207,43 @@ pub fn populate_special(original_url: String, metadata: &mut WebsiteMetadata) {
id: captures[1].to_string(), id: captures[1].to_string(),
}) })
} else if let Some(captures) = RE_YOUTUBE.captures_iter(url).next() { } else if let Some(captures) = RE_YOUTUBE.captures_iter(url).next() {
let id = captures[1].to_string();
lazy_static! { lazy_static! {
static ref RE_TIMESTAMP: Regex = Regex::new("(?:\\?|&)(?:t|start)=([\\w]+)").unwrap(); static ref RE_TIMESTAMP: Regex = Regex::new("(?:\\?|&)(?:t|start)=([\\w]+)").unwrap();
} }
if let Some(video) = &metadata.video { // YouTube now blocks datacentre IPs from fetching information
if let Some(timestamp_captures) = RE_TIMESTAMP.captures_iter(&video.url).next() { // This is a fallback to prevent the embed from looking weird
Some(Special::YouTube { if metadata.video.is_none() {
id: captures[1].to_string(), metadata.title.replace("YouTube".to_owned());
timestamp: Some(timestamp_captures[1].to_string()), metadata.description.take();
}) metadata.colour.take();
} else { metadata.icon_url.take();
Some(Special::YouTube { metadata.site_name.take();
id: captures[1].to_string(),
timestamp: None, // Verify the video exists
}) // TODO: breaks axum :(
} // if !crate::requests::Request::exists(&format!(
// "http://img.youtube.com/vi/{}/sddefault.jpg",
// id
// ))
// .await
// {
// return;
// }
}
if let Some(timestamp_captures) = RE_TIMESTAMP.captures_iter(url).next() {
Some(Special::YouTube {
id,
timestamp: Some(timestamp_captures[1].to_string()),
})
} else { } else {
None Some(Special::YouTube {
id,
timestamp: None,
})
} }
} else if let Some(captures) = RE_LIGHTSPEED.captures_iter(url).next() { } else if let Some(captures) = RE_LIGHTSPEED.captures_iter(url).next() {
Some(Special::Lightspeed { Some(Special::Lightspeed {