Skip to content

Commit

Permalink
Match Genius' artist path
Browse files Browse the repository at this point in the history
  • Loading branch information
Insprill committed Apr 3, 2024
1 parent 3752dc7 commit 98527ef
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 20 deletions.
24 changes: 8 additions & 16 deletions src/artist.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -13,7 +12,7 @@ use crate::templates::template;
static GENIUS_IMAGE_URL: &str = "https://images.genius.com/";
static GENIUS_BASE_PATTERN: Lazy<Regex> = lazy_regex!(r#"https?://\w*.?genius\.com/"#);
static GENIUS_ALBUMS_PATTERN: Lazy<Regex> = lazy_regex!(r#"https?://\w*.?genius\.com/albums/"#);
static GENIUS_ARTIST_PATTERN: Lazy<Regex> = lazy_regex!(r#"https?://\w*.?genius\.com/artists/"#);
static GENIUS_ARTIST_PATTERN: Lazy<Regex> = lazy_regex!(r#"https?://\w*.?genius\.com/"#);

#[derive(Template)]
#[template(path = "artist.html")]
Expand All @@ -22,20 +21,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<ArtistQuery>) -> Result<impl Responder> {
let artist_res = genius::extract_data::<GeniusArtistResponse>(&utils::ensure_path_prefix(
"artists", &info.path,
))
.await?;
let mut artist = artist_res.artist;
#[get("/artists/{name}")]
pub async fn artist(req: HttpRequest) -> Result<impl Responder> {
let mut artist = genius::extract_data::<GeniusArtistResponse>(req.path())
.await?
.artist;

artist.popular_songs =
Some(genius::get_artist_songs(artist.id, SortMode::Popularity, MAX_SONGS).await?);
Expand All @@ -59,7 +51,7 @@ fn rewrite_links(html: &str) -> String {
&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_ARTIST_PATTERN.replace_all(&html, ""); // Artists
let html = GENIUS_BASE_PATTERN.replace_all(&html, "/lyrics?path=/"); // Lyrics
html.to_string()
}
2 changes: 1 addition & 1 deletion templates/album.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<div class="song-info">
<p class="title">{{ album.name|e }}</p>
<p class="artist-name">By
<a href="artist?path={{ utils::path_from_url(album.artist.url)|urlencode }}">
<a href="/{{ utils::path_from_url(album.artist.url)|urlencode }}">
<cite>{{ album.artist.name|e }}</cite>
</a>
</p>
Expand Down
4 changes: 2 additions & 2 deletions templates/artist.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<div class="artist-socials">
{% for social in artist.socials() %}
<a class="social {{social.brand|e}}" href="https://{{social.brand}}.com/{{social.name_raw|urlencode}}">
<img class="social-icon" src="icon/{{social.brand}}.svg"/>
<img class="social-icon" src="/icon/{{social.brand}}.svg"/>
<p class="social-name">{{ social.name_formatted|e }}</p>
</a>
{% endfor %}
Expand All @@ -46,7 +46,7 @@ <h1 class="text-centered">Popular Songs</h1>
{% for song in artist.popular_songs.as_ref().unwrap() %}
{% include "song.html" %}
{% endfor %}
<a class="artist-search-songs text-centered" href="search?q={{ artist.name|urlencode }}">Search for songs</a>
<a class="artist-search-songs text-centered" href="/search?q={{ artist.name|urlencode }}">Search for songs</a>
</div>
{% endif %}
</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/lyrics.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<div class="song-info">
<p class="title">{{ song.title|e }}</p>
<p class="artist-name">By
<a href="artist?path={{ utils::path_from_url(song.primary_artist.url)|urlencode }}">
<a href="/{{ utils::path_from_url(song.primary_artist.url)|urlencode }}">
<cite>{{ song.primary_artist.name|e }}</cite>
</a>
</p>
Expand Down

0 comments on commit 98527ef

Please sign in to comment.