mirror of
https://github.com/whoeevee/EeveeSpotifyReborn.git
synced 2026-01-09 00:23:20 +01:00
genius search only songs
This commit is contained in:
@@ -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)
|
||||
|
||||
+2
-2
@@ -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]
|
||||
}
|
||||
Reference in New Issue
Block a user