-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from thatcherclough/spotify
Added support for Spotify
- Loading branch information
Showing
236 changed files
with
2,784 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
.DS_Store | ||
/Pods | ||
*xcworkspace/xcuserdata/* | ||
/videos/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
// | ||
|
||
#import <HueSDK/HueSDK.h> | ||
#import <SpotifyiOS/SpotifyiOS.h> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file removed
BIN
-68.3 KB
CoverFlow.xcworkspace/xcuserdata/thatcher.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
6 changes: 0 additions & 6 deletions
6
CoverFlow.xcworkspace/xcuserdata/thatcher.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// | ||
// AppleMusicController.swift | ||
// CoverFlow | ||
// | ||
// Created by Thatcher Clough on 11/14/20. | ||
// | ||
|
||
import Foundation | ||
import StoreKit | ||
import MediaPlayer | ||
import Keys | ||
|
||
class AppleMusicController { | ||
|
||
// MARK: Constructor and variables | ||
|
||
var apiKey: String! | ||
var countryCode: String! | ||
let player = MPMusicPlayerController.systemMusicPlayer | ||
|
||
init(apiKey: String) { | ||
self.apiKey = apiKey | ||
getCountryCode() | ||
} | ||
|
||
func getCountryCode() { | ||
DispatchQueue.global(qos: .background).async { | ||
let controller = SKCloudServiceController() | ||
controller.requestStorefrontCountryCode { countryCode, error in | ||
if countryCode == nil || error != nil { | ||
self.countryCode = "us" | ||
} else { | ||
self.countryCode = countryCode | ||
} | ||
} | ||
} | ||
} | ||
|
||
// MARK: Functions | ||
|
||
func getCurrentAlbumName() -> String { | ||
let nowPlaying: MPMediaItem? = player.nowPlayingItem | ||
let albumName = nowPlaying?.albumTitle | ||
if nowPlaying == nil || albumName == nil { | ||
return "nil" | ||
} else { | ||
return albumName! | ||
} | ||
} | ||
|
||
func getCurrentArtistName() -> String { | ||
let nowPlaying: MPMediaItem? = player.nowPlayingItem | ||
let artistName = (nowPlaying?.albumArtist != nil) ? nowPlaying?.albumArtist : nowPlaying?.artist | ||
if nowPlaying == nil || artistName == nil { | ||
return "nil" | ||
} else { | ||
return artistName! | ||
} | ||
} | ||
|
||
func getCoverFromAPI(albumName: String, artistName: String, completion: @escaping (String?)->()) { | ||
let searchTerm = albumName.replacingOccurrences(of: " ", with: "+") | ||
var components = URLComponents() | ||
components.scheme = "https" | ||
components.host = "api.music.apple.com" | ||
components.path = "/v1/catalog/\(countryCode ?? "us")/search" | ||
components.queryItems = [ | ||
URLQueryItem(name: "term", value: searchTerm), | ||
URLQueryItem(name: "limit", value: "15"), | ||
URLQueryItem(name: "types", value: "albums"), | ||
] | ||
let url = components.url! | ||
|
||
var request = URLRequest(url: url) | ||
request.setValue("Bearer \(apiKey ?? "nil")", forHTTPHeaderField: "Authorization") | ||
request.httpMethod = "GET" | ||
|
||
let task = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { data, response, error in | ||
guard error == nil else { | ||
return completion(nil) | ||
} | ||
guard let data = data else { | ||
return completion(nil) | ||
} | ||
|
||
do { | ||
if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] { | ||
if let results = json["results"] as? [String: Any] { | ||
if let albums = results["albums"] as? [String: Any] { | ||
if let data = albums["data"] as? NSArray { | ||
for album in data { | ||
guard let albumJson = album as? [String: Any] else { | ||
continue | ||
} | ||
guard let attributes = albumJson["attributes"] as? [String: Any] else { | ||
continue | ||
} | ||
|
||
if (attributes["name"] as! String == albumName) && (attributes["artistName"] as! String == artistName) { | ||
guard let artwork = attributes["artwork"] as? [String: Any] else { | ||
continue | ||
} | ||
|
||
var url = artwork["url"] as? String | ||
url = url?.replacingOccurrences(of: "{w}", with: "200") | ||
url = url?.replacingOccurrences(of: "{h}", with: "200") | ||
return completion(url) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return completion(nil) | ||
} catch { | ||
return completion(nil) | ||
} | ||
}) | ||
task.resume() | ||
} | ||
} |
Oops, something went wrong.