diff --git a/src/album.rs b/src/album.rs index 64fed2d..8983a01 100644 --- a/src/album.rs +++ b/src/album.rs @@ -2,9 +2,8 @@ use crate::genius::{self, GeniusAlbumResponse}; use crate::settings::{settings_from_req, Settings}; use crate::utils; use actix_web::HttpRequest; -use actix_web::{get, web, Responder, Result}; +use actix_web::{get, Responder, Result}; use askama::Template; -use serde::Deserialize; use crate::genius::GeniusAlbum; use crate::templates::template; @@ -16,18 +15,11 @@ struct AlbumTemplate { album: GeniusAlbum, } -#[derive(Debug, Deserialize)] -pub struct AlbumQuery { - path: String, -} - -#[get("/album")] -pub async fn album(req: HttpRequest, info: web::Query) -> Result { - let album_res = genius::extract_data::(&utils::ensure_path_prefix( - "albums", &info.path, - )) - .await?; - let mut album = album_res.album; +#[get("/albums/{name:.*}")] +pub async fn album(req: HttpRequest) -> Result { + let mut album = genius::extract_data::(req.path()) + .await? + .album; album.tracks = Some(genius::get_album_tracks(album.id).await?); diff --git a/src/artist.rs b/src/artist.rs index 06fe1db..70b636b 100644 --- a/src/artist.rs +++ b/src/artist.rs @@ -1,10 +1,9 @@ use crate::settings::{settings_from_req, Settings}; use crate::utils; -use actix_web::{get, web, HttpRequest, Responder, Result}; +use actix_web::{get, HttpRequest, Responder, Result}; use askama::Template; use lazy_regex::*; use regex::Regex; -use serde::Deserialize; use crate::genius::{self, GeniusArtist}; use crate::genius::{GeniusArtistResponse, SortMode}; @@ -12,8 +11,6 @@ use crate::templates::template; static GENIUS_IMAGE_URL: &str = "https://images.genius.com/"; static GENIUS_BASE_PATTERN: Lazy = lazy_regex!(r#"https?://\w*.?genius\.com/"#); -static GENIUS_ALBUMS_PATTERN: Lazy = lazy_regex!(r#"https?://\w*.?genius\.com/albums/"#); -static GENIUS_ARTIST_PATTERN: Lazy = lazy_regex!(r#"https?://\w*.?genius\.com/artists/"#); #[derive(Template)] #[template(path = "artist.html")] @@ -22,20 +19,13 @@ struct ArtistTemplate { artist: GeniusArtist, } -#[derive(Debug, Deserialize)] -pub struct ArtistQuery { - path: String, -} - const MAX_SONGS: u8 = 5; -#[get("/artist")] -pub async fn artist(req: HttpRequest, info: web::Query) -> Result { - let artist_res = genius::extract_data::(&utils::ensure_path_prefix( - "artists", &info.path, - )) - .await?; - let mut artist = artist_res.artist; +#[get("/artists/{name}")] +pub async fn artist(req: HttpRequest) -> Result { + let mut artist = genius::extract_data::(req.path()) + .await? + .artist; artist.popular_songs = Some(genius::get_artist_songs(artist.id, SortMode::Popularity, MAX_SONGS).await?); @@ -58,8 +48,6 @@ fn rewrite_links(html: &str) -> String { GENIUS_IMAGE_URL, &format!("/api/image?url={}", GENIUS_IMAGE_URL), ); // Images - let html = GENIUS_ALBUMS_PATTERN.replace_all(&html, "/album?path=albums/"); // Albums - let html = GENIUS_ARTIST_PATTERN.replace_all(&html, "/artist?path=artists/"); // Artists - let html = GENIUS_BASE_PATTERN.replace_all(&html, "/lyrics?path=/"); // Lyrics + let html = GENIUS_BASE_PATTERN.replace_all(&html, ""); // We follow Genius' schema html.to_string() } diff --git a/src/lyrics.rs b/src/lyrics.rs index 8ceae2a..f5bff31 100644 --- a/src/lyrics.rs +++ b/src/lyrics.rs @@ -25,34 +25,40 @@ struct Verse { #[derive(Template)] #[template(path = "lyrics.html")] -struct LyricsTemplate { +struct LyricsTemplate<'a> { settings: Settings, verses: Vec, - query: LyricsQuery, + path: &'a str, song: GeniusSong, } #[derive(Debug, Deserialize)] pub struct LyricsQuery { id: Option, - path: String, } -#[get("/lyrics")] +#[get("/{path}-lyrics")] pub async fn lyrics(req: HttpRequest, info: web::Query) -> Result { let document: Html; let song: GeniusSong; + // The '-lyrics' bit of the path gets cut off since we match for it explicitly, + // so we need to add it back here otherwise the path will be incorrect. + let path = &format!( + "{}-lyrics", + req.match_info().query("path").trim_end_matches('?') + ); + if let Some(id) = info.id { let responses = future::join( - genius::get_text(genius::SubDomain::Root, &info.path, None), + genius::get_text(genius::SubDomain::Root, path, None), genius::get_song(id), ) .await; document = Html::parse_document(&responses.0?); song = responses.1?; } else { - let lyric_page = genius::get_text(genius::SubDomain::Root, &info.path, None).await?; + let lyric_page = genius::get_text(genius::SubDomain::Root, path, None).await?; document = Html::parse_document(&lyric_page); let id = get_song_id(&document)?; song = genius::get_song(id).await?; @@ -65,7 +71,7 @@ pub async fn lyrics(req: HttpRequest, info: web::Query) -> Result) -> impl Responder { asset(path.as_str()) } diff --git a/src/utils.rs b/src/utils.rs index 2a513f2..a4c2ee7 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -16,12 +16,3 @@ pub fn borrowed_u8_eq(a: &u8, b: &u8) -> bool { pub fn path_from_url(url: &str) -> String { url.splitn(4, '/').last().unwrap().to_owned() } - -pub fn ensure_path_prefix(prefix: &'static str, path: &str) -> String { - let path = path.trim_start_matches('/'); - if path.starts_with(prefix) { - path.to_string() - } else { - format!("{}/{}", prefix, path) - } -} diff --git a/templates/album.html b/templates/album.html index d1c46d4..e477983 100644 --- a/templates/album.html +++ b/templates/album.html @@ -19,7 +19,7 @@

