Skip to content

Commit

Permalink
Match Genius' lyric path
Browse files Browse the repository at this point in the history
  • Loading branch information
Insprill committed Apr 3, 2024
1 parent 9a63aa9 commit fd527a2
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 16 deletions.
6 changes: 1 addition & 5 deletions src/artist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ 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/"#);
static GENIUS_ARTIST_PATTERN: Lazy<Regex> = lazy_regex!(r#"https?://\w*.?genius\.com/"#);

#[derive(Template)]
#[template(path = "artist.html")]
Expand Down Expand Up @@ -50,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, ""); // Albums
let html = GENIUS_ARTIST_PATTERN.replace_all(&html, ""); // 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()
}
15 changes: 8 additions & 7 deletions src/lyrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,35 @@ struct Verse {

#[derive(Template)]
#[template(path = "lyrics.html")]
struct LyricsTemplate {
struct LyricsTemplate<'a> {
settings: Settings,
verses: Vec<Verse>,
query: LyricsQuery,
path: &'a str,
song: GeniusSong,
}

#[derive(Debug, Deserialize)]
pub struct LyricsQuery {
id: Option<u32>,
path: String,
}

#[get("/lyrics")]
#[get("/{path:.*}")]
pub async fn lyrics(req: HttpRequest, info: web::Query<LyricsQuery>) -> Result<impl Responder> {
let document: Html;
let song: GeniusSong;

let path = 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?;
Expand All @@ -65,7 +66,7 @@ pub async fn lyrics(req: HttpRequest, info: web::Query<LyricsQuery>) -> Result<i
LyricsTemplate {
settings: settings_from_req(&req),
verses,
query: info.0,
path,
song,
},
))
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ async fn main() -> std::io::Result<()> {
.service(api::image)
.service(artist::artist)
.service(home::home)
.service(lyrics::lyrics)
.service(search::search)
.service(settings::settings)
.service(settings::settings_form)
Expand All @@ -111,6 +110,8 @@ async fn main() -> std::io::Result<()> {
.service(resource::style_theme)
.service(resource::icon)
.service(resource::font)
// Must be last!
.service(lyrics::lyrics)
});

if args.keep_alive_timeout > 0.0 {
Expand Down
2 changes: 1 addition & 1 deletion src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use include_dir::{include_dir, Dir};

const STATIC_RESOURCES: Dir = include_dir!("$CARGO_MANIFEST_DIR/static");

#[get("{resource}")]
#[get("{resource}.{ext}")]
pub async fn resource(path: web::Path<String>) -> impl Responder {
asset(path.as_str())
}
Expand Down
2 changes: 1 addition & 1 deletion templates/lyrics.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

{% block navright %}
<div class="nav-item.right">
<a class="external-link" href="https://genius.com{{ query.path|urlencode }}">View on Genius</a>
<a class="external-link" href="https://genius.com{{ path|urlencode }}">View on Genius</a>
</div>
{% endblock %}

Expand Down
2 changes: 1 addition & 1 deletion templates/song.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<a class="song" href="lyrics?path={{ song.path|urlencode }}&id={{ song.id|urlencode }}">
<a class="song" href="{{ song.path|urlencode }}?id={{ song.id|urlencode }}">
<img class="song-thumbnail"
src="/api/image?url={{ song.song_art_image_thumbnail_url|urlencode }}&size=150"
alt="Thumbnail"
Expand Down

0 comments on commit fd527a2

Please sign in to comment.