genius search only songs

This commit is contained in:
eevee
2024-06-11 18:05:19 +03:00
parent aed800429c
commit c49b8b38f1
6 changed files with 39 additions and 11 deletions
@@ -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)
@@ -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 {
@@ -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(
@@ -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)
@@ -1,5 +1,5 @@
import Foundation
struct GeniusHitsResponse: Decodable {
struct GeniusSection: Decodable {
var hits: [GeniusHit]
}
}
@@ -0,0 +1,5 @@
import Foundation
struct GeniusSectionsResponse: Decodable {
var sections: [GeniusSection]
}