Skip to content

Commit

Permalink
genius search only songs
Browse files Browse the repository at this point in the history
  • Loading branch information
whoeevee committed Jun 11, 2024
1 parent aed8004 commit c49b8b3
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 11 deletions.
16 changes: 16 additions & 0 deletions Sources/EeveeSpotify/Helpers/URLSessionHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion Sources/EeveeSpotify/Lyrics/LyricsRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Foundation

struct GeniusHitsResponse: Decodable {
struct GeniusSection: Decodable {
var hits: [GeniusHit]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

struct GeniusSectionsResponse: Decodable {
var sections: [GeniusSection]
}

0 comments on commit c49b8b3

Please sign in to comment.