From: Ruben Beltran del Rio Date: Mon, 10 Apr 2023 13:00:55 +0000 (+0200) Subject: Use swiftsoup for parsing X-Git-Tag: 2.0.0~2 X-Git-Url: https://git.r.bdr.sh/rbdr/lyricli/commitdiff_plain/3a398bc031f1a1659073208e83d778a357a8243a?ds=sidebyside;hp=6e94967396373626e5f6ee228fbba5fcb111f3cb Use swiftsoup for parsing --- diff --git a/Makefile b/Makefile index 8428b44..e535bdb 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ build: swift build --build-path $(build_path) --configuration $(configuration) install: build - cp $(source_binary_path) $(install_binary_path) + sudo cp $(source_binary_path) $(install_binary_path) test: build swift test diff --git a/Package.resolved b/Package.resolved index 4d93f8f..54140ee 100644 --- a/Package.resolved +++ b/Package.resolved @@ -8,6 +8,15 @@ "revision" : "fee6933f37fde9a5e12a1e4aeaa93fe60116ff2a", "version" : "1.2.2" } + }, + { + "identity" : "swiftsoup", + "kind" : "remoteSourceControl", + "location" : "https://github.com/scinfu/SwiftSoup.git", + "state" : { + "revision" : "f707b8680cddb96dc1855632340a572ef37bbb98", + "version" : "2.5.3" + } } ], "version" : 2 diff --git a/Package.swift b/Package.swift index 2f49d10..508b5fb 100644 --- a/Package.swift +++ b/Package.swift @@ -5,12 +5,18 @@ import PackageDescription let package = Package( name: "lyricli", dependencies: [ + /// HTML Parsing + .package(url: "https://github.com/scinfu/SwiftSoup.git", from: "2.5.3"), + /// 🚩 Command Line Arguments .package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.2.2") ], targets: [ .executableTarget( name: "lyricli", - dependencies: [.product(name: "ArgumentParser", package: "swift-argument-parser")]) + dependencies: [ + .product(name: "SwiftSoup", package: "SwiftSoup"), + .product(name: "ArgumentParser", package: "swift-argument-parser") + ]) ] ) 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'>(.+) 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 } }