From c49b8b38f13ed4c432f23bd7a58a92e50a1f38bd Mon Sep 17 00:00:00 2001 From: eevee Date: Tue, 11 Jun 2024 18:05:19 +0300 Subject: [PATCH] genius search only songs --- .../EeveeSpotify/Helpers/URLSessionHelper.swift | 16 ++++++++++++++++ .../DataSources/GeniusLyricsDataSource.swift | 17 ++++++++++++----- .../EeveeSpotify/Lyrics/LyricsRepository.swift | 2 +- .../Models/Genius/GeniusDataResponse.swift | 6 +++--- ...usHitsResponse.swift => GeniusSection.swift} | 4 ++-- .../Models/Genius/GeniusSectionsResponse.swift | 5 +++++ 6 files changed, 39 insertions(+), 11 deletions(-) rename Sources/EeveeSpotify/Lyrics/Models/Genius/{GeniusHitsResponse.swift => GeniusSection.swift} (52%) create mode 100644 Sources/EeveeSpotify/Lyrics/Models/Genius/GeniusSectionsResponse.swift diff --git a/Sources/EeveeSpotify/Helpers/URLSessionHelper.swift b/Sources/EeveeSpotify/Helpers/URLSessionHelper.swift index e8186074..86365f60 100644 --- a/Sources/EeveeSpotify/Helpers/URLSessionHelper.swift +++ b/Sources/EeveeSpotify/Helpers/URLSessionHelper.swift @@ -10,6 +10,22 @@ class URLSessionHelper { self.requestsMap = [:] } + static var DarwinVersion: String { + var sysinfo = utsname() + uname(&sysinfo) + let dv = String( + bytes: Data(bytes: &sysinfo.release, count: Int(_SYS_NAMELEN)), + encoding: .ascii + )!.trimmingCharacters(in: .controlCharacters) + return "Darwin/\(dv)" + } + + static var CFNetworkVersion: String { + let dictionary = Bundle(identifier: "com.apple.CFNetwork")?.infoDictionary! + let version = dictionary?["CFBundleShortVersionString"] as! String + return "CFNetwork/\(version)" + } + func setOrAppend(_ data: Data, for url: URL) { var loadedData = requestsMap[url] ?? Data() loadedData.append(data) diff --git a/Sources/EeveeSpotify/Lyrics/DataSources/GeniusLyricsDataSource.swift b/Sources/EeveeSpotify/Lyrics/DataSources/GeniusLyricsDataSource.swift index df36a2d3..e99d8590 100644 --- a/Sources/EeveeSpotify/Lyrics/DataSources/GeniusLyricsDataSource.swift +++ b/Sources/EeveeSpotify/Lyrics/DataSources/GeniusLyricsDataSource.swift @@ -7,7 +7,11 @@ struct GeniusLyricsDataSource { init() { let configuration = URLSessionConfiguration.default - configuration.httpAdditionalHeaders = ["X-Genius-iOS-Version": "6.19.1"] + configuration.httpAdditionalHeaders = [ + "X-Genius-iOS-Version": "6.21.0", + "X-Genius-Logged-Out": "true", + "User-Agent": "Genius/1109 \(URLSessionHelper.CFNetworkVersion) \(URLSessionHelper.DarwinVersion)" + ] session = URLSession(configuration: configuration) } @@ -50,15 +54,18 @@ struct GeniusLyricsDataSource { return rootResponse.response } - func search(_ query: String) throws -> [GeniusHit] { + func searchSong(_ query: String) throws -> [GeniusHit] { - let data = try perform("/search", query: ["q": query]) + let data = try perform("/search/song", query: ["q": query]) - guard case .hits(let hitsResponse) = data else { + guard + case .sections(let sectionsResponse) = data, + let section = sectionsResponse.sections.first + else { throw LyricsError.DecodingError } - return hitsResponse.hits + return section.hits } func getSongInfo(_ songId: Int) throws -> GeniusSong { diff --git a/Sources/EeveeSpotify/Lyrics/LyricsRepository.swift b/Sources/EeveeSpotify/Lyrics/LyricsRepository.swift index d90a762d..79179db9 100644 --- a/Sources/EeveeSpotify/Lyrics/LyricsRepository.swift +++ b/Sources/EeveeSpotify/Lyrics/LyricsRepository.swift @@ -20,7 +20,7 @@ struct LyricsRepository { case .genius: - let hits = try geniusDataSource.search(query) + let hits = try geniusDataSource.searchSong(query) guard let song = ( hits.first( diff --git a/Sources/EeveeSpotify/Lyrics/Models/Genius/GeniusDataResponse.swift b/Sources/EeveeSpotify/Lyrics/Models/Genius/GeniusDataResponse.swift index fc541dec..1c541082 100644 --- a/Sources/EeveeSpotify/Lyrics/Models/Genius/GeniusDataResponse.swift +++ b/Sources/EeveeSpotify/Lyrics/Models/Genius/GeniusDataResponse.swift @@ -1,14 +1,14 @@ import Foundation enum GeniusDataResponse: Decodable { - case hits(GeniusHitsResponse) + case sections(GeniusSectionsResponse) case song(GeniusSongResponse) init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() - if let hits = try? container.decode(GeniusHitsResponse.self) { - self = .hits(hits) + if let sections = try? container.decode(GeniusSectionsResponse.self) { + self = .sections(sections) } else if let song = try? container.decode(GeniusSongResponse.self) { self = .song(song) diff --git a/Sources/EeveeSpotify/Lyrics/Models/Genius/GeniusHitsResponse.swift b/Sources/EeveeSpotify/Lyrics/Models/Genius/GeniusSection.swift similarity index 52% rename from Sources/EeveeSpotify/Lyrics/Models/Genius/GeniusHitsResponse.swift rename to Sources/EeveeSpotify/Lyrics/Models/Genius/GeniusSection.swift index bc17aadb..93899356 100644 --- a/Sources/EeveeSpotify/Lyrics/Models/Genius/GeniusHitsResponse.swift +++ b/Sources/EeveeSpotify/Lyrics/Models/Genius/GeniusSection.swift @@ -1,5 +1,5 @@ import Foundation -struct GeniusHitsResponse: Decodable { +struct GeniusSection: Decodable { var hits: [GeniusHit] -} \ No newline at end of file +} diff --git a/Sources/EeveeSpotify/Lyrics/Models/Genius/GeniusSectionsResponse.swift b/Sources/EeveeSpotify/Lyrics/Models/Genius/GeniusSectionsResponse.swift new file mode 100644 index 00000000..18840abd --- /dev/null +++ b/Sources/EeveeSpotify/Lyrics/Models/Genius/GeniusSectionsResponse.swift @@ -0,0 +1,5 @@ +import Foundation + +struct GeniusSectionsResponse: Decodable { + var sections: [GeniusSection] +}