diff options
| author | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2023-04-10 15:00:55 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2023-04-10 15:00:55 +0200 |
| commit | 3a398bc031f1a1659073208e83d778a357a8243a (patch) | |
| tree | abbef70e09ae49e14d0f921af0358ac6ae3fc21c /Sources | |
| parent | 6e94967396373626e5f6ee228fbba5fcb111f3cb (diff) | |
Use swiftsoup for parsing
Diffstat (limited to 'Sources')
| -rw-r--r-- | Sources/lyricli/lyrics_engine.swift | 65 |
1 files changed, 25 insertions, 40 deletions
diff --git a/Sources/lyricli/lyrics_engine.swift b/Sources/lyricli/lyrics_engine.swift index f0a02db..620e521 100644 --- a/Sources/lyricli/lyrics_engine.swift +++ b/Sources/lyricli/lyrics_engine.swift @@ -1,4 +1,5 @@ import Foundation +import SwiftSoup // Given a track, attempts to fetch the lyrics from lyricswiki class LyricsEngine { @@ -11,9 +12,6 @@ class LyricsEngine { // 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 @@ -25,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)&q=\(artist) \(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 @@ -74,18 +73,20 @@ class LyricsEngine { if let data = data { if let jsonResponse = try? JSONSerialization.jsonObject(with: data) { if let jsonResponse = jsonResponse as? [String: Any] { - if let hits = jsonResponse["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) { + 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 + // At this point we have a valid wiki url + self.fetchLyricsFromPage( + withURL: lyricsUrl, + completionHandler: completionHandler + ) + return + } } } } @@ -126,30 +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 and HTML - private func decodeLyrics(_ lyrics: String) -> String { - - return lyrics } } |