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