]>
Commit | Line | Data |
---|---|---|
85d0536f BB |
1 | import Foundation |
2 | import HTMLEntities | |
3 | ||
d852b84e | 4 | // Given a track, attempts to fetch the lyrics from lyricswiki |
a968bff7 BB |
5 | class LyricsEngine { |
6 | ||
1b8bdf0f RBR |
7 | private let clientToken = "_-P6qiz2dPDMaRUih-VxSS--PBYA4OtWrHiTgVY7Qd3lMss_oewL04FX8lmh37ma" |
8 | ||
d852b84e | 9 | // URL of the API endpoint to use |
1b8bdf0f | 10 | private let apiURL = "https://api.genius.com/search" |
d852b84e BB |
11 | |
12 | // Method used to call the API | |
85d0536f | 13 | private let apiMethod = "GET" |
d852b84e BB |
14 | |
15 | // Regular expxression used to find the lyrics in the lyricswiki HTML | |
85d0536f BB |
16 | private let lyricsMatcher = "class='lyricbox'>(.+)<div" |
17 | ||
d852b84e | 18 | // The track we'll be looking for |
85d0536f BB |
19 | private let track: Track |
20 | ||
21 | // Fetches the lyrics and returns if found | |
a968bff7 | 22 | var lyrics: String? { |
d852b84e | 23 | |
1263f62c | 24 | var lyrics: String? |
85d0536f | 25 | |
d852b84e BB |
26 | // Encode the track artist and name and finish building the API call URL |
27 | ||
1263f62c BB |
28 | if let artist = track.artist.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) { |
29 | if let name: String = track.name.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) { | |
1b8bdf0f | 30 | if let url = URL(string: "\(apiURL)&q=\(artist) \(name)") { |
85d0536f | 31 | |
1263f62c | 32 | // We'll lock until the async call is finished |
85d0536f | 33 | |
1263f62c BB |
34 | var requestFinished = false |
35 | let asyncLock = NSCondition() | |
36 | asyncLock.lock() | |
85d0536f | 37 | |
1263f62c | 38 | // Call the API and unlock when you're done |
85d0536f | 39 | |
1b8bdf0f | 40 | searchLyricsUsingAPI(withURL: url, completionHandler: {lyricsResult -> Void in |
d0705bff BB |
41 | lyrics = lyricsResult |
42 | requestFinished = true | |
43 | asyncLock.signal() | |
1263f62c BB |
44 | }) |
45 | ||
46 | while !requestFinished { | |
47 | asyncLock.wait() | |
85d0536f | 48 | } |
1263f62c | 49 | asyncLock.unlock() |
85d0536f | 50 | } |
a968bff7 | 51 | } |
a968bff7 | 52 | } |
1263f62c BB |
53 | |
54 | return lyrics | |
a968bff7 BB |
55 | } |
56 | ||
d852b84e | 57 | // Initializes with a track |
a968bff7 BB |
58 | init(withTrack targetTrack: Track) { |
59 | ||
60 | track = targetTrack | |
61 | } | |
85d0536f | 62 | |
d852b84e BB |
63 | // Fetch the lyrics URL from the API, triggers the request to fetch the |
64 | // lyrics page | |
1b8bdf0f | 65 | private func searchLyricsUsingAPI(withURL url: URL, completionHandler: @escaping (String?) -> Void) { |
85d0536f BB |
66 | |
67 | var apiRequest = URLRequest(url: url) | |
68 | apiRequest.httpMethod = "GET" | |
69 | ||
1263f62c | 70 | let task = URLSession.shared.dataTask(with: apiRequest, completionHandler: {data, _, _ -> Void in |
85d0536f BB |
71 | |
72 | // If the response is parseable JSON, and has a url, we'll look for | |
73 | // the lyrics in there | |
74 | ||
75 | if let data = data { | |
1263f62c BB |
76 | if let jsonResponse = try? JSONSerialization.jsonObject(with: data) { |
77 | if let jsonResponse = jsonResponse as? [String: Any] { | |
1b8bdf0f RBR |
78 | if let hits = jsonResponse["hits"] as? [Any] { |
79 | if let firstHit = hits[0] as? [String: Any] { | |
80 | if let firstHitData = firstHit["result"] as? [String: Any] { | |
81 | if let lyricsUrlString = firstHitData["url"] as? String { | |
82 | if let lyricsUrl = URL(string: lyricsUrlString) { | |
83 | ||
84 | // At this point we have a valid wiki url | |
85 | self.fetchLyricsFromPage(withURL: lyricsUrl, completionHandler: completionHandler) | |
86 | return | |
87 | } | |
88 | } | |
89 | } | |
1263f62c | 90 | } |
85d0536f BB |
91 | } |
92 | } | |
93 | } | |
94 | } | |
95 | ||
96 | completionHandler(nil) | |
97 | }) | |
98 | task.resume() | |
99 | } | |
100 | ||
d852b84e | 101 | // Fetch the lyrics from the page and send it to the parser |
85d0536f BB |
102 | private func fetchLyricsFromPage(withURL url: URL, completionHandler: @escaping (String?) -> Void) { |
103 | ||
104 | var pageRequest = URLRequest(url: url) | |
105 | pageRequest.httpMethod = "GET" | |
106 | ||
1263f62c | 107 | let task = URLSession.shared.dataTask(with: pageRequest, completionHandler: {data, _, _ -> Void in |
85d0536f BB |
108 | |
109 | // If the response is parseable JSON, and has a url, we'll look for | |
110 | // the lyrics in there | |
111 | ||
112 | if let data = data { | |
113 | if let htmlBody = String(data: data, encoding: String.Encoding.utf8) { | |
114 | self.parseHtmlBody(htmlBody, completionHandler: completionHandler) | |
115 | return | |
116 | } | |
117 | } | |
118 | ||
119 | completionHandler(nil) | |
120 | }) | |
121 | task.resume() | |
122 | } | |
123 | ||
d852b84e | 124 | // Parses the wiki to find the lyrics, decodes the lyrics object |
85d0536f BB |
125 | private func parseHtmlBody(_ body: String, completionHandler: @escaping (String?) -> Void) { |
126 | ||
d852b84e BB |
127 | // Look for the lyrics lightbox |
128 | ||
85d0536f | 129 | if let regex = try? NSRegularExpression(pattern: lyricsMatcher) { |
fdafe0d4 | 130 | let matches = regex.matches(in: body, range: NSRange(location: 0, length: body.count)) |
85d0536f BB |
131 | |
132 | for match in matches { | |
133 | ||
134 | let nsBody = body as NSString | |
fdafe0d4 | 135 | let range = match.range(at: 1) |
85d0536f BB |
136 | let encodedLyrics = nsBody.substring(with: range) |
137 | ||
138 | let decodedLyrics = decodeLyrics(encodedLyrics) | |
139 | ||
140 | completionHandler(decodedLyrics) | |
141 | return | |
142 | } | |
143 | } | |
144 | ||
145 | completionHandler(nil) | |
146 | } | |
147 | ||
148 | // Escapes the HTML entities | |
85d0536f BB |
149 | private func decodeLyrics(_ lyrics: String) -> String { |
150 | ||
151 | let unescapedLyrics = lyrics.htmlUnescape() | |
152 | return unescapedLyrics.replacingOccurrences(of: "<br />", with: "\n") | |
153 | } | |
a968bff7 | 154 | } |