]> git.r.bdr.sh - rbdr/lyricli/blobdiff - Sources/lyricli/lyrics_engine.swift
Use swiftsoup for parsing
[rbdr/lyricli] / Sources / lyricli / lyrics_engine.swift
index 085a61c3315abc37851db709a39b47b6162708ae..620e5215c0a3834a271332106e51093e6e345100 100644 (file)
@@ -1,18 +1,17 @@
 import Foundation
-import HTMLEntities
+import SwiftSoup
 
 // Given a track, attempts to fetch the lyrics from lyricswiki
 class LyricsEngine {
 
+    private let clientToken = <GENIUS_CLIENT_TOKEN>
+
     // URL of the API endpoint to use
-    private let apiURL = "https://lyrics.wikia.com/api.php?action=lyrics&func=getSong&fmt=realjson"
+    private let apiURL = "https://api.genius.com/search"
 
     // 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'>(.+)<div"
-
     // The track we'll be looking for
     private let track: Track
 
@@ -24,8 +23,9 @@ class LyricsEngine {
         // Encode the track artist and name and finish building the API call URL
 
         if let artist = track.artist.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
-            if let name: String = track.name.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
-                if let url = URL(string: "\(apiURL)&artist=\(artist)&song=\(name)") {
+            if let name = track.name.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
+
+                if let url = URL(string: "\(apiURL)?access_token=\(clientToken)&q=\(artist)%20\(name)") {
 
                     // We'll lock until the async call is finished
 
@@ -35,7 +35,7 @@ class LyricsEngine {
 
                     // Call the API and unlock when you're done
 
-                    fetchLyricsFromAPI(withURL: url, completionHandler: {lyricsResult -> Void in
+                    searchLyricsUsingAPI(withURL: url, completionHandler: {lyricsResult -> Void in
                         lyrics = lyricsResult
                         requestFinished = true
                         asyncLock.signal()
@@ -60,7 +60,7 @@ class LyricsEngine {
 
     // Fetch the lyrics URL from the API, triggers the request to fetch the
     // lyrics page
-    private func fetchLyricsFromAPI(withURL url: URL, completionHandler: @escaping (String?) -> Void) {
+    private func searchLyricsUsingAPI(withURL url: URL, completionHandler: @escaping (String?) -> Void) {
 
         var apiRequest = URLRequest(url: url)
         apiRequest.httpMethod = "GET"
@@ -73,12 +73,23 @@ class LyricsEngine {
             if let data = data {
                 if let jsonResponse = try? JSONSerialization.jsonObject(with: data) {
                     if let jsonResponse = jsonResponse as? [String: Any] {
-                        if let lyricsUrlString = jsonResponse["url"] as? String {
-                            if let lyricsUrl = URL(string: lyricsUrlString) {
-
-                                // At this point we have a valid wiki url
-                                self.fetchLyricsFromPage(withURL: lyricsUrl, completionHandler: completionHandler)
-                                return
+                        if let response = jsonResponse["response"] as? [String:Any] {
+                            if let hits = response["hits"] as? [Any] {
+                                if let firstHit = hits[0] as? [String: Any] {
+                                    if let firstHitData = firstHit["result"] as? [String: Any] {
+                                        if let lyricsUrlString = firstHitData["url"] as? String {
+                                            if let lyricsUrl = URL(string: lyricsUrlString) {
+
+                                                // At this point we have a valid wiki url
+                                                self.fetchLyricsFromPage(
+                                                    withURL: lyricsUrl,
+                                                    completionHandler: completionHandler
+                                                )
+                                                return
+                                            }
+                                        }
+                                    }
+                                }
                             }
                         }
                     }
@@ -116,31 +127,14 @@ class LyricsEngine {
     // 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.count))
-
-            for match in matches {
-
-                let nsBody = body as NSString
-                let range = match.range(at: 1)
-                let encodedLyrics = nsBody.substring(with: range)
-
-                let decodedLyrics = decodeLyrics(encodedLyrics)
-
-                completionHandler(decodedLyrics)
-                return
-            }
+        do {
+            let document: Document = try SwiftSoup.parse(body)
+            let lyricsBox = try document.select("div[data-lyrics-container=\"true\"]")
+            try lyricsBox.select("br").after("\\n")
+            let lyrics = try lyricsBox.text()
+            completionHandler(lyrics.replacingOccurrences(of: "\\n", with: "\r\n"))
+        } catch {
+            completionHandler(nil)
         }
-
-        completionHandler(nil)
-    }
-
-    // Escapes the HTML entities
-    private func decodeLyrics(_ lyrics: String) -> String {
-
-        let unescapedLyrics = lyrics.htmlUnescape()
-        return unescapedLyrics.replacingOccurrences(of: "<br />", with: "\n")
     }
 }