Skip to content

Commit

Permalink
First pass at Implementation of MusicKit as a Source (#55)
Browse files Browse the repository at this point in the history
- There is not a 100% mapping of properties... tbd as to what will be done.
  • Loading branch information
bolsinga committed Dec 7, 2023
1 parent 57ee39b commit 475d9a4
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 2 deletions.
117 changes: 117 additions & 0 deletions Sources/iTunes/Track+Song.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//
// Track+Song.swift
//
//
// Created by Greg Bolsinga on 10/31/23.
//

import Foundation
import MusicKit

extension Track {
init(_ song: Song) {
// let refinedSong = try await song.with([.albums], preferredSource: .library)

self.album = song.albumTitle
self.albumArtist = song.artistName
// self.albumRating = album.rating
// self.albumRatingComputed = song.isRatingComputed
self.artist = song.artistName
// self.bitRate = song.bitrate
// self.bPM = song.beatsPerMinute
// self.comments = comments
// self.compilation = album.isCompilation
self.composer = song.composerName
// self.contentRating = contentRating
self.dateAdded = song.libraryAddedDate
// self.dateModified = date
// self.disabled = song.isUserDisabled
// self.discCount = album.discCount
self.discNumber = song.discNumber
if let contentRating = song.contentRating, contentRating == .explicit {
self.explicit = true
}
self.genre = song.genreNames.first
// self.grouping = grouping
// self.hasVideo = true
// self.kind = kind
// self.location = location.absoluteString
// self.movie = true
// self.musicVideo = true
// self.podcast = true
// self.tVShow = true
self.name = song.title
// self.partOfGaplessAlbum = album.isGapless
//
self.persistentID = UInt(song.id.rawValue) ?? 0
self.playCount = song.playCount
self.playDateUTC = song.lastPlayedDate
// self.protected = song.isDRMProtected
// self.purchased = song.isPurchased
// self.rating = song.rating
// self.ratingComputed = song.isRatingComputed
self.releaseDate = song.releaseDate
// self.sampleRate = song.sampleRate
// self.size = song.fileSize
// self.skipCount = song.skipCount
// self.skipDate = date
// self.sortAlbum = name
// self.sortAlbumArtist = name
// self.sortArtist = name
// self.sortComposer = sortComposer
// self.sortName = sortName
if let duration = song.duration {
self.totalTime = Int(duration)
}
//
// self.trackCount = album.trackCount
self.trackNumber = song.trackNumber
// self.trackType = "File"
// self.trackType = "URL"
// self.trackType = "Remote"
// self.unplayed = playStatus
// self.year = song.year

// if let videoInfo = song.videoInfo {
// if let episode = videoInfo.episode {
// self.episode = episode
// }
// if videoInfo.episodeOrder != 0 {
// self.episodeOrder = videoInfo.episodeOrder
// }
// if videoInfo.isHD {
// self.hD = videoInfo.isHD
// }
// if videoInfo.season != 0 {
// self.season = videoInfo.season
// }
// if let series = videoInfo.series {
// self.series = series
// }
// if let sortSeries = videoInfo.sortSeries {
// self.sortSeries = sortSeries
// }
// if videoInfo.videoHeight != 0 {
// self.videoHeight = videoInfo.videoHeight
// }
// if videoInfo.videoWidth != 0 {
// self.videoWidth = videoInfo.videoWidth
// }
// }
}
}

extension Track {
static func requestAccess() async {
let musicAuthorizationStatus = await MusicAuthorization.request()
print("\(musicAuthorizationStatus)")
}

static public func gatherWithMusicKit() async throws -> [Track] {
await requestAccess()

let request = MusicLibraryRequest<Song>()
let response = try await request.response()
return response.items.map { Track($0) }
}
}
3 changes: 1 addition & 2 deletions Sources/tool/Track+Export.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import iTunes
enum TrackExportError: Error {
case noITunesTracks
case cannotConvertJSONToString
case invalidSource
}

extension Source {
Expand All @@ -20,7 +19,7 @@ extension Source {
case .itunes:
return try Track.gatherAllTracks()
case .musickit:
throw TrackExportError.invalidSource
return try await Track.gatherWithMusicKit()
}
}
}
Expand Down

0 comments on commit 475d9a4

Please sign in to comment.