X-Git-Url: https://git.r.bdr.sh/rbdr/lyricli/blobdiff_plain/1263f62c5c6379f11e19eb184ffedf5889390b70..382e1e1ab249c7692be6924167ed02a663ab5634:/Sources/lyrics_engine.swift?ds=sidebyside
diff --git a/Sources/lyrics_engine.swift b/Sources/lyrics_engine.swift
index 9741a2c..85e4735 100644
--- a/Sources/lyrics_engine.swift
+++ b/Sources/lyrics_engine.swift
@@ -1,20 +1,28 @@
import Foundation
import HTMLEntities
-/// Looks for lyrics on the internet
+// Given a track, attempts to fetch the lyrics from lyricswiki
class LyricsEngine {
+ // URL of the API endpoint to use
private let apiURL = "https://lyrics.wikia.com/api.php?action=lyrics&func=getSong&fmt=realjson"
+
+ // Method used to call the API
private let apiMethod = "GET"
+
+ // Regular expxression used to find the lyrics in the lyricswiki HTML
private let lyricsMatcher = "class='lyricbox'>(.+)
Void) {
var apiRequest = URLRequest(url: url)
@@ -83,8 +92,7 @@ class LyricsEngine {
task.resume()
}
- // Fetch the lyrics from the page and parse the page
-
+ // Fetch the lyrics from the page and send it to the parser
private func fetchLyricsFromPage(withURL url: URL, completionHandler: @escaping (String?) -> Void) {
var pageRequest = URLRequest(url: url)
@@ -107,10 +115,11 @@ class LyricsEngine {
task.resume()
}
- // Parses the wiki to obtain the lyrics
-
+ // Parses the wiki to find the lyrics, decodes the lyrics object
private func parseHtmlBody(_ body: String, completionHandler: @escaping (String?) -> Void) {
+ // Look for the lyrics lightbox
+
if let regex = try? NSRegularExpression(pattern: lyricsMatcher) {
let matches = regex.matches(in: body, range: NSRange(location: 0, length: body.characters.count))
@@ -131,7 +140,6 @@ class LyricsEngine {
}
// Escapes the HTML entities
-
private func decodeLyrics(_ lyrics: String) -> String {
let unescapedLyrics = lyrics.htmlUnescape()