]> git.r.bdr.sh - rbdr/lyricli/blame - Sources/lyrics_engine.swift
Add doc generation to linter
[rbdr/lyricli] / Sources / lyrics_engine.swift
CommitLineData
85d0536f
BB
1import Foundation
2import HTMLEntities
3
a968bff7
BB
4/// Looks for lyrics on the internet
5class LyricsEngine {
6
85d0536f
BB
7 private let apiURL = "https://lyrics.wikia.com/api.php?action=lyrics&func=getSong&fmt=realjson"
8 private let apiMethod = "GET"
9 private let lyricsMatcher = "class='lyricbox'>(.+)<div"
10
11 private let track: Track
12
13 // Fetches the lyrics and returns if found
a968bff7
BB
14
15 var lyrics: String? {
1263f62c 16 var lyrics: String?
85d0536f 17
1263f62c
BB
18 if let artist = track.artist.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
19 if let name: String = track.name.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
20 if let url = URL(string: "\(apiURL)&artist=\(artist)&song=\(name)") {
85d0536f 21
1263f62c 22 // We'll lock until the async call is finished
85d0536f 23
1263f62c
BB
24 var requestFinished = false
25 let asyncLock = NSCondition()
26 asyncLock.lock()
85d0536f 27
1263f62c 28 // Call the API and unlock when you're done
85d0536f 29
1263f62c
BB
30 fetchLyricsFromAPI(withURL: url, completionHandler: {lyricsResult -> Void in
31 if let lyricsResult = lyricsResult {
32 lyrics = lyricsResult
33 requestFinished = true
34 asyncLock.signal()
85d0536f 35 }
1263f62c
BB
36 })
37
38 while !requestFinished {
39 asyncLock.wait()
85d0536f 40 }
1263f62c 41 asyncLock.unlock()
85d0536f 42 }
a968bff7 43 }
a968bff7 44 }
1263f62c
BB
45
46 return lyrics
a968bff7
BB
47 }
48
49 init(withTrack targetTrack: Track) {
50
51 track = targetTrack
52 }
85d0536f
BB
53
54 // Fetch the lyrics from the API and request / parse the page
55
56 private func fetchLyricsFromAPI(withURL url: URL, completionHandler: @escaping (String?) -> Void) {
57
58 var apiRequest = URLRequest(url: url)
59 apiRequest.httpMethod = "GET"
60
1263f62c 61 let task = URLSession.shared.dataTask(with: apiRequest, completionHandler: {data, _, _ -> Void in
85d0536f
BB
62
63 // If the response is parseable JSON, and has a url, we'll look for
64 // the lyrics in there
65
66 if let data = data {
1263f62c
BB
67 if let jsonResponse = try? JSONSerialization.jsonObject(with: data) {
68 if let jsonResponse = jsonResponse as? [String: Any] {
69 if let lyricsUrlString = jsonResponse["url"] as? String {
70 if let lyricsUrl = URL(string: lyricsUrlString) {
71
72 // At this point we have a valid wiki url
73 self.fetchLyricsFromPage(withURL: lyricsUrl, completionHandler: completionHandler)
74 return
75 }
85d0536f
BB
76 }
77 }
78 }
79 }
80
81 completionHandler(nil)
82 })
83 task.resume()
84 }
85
86 // Fetch the lyrics from the page and parse the page
87
88 private func fetchLyricsFromPage(withURL url: URL, completionHandler: @escaping (String?) -> Void) {
89
90 var pageRequest = URLRequest(url: url)
91 pageRequest.httpMethod = "GET"
92
1263f62c 93 let task = URLSession.shared.dataTask(with: pageRequest, completionHandler: {data, _, _ -> Void in
85d0536f
BB
94
95 // If the response is parseable JSON, and has a url, we'll look for
96 // the lyrics in there
97
98 if let data = data {
99 if let htmlBody = String(data: data, encoding: String.Encoding.utf8) {
100 self.parseHtmlBody(htmlBody, completionHandler: completionHandler)
101 return
102 }
103 }
104
105 completionHandler(nil)
106 })
107 task.resume()
108 }
109
110 // Parses the wiki to obtain the lyrics
111
112 private func parseHtmlBody(_ body: String, completionHandler: @escaping (String?) -> Void) {
113
114 if let regex = try? NSRegularExpression(pattern: lyricsMatcher) {
115 let matches = regex.matches(in: body, range: NSRange(location: 0, length: body.characters.count))
116
117 for match in matches {
118
119 let nsBody = body as NSString
120 let range = match.rangeAt(1)
121 let encodedLyrics = nsBody.substring(with: range)
122
123 let decodedLyrics = decodeLyrics(encodedLyrics)
124
125 completionHandler(decodedLyrics)
126 return
127 }
128 }
129
130 completionHandler(nil)
131 }
132
133 // Escapes the HTML entities
134
135 private func decodeLyrics(_ lyrics: String) -> String {
136
137 let unescapedLyrics = lyrics.htmlUnescape()
138 return unescapedLyrics.replacingOccurrences(of: "<br />", with: "\n")
139 }
a968bff7 140}