|
| 1 | +// |
| 2 | +// MusicLyricsRequest.swift |
| 3 | +// MusanovaKit |
| 4 | +// |
| 5 | +// Created by Rudrank Riyam on 26/05/24. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +/// A request object used to fetch lyrics for a specified song. |
| 11 | +struct MusicLyricsRequest { |
| 12 | + |
| 13 | + /// The identifier of the song. |
| 14 | + let songID: MusicItemID |
| 15 | + |
| 16 | + /// The privileged developer token used to authorize the request. |
| 17 | + let developerToken: String |
| 18 | + |
| 19 | + /// Initializes a new `MusicLyricsRequest`. |
| 20 | + /// |
| 21 | + /// - Parameters: |
| 22 | + /// - songID: The identifier of the song. |
| 23 | + /// - developerToken: The privileged developer token used to authorize the request. |
| 24 | + init(songID: MusicItemID, developerToken: String) { |
| 25 | + self.songID = songID |
| 26 | + self.developerToken = developerToken |
| 27 | + } |
| 28 | + |
| 29 | + /// Sends the request and returns a response object containing the fetched lyrics. |
| 30 | + /// |
| 31 | + /// - Returns: A `LyricsResponse` object. |
| 32 | + func response(countryCode: String? = nil) async throws -> MusicLyricsResponse { |
| 33 | + let url = try await lyricsEndpointURL(countryCode: countryCode) |
| 34 | + print(url) |
| 35 | + let request = MusicPrivilegedDataRequest(url: url, developerToken: developerToken) |
| 36 | + let response = try await request.response() |
| 37 | + |
| 38 | + |
| 39 | + if let jsonString = String(data: response.data, encoding: .utf8) { |
| 40 | + print("Raw JSON received:") |
| 41 | + print(jsonString) |
| 42 | + } |
| 43 | + |
| 44 | + let lyricsResponse = try JSONDecoder().decode(MusicLyricsResponse.self, from: response.data) |
| 45 | + return lyricsResponse |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +extension MusicLyricsRequest { |
| 50 | + internal func lyricsEndpointURL(countryCode: String? = nil) async throws -> URL { |
| 51 | + var components = AppleMusicAMPURLComponents() |
| 52 | + |
| 53 | + let resolvedCountryCode: String |
| 54 | + if let countryCode = countryCode { |
| 55 | + resolvedCountryCode = countryCode |
| 56 | + } else { |
| 57 | + resolvedCountryCode = try await MusicDataRequest.currentCountryCode |
| 58 | + } |
| 59 | + |
| 60 | + components.path = "/catalog/\(resolvedCountryCode)/songs/\(songID.rawValue)/syllable-lyrics" |
| 61 | + |
| 62 | + guard let url = components.url else { |
| 63 | + throw URLError(.badURL) |
| 64 | + } |
| 65 | + |
| 66 | + return url |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +public extension MCatalog { |
| 71 | + /// Fetches and parses the lyrics for a specified song. |
| 72 | + /// |
| 73 | + /// This method performs the following steps: |
| 74 | + /// 1. Creates a `MusicLyricsRequest` using the provided song ID and developer token. |
| 75 | + /// 2. Sends the request to fetch the lyrics data. |
| 76 | + /// 3. Extracts the TTML (Timed Text Markup Language) string from the response. |
| 77 | + /// 4. Parses the TTML string into a structured `LyricParagraphs` object. |
| 78 | + /// |
| 79 | + /// - Parameters: |
| 80 | + /// - song: The `Song` object representing the song for which to fetch lyrics. |
| 81 | + /// This object must have a valid `id` property. |
| 82 | + /// - developerToken: A string containing the developer token used to authorize the request. |
| 83 | + /// This token must be valid and have the necessary permissions to fetch lyrics. |
| 84 | + /// |
| 85 | + /// - Returns: A `LyricParagraphs` object containing the parsed lyrics. |
| 86 | + /// This object is an array of `LyricParagraph` structures, each representing |
| 87 | + /// a section of the song's lyrics. |
| 88 | + /// |
| 89 | + /// - Throws: This method can throw errors in the following situations: |
| 90 | + /// - `MusicLyricsRequest.Error`: If there's an error creating or sending the lyrics request. |
| 91 | + /// - `DecodingError`: If the response cannot be properly decoded into the expected format. |
| 92 | + /// - `URLError`: If there's a network-related error during the request. |
| 93 | + /// - `LyricsParser.Error`: If there's an error parsing the TTML string into `LyricParagraphs`. |
| 94 | + /// |
| 95 | + /// - Note: If no lyrics are found for the specified song, this method returns an empty `LyricParagraphs` array |
| 96 | + /// instead of throwing an error. |
| 97 | + /// |
| 98 | + /// - Important: Ensure that you have the necessary permissions and a valid developer token |
| 99 | + /// before calling this method. Unauthorized or incorrect usage may result in errors or empty results. |
| 100 | + static func lyrics(for song: Song, developerToken: String) async throws -> LyricParagraphs { |
| 101 | + let request = MusicLyricsRequest(songID: song.id, developerToken: developerToken) |
| 102 | + let response = try await request.response() |
| 103 | + |
| 104 | + guard let lyricsString = response.data.first?.attributes.ttml else { |
| 105 | + return [] |
| 106 | + } |
| 107 | + |
| 108 | + let parser = LyricsParser() |
| 109 | + return parser.parse(lyricsString) |
| 110 | + } |
| 111 | +} |
0 commit comments