]> git.r.bdr.sh - rbdr/lyricli/blobdiff - Sources/lyrics_engine.swift
Merge branch 'release/0.1.0' into develop
[rbdr/lyricli] / Sources / lyrics_engine.swift
index d6b1985908ec8eb8af269b1b3ce0487691da61f1..85e4735eaa00a5027f795285a65b46061d89bcf2 100644 (file)
@@ -1,80 +1,87 @@
 import Foundation
 import HTMLEntities
 
 import Foundation
 import HTMLEntities
 
-/// Looks for lyrics on the internet
+// Given a track, attempts to fetch the lyrics from lyricswiki
 class LyricsEngine {
 
 class LyricsEngine {
 
+    // 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://lyrics.wikia.com/api.php?action=lyrics&func=getSong&fmt=realjson"
+
+    // Method used to call the API
     private let apiMethod = "GET"
     private let apiMethod = "GET"
+
+    // Regular expxression used to find the lyrics in the lyricswiki HTML
     private let lyricsMatcher = "class='lyricbox'>(.+)<div"
 
     private let lyricsMatcher = "class='lyricbox'>(.+)<div"
 
+    // The track we'll be looking for
     private let track: Track
 
     // Fetches the lyrics and returns if found
     private let track: Track
 
     // Fetches the lyrics and returns if found
-
     var lyrics: String? {
     var lyrics: String? {
-        get {
 
 
-            var lyrics: String?
+        var lyrics: String?
 
 
-            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)") {
+        // Encode the track artist and name and finish building the API call URL
 
 
-                        // We'll lock until the async call is finished
+        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)") {
 
 
-                        var requestFinished = false
-                        let asyncLock = NSCondition()
-                        asyncLock.lock()
+                    // We'll lock until the async call is finished
 
 
-                        // Call the API and unlock when you're done
+                    var requestFinished = false
+                    let asyncLock = NSCondition()
+                    asyncLock.lock()
 
 
-                        fetchLyricsFromAPI(withURL: url, completionHandler: {lyricsResult -> Void in
-                            if let lyricsResult = lyricsResult {
-                                lyrics = lyricsResult
-                                requestFinished = true
-                                asyncLock.signal()
-                            }
-                        })
+                    // Call the API and unlock when you're done
 
 
-                        while(!requestFinished) {
-                            asyncLock.wait()
+                    fetchLyricsFromAPI(withURL: url, completionHandler: {lyricsResult -> Void in
+                        if let lyricsResult = lyricsResult {
+                            lyrics = lyricsResult
+                            requestFinished = true
+                            asyncLock.signal()
                         }
                         }
-                        asyncLock.unlock()
+                    })
+
+                    while !requestFinished {
+                        asyncLock.wait()
                     }
                     }
+                    asyncLock.unlock()
                 }
             }
                 }
             }
-
-            return lyrics
         }
         }
+
+        return lyrics
     }
 
     }
 
+    // Initializes with a track
     init(withTrack targetTrack: Track) {
 
         track = targetTrack
     }
 
     init(withTrack targetTrack: Track) {
 
         track = targetTrack
     }
 
-    // Fetch the lyrics from the API and request / parse the page
-
+    // 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) {
 
         var apiRequest = URLRequest(url: url)
         apiRequest.httpMethod = "GET"
 
     private func fetchLyricsFromAPI(withURL url: URL, completionHandler: @escaping (String?) -> Void) {
 
         var apiRequest = URLRequest(url: url)
         apiRequest.httpMethod = "GET"
 
-        let task = URLSession.shared.dataTask(with: apiRequest, completionHandler: {data, response, error -> Void in
+        let task = URLSession.shared.dataTask(with: apiRequest, completionHandler: {data, _, _ -> Void in
 
             // If the response is parseable JSON, and has a url, we'll look for
             // the lyrics in there
 
             if let data = data {
 
             // If the response is parseable JSON, and has a url, we'll look for
             // the lyrics in there
 
             if let data = data {
-                let jsonResponse = try? JSONSerialization.jsonObject(with: data) as! [String: Any]
-                if let jsonResponse = jsonResponse {
-                    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 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
+                            }
                         }
                     }
                 }
                         }
                     }
                 }
@@ -85,14 +92,13 @@ class LyricsEngine {
         task.resume()
     }
 
         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)
         pageRequest.httpMethod = "GET"
 
     private func fetchLyricsFromPage(withURL url: URL, completionHandler: @escaping (String?) -> Void) {
 
         var pageRequest = URLRequest(url: url)
         pageRequest.httpMethod = "GET"
 
-        let task = URLSession.shared.dataTask(with: pageRequest, completionHandler: {data, response, error -> Void in
+        let task = URLSession.shared.dataTask(with: pageRequest, completionHandler: {data, _, _ -> Void in
 
             // If the response is parseable JSON, and has a url, we'll look for
             // the lyrics in there
 
             // If the response is parseable JSON, and has a url, we'll look for
             // the lyrics in there
@@ -109,10 +115,11 @@ class LyricsEngine {
         task.resume()
     }
 
         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) {
 
     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))
 
         if let regex = try? NSRegularExpression(pattern: lyricsMatcher) {
             let matches = regex.matches(in: body, range: NSRange(location: 0, length: body.characters.count))
 
@@ -133,7 +140,6 @@ class LyricsEngine {
     }
 
     // Escapes the HTML entities
     }
 
     // Escapes the HTML entities
-
     private func decodeLyrics(_ lyrics: String) -> String {
 
         let unescapedLyrics = lyrics.htmlUnescape()
     private func decodeLyrics(_ lyrics: String) -> String {
 
         let unescapedLyrics = lyrics.htmlUnescape()