]> git.r.bdr.sh - rbdr/lyricli/blame_incremental - Sources/lyrics_engine.swift
Use before_deploy to generate docs
[rbdr/lyricli] / Sources / lyrics_engine.swift
... / ...
CommitLineData
1import Foundation
2import HTMLEntities
3
4/// Looks for lyrics on the internet
5class LyricsEngine {
6
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
14
15 var lyrics: String? {
16 var lyrics: String?
17
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)") {
21
22 // We'll lock until the async call is finished
23
24 var requestFinished = false
25 let asyncLock = NSCondition()
26 asyncLock.lock()
27
28 // Call the API and unlock when you're done
29
30 fetchLyricsFromAPI(withURL: url, completionHandler: {lyricsResult -> Void in
31 if let lyricsResult = lyricsResult {
32 lyrics = lyricsResult
33 requestFinished = true
34 asyncLock.signal()
35 }
36 })
37
38 while !requestFinished {
39 asyncLock.wait()
40 }
41 asyncLock.unlock()
42 }
43 }
44 }
45
46 return lyrics
47 }
48
49 init(withTrack targetTrack: Track) {
50
51 track = targetTrack
52 }
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
61 let task = URLSession.shared.dataTask(with: apiRequest, completionHandler: {data, _, _ -> Void in
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 {
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 }
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
93 let task = URLSession.shared.dataTask(with: pageRequest, completionHandler: {data, _, _ -> Void in
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 }
140}