Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First pass at Implementation of MusicKit as a Source #55

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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