Skip to content

Commit

Permalink
Lazy initialize selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Insprill committed May 7, 2023
1 parent a3a64f1 commit e558704
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/lyrics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use actix_web::{get, web, Responder, Result};
use askama::Template;
use futures::future;
use once_cell::sync::Lazy;
use scraper::{Html, Selector};
use serde::Deserialize;

Expand All @@ -9,6 +10,11 @@ use crate::genius::{self, GeniusApi};
use crate::templates::template;
use crate::utils;

static SONG_ID_SELECTOR: Lazy<Selector> =
Lazy::new(|| Selector::parse("meta[property='twitter:app:url:iphone']").unwrap());
static LYRIC_SELECTOR: Lazy<Selector> =
Lazy::new(|| Selector::parse("div[data-lyrics-container=true]").unwrap());

struct Verse {
title: String,
lyrics: Vec<String>,
Expand Down Expand Up @@ -60,9 +66,8 @@ pub async fn lyrics(info: web::Query<LyricsQuery>) -> Result<impl Responder> {
}

fn get_song_id(document: &Html) -> crate::Result<u32> {
let parser = &Selector::parse("meta[property='twitter:app:url:iphone']").unwrap();
let meta = document
.select(parser)
.select(&SONG_ID_SELECTOR)
.next()
.ok_or("Failed to find meta tag with song ID")?;
let id = meta
Expand All @@ -75,9 +80,7 @@ fn get_song_id(document: &Html) -> crate::Result<u32> {
}

fn scrape_lyrics(document: &Html) -> Vec<Verse> {
let parser = &Selector::parse("div[data-lyrics-container=true]").unwrap();

let text_iter = document.select(parser).flat_map(|x| x.text());
let text_iter = document.select(&LYRIC_SELECTOR).flat_map(|x| x.text());

let mut verses = Vec::with_capacity(text_iter.size_hint().0);

Expand Down

0 comments on commit e558704

Please sign in to comment.