aboutsummaryrefslogtreecommitdiff
path: root/Sources/lyrics_engine.swift
blob: 9741a2c63d65393ebeadc27fd63c5d5829455bd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import Foundation
import HTMLEntities

/// Looks for lyrics on the internet
class LyricsEngine {

    private let apiURL = "https://lyrics.wikia.com/api.php?action=lyrics&func=getSong&fmt=realjson"
    private let apiMethod = "GET"
    private let lyricsMatcher = "class='lyricbox'>(.+)<div"

    private let track: Track

    // Fetches the lyrics and returns if found

    var lyrics: String? {
        var lyrics: String?

        if let artist = track.artist.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
            if let name: String = track.name.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
                if let url = URL(string: "\(apiURL)&artist=\(artist)&song=\(name)") {

                    // We'll lock until the async call is finished

                    var requestFinished = false
                    let asyncLock = NSCondition()
                    asyncLock.lock()

                    // Call the API and unlock when you're done

                    fetchLyricsFromAPI(withURL: url, completionHandler: {lyricsResult -> Void in
                        if let lyricsResult = lyricsResult {
                            lyrics = lyricsResult
                            requestFinished = true
                            asyncLock.signal()
                        }
                    })

                    while !requestFinished {
                        asyncLock.wait()
                    }
                    asyncLock.unlock()
                }
            }
        }

        return lyrics
    }

    init(withTrack targetTrack: Track) {

        track = targetTrack
    }

    // Fetch the lyrics from the API and request / parse the page

    private func fetchLyricsFromAPI(withURL url: URL, completionHandler: @escaping (String?) -> Void) {

        var apiRequest = URLRequest(url: url)
        apiRequest.httpMethod = "GET"

        let task = URLSession.shared.dataTask(with: apiRequest, completionHandler: {data, _, _ -> Void in

            // If the response is parseable JSON, and has a url, we'll look for
            // the lyrics in there

            if let data = data {
                if let jsonResponse = try? JSONSerialization.jsonObject(with: data) {
                    if let jsonResponse = jsonResponse as? [String: Any] {
                        if let lyricsUrlString = jsonResponse["url"] as? String {
                            if let lyricsUrl = URL(string: lyricsUrlString) {

                                // At this point we have a valid wiki url
                                self.fetchLyricsFromPage(withURL: lyricsUrl, completionHandler: completionHandler)
                                return
                            }
                        }
                    }
                }
            }

            completionHandler(nil)
        })
        task.resume()
    }

    // Fetch the lyrics from the page and parse the page

    private func fetchLyricsFromPage(withURL url: URL, completionHandler: @escaping (String?) -> Void) {

        var pageRequest = URLRequest(url: url)
        pageRequest.httpMethod = "GET"

        let task = URLSession.shared.dataTask(with: pageRequest, completionHandler: {data, _, _ -> Void in

            // If the response is parseable JSON, and has a url, we'll look for
            // the lyrics in there

            if let data = data {
                if let htmlBody = String(data: data, encoding: String.Encoding.utf8) {
                    self.parseHtmlBody(htmlBody, completionHandler: completionHandler)
                    return
                }
            }

            completionHandler(nil)
        })
        task.resume()
    }

    // Parses the wiki to obtain the lyrics

    private func parseHtmlBody(_ body: String, completionHandler: @escaping (String?) -> Void) {

        if let regex = try? NSRegularExpression(pattern: lyricsMatcher) {
            let matches = regex.matches(in: body, range: NSRange(location: 0, length: body.characters.count))

            for match in matches {

                let nsBody = body as NSString
                let range = match.rangeAt(1)
                let encodedLyrics = nsBody.substring(with: range)

                let decodedLyrics = decodeLyrics(encodedLyrics)

                completionHandler(decodedLyrics)
                return
            }
        }

        completionHandler(nil)
    }

    // Escapes the HTML entities

    private func decodeLyrics(_ lyrics: String) -> String {

        let unescapedLyrics = lyrics.htmlUnescape()
        return unescapedLyrics.replacingOccurrences(of: "<br />", with: "\n")
    }
}