{{ album.name|e }}

By - + {{ album.artist.name|e }}

@@ -29,7 +29,7 @@

Released on {{ album.release_date_for_display.as_ref().unwrap()|e }}

{% endif %}
- Thumbnail + Thumbnail
{% if album.tracks.is_some() %} diff --git a/templates/artist.html b/templates/artist.html index e61942a..2c7ed08 100644 --- a/templates/artist.html +++ b/templates/artist.html @@ -16,7 +16,7 @@ {% block content %}
- Thumbnail + Thumbnail

{{ artist.name|e }}

{% if artist.alternate_names.is_some() && !artist.alternate_names.as_ref().unwrap().is_empty() %} @@ -31,7 +31,7 @@
{% for social in artist.socials() %} {% endfor %} @@ -46,7 +46,7 @@

Popular Songs

{% for song in artist.popular_songs.as_ref().unwrap() %} {% include "song.html" %} {% endfor %} - Search for songs + Search for songs
{% endif %}
diff --git a/templates/lyrics.html b/templates/lyrics.html index 333b74c..86dd17a 100644 --- a/templates/lyrics.html +++ b/templates/lyrics.html @@ -6,7 +6,7 @@ {% block navright %} {% endblock %} @@ -16,13 +16,13 @@

{{ song.title|e }}

By - + {{ song.primary_artist.name|e }}

{% if song.album.is_some() %}

On - + {{ song.album.as_ref().unwrap().name|e }}

@@ -36,7 +36,7 @@

{{ utils::pretty_format_num(song.stats.pageviews.unwrap())|e }} Views

{% endif %}
- Thumbnail + Thumbnail

{% for verse in verses %} diff --git a/templates/song.html b/templates/song.html index a53716e..a96708d 100644 --- a/templates/song.html +++ b/templates/song.html @@ -1,6 +1,6 @@ - + Thumbnail

{{ song.title|e }}