Improve MediaKind usage.
This commit is contained in:
9 files changed
+185
-191
No files matched your search
@@ -366,7 +366,7 @@ final class DocumentWindowController: NSWindowController, TracksViewControllerDe
|
||||
|
||||
@IBAction func searchChapters(_ sender: Any?) {
|
||||
let name = mp4.metadata.metadataItemsFiltered(byIdentifier: MP42MetadataKeyName).first?.stringValue
|
||||
let url = mp4.firstSourceURL() ?? doc.fileURL
|
||||
let url = mp4.firstSourceURL ?? doc.fileURL
|
||||
let title = (name?.isEmpty == false ? name : url?.lastPathComponent) ?? ""
|
||||
let duration = UInt64(mp4.duration)
|
||||
|
||||
|
||||
@@ -37,30 +37,23 @@ extension MP42File {
|
||||
return nil
|
||||
}
|
||||
|
||||
func firstSourceURL() -> URL? {
|
||||
for track in tracks {
|
||||
if track.url != nil {
|
||||
return track.url
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
var firstSourceURL: URL? { tracks.lazy.compactMap { $0.url }.first }
|
||||
|
||||
func extractSearchTerms(fallbackURL: URL?) -> MetadataSearchTerms {
|
||||
if let tvShow = metadata.metadataItemsFiltered(byIdentifier: MP42MetadataKeyTVShow).first?.stringValue,
|
||||
let season = metadata.metadataItemsFiltered(byIdentifier: MP42MetadataKeyTVSeason).first?.numberValue?.intValue,
|
||||
let number = metadata.metadataItemsFiltered(byIdentifier: MP42MetadataKeyTVEpisodeNumber).first?.numberValue?.intValue,
|
||||
let mediaKind = metadata.metadataItemsFiltered(byIdentifier: MP42MetadataKeyMediaKind).first?.numberValue?.intValue,
|
||||
mediaKind == MetadataResult.MediaKindType.tvShow.rawValue {
|
||||
mediaKind == MediaKind.tvShow.rawValue {
|
||||
return MetadataSearchTerms.tvShow(seriesName: tvShow, season: season, episode: number)
|
||||
}
|
||||
else if let title = metadata.metadataItemsFiltered(byIdentifier: MP42MetadataKeyName).first?.stringValue,
|
||||
let _ = metadata.metadataItemsFiltered(byIdentifier: MP42MetadataKeyReleaseDate).first?.stringValue,
|
||||
let mediaKind = metadata.metadataItemsFiltered(byIdentifier: MP42MetadataKeyMediaKind).first?.numberValue?.intValue,
|
||||
mediaKind == MetadataResult.MediaKindType.movie.rawValue {
|
||||
mediaKind == MediaKind.movie.rawValue {
|
||||
return MetadataSearchTerms.movie(title: title)
|
||||
}
|
||||
else if let url = firstSourceURL() ?? fallbackURL {
|
||||
else if let url = firstSourceURL ?? fallbackURL {
|
||||
let parsed = url.deletingPathExtension().lastPathComponent.parsedAsFilename()
|
||||
|
||||
switch parsed {
|
||||
@@ -75,21 +68,22 @@ extension MP42File {
|
||||
return MetadataSearchTerms.none
|
||||
}
|
||||
|
||||
private func outputNameFormat(mediaKind: Int) -> [Token]? {
|
||||
if mediaKind == MetadataResult.MediaKindType.tvShow.rawValue {
|
||||
private func outputNameFormat(mediaKind: MediaKind) -> [Token] {
|
||||
switch mediaKind {
|
||||
case .tvShow:
|
||||
return UserDefaults.standard.tokenArray(forKey: "SBTVShowFormatTokens")
|
||||
} else if mediaKind == MetadataResult.MediaKindType.movie.rawValue {
|
||||
case .movie:
|
||||
return UserDefaults.standard.tokenArray(forKey: "SBMovieFormatTokens")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// MARK: File name
|
||||
|
||||
func preferredFileName() -> String? {
|
||||
if let mediaKind = metadata.metadataItemsFiltered(byIdentifier: MP42MetadataKeyMediaKind).first?.numberValue?.intValue,
|
||||
(mediaKind == MetadataResult.MediaKindType.tvShow.rawValue && UserDefaults.standard.bool(forKey: "SBSetTVShowFormat")) ||
|
||||
(mediaKind == MetadataResult.MediaKindType.movie.rawValue && UserDefaults.standard.bool(forKey: "SBSetMovieFormat")),
|
||||
if let mediaKindValue = metadata.metadataItemsFiltered(byIdentifier: MP42MetadataKeyMediaKind).first?.numberValue?.intValue,
|
||||
let mediaKind = MediaKind(rawValue: mediaKindValue),
|
||||
(mediaKind == .tvShow && UserDefaults.standard.bool(forKey: "SBSetTVShowFormat")) ||
|
||||
(mediaKind == .movie && UserDefaults.standard.bool(forKey: "SBSetMovieFormat")),
|
||||
let name = formattedFileName() {
|
||||
return name
|
||||
} else {
|
||||
@@ -98,10 +92,11 @@ extension MP42File {
|
||||
}
|
||||
|
||||
func formattedFileName() -> String? {
|
||||
guard let mediaKind = metadata.metadataItemsFiltered(byIdentifier: MP42MetadataKeyMediaKind).first?.numberValue?.intValue,
|
||||
let format = outputNameFormat(mediaKind: mediaKind)
|
||||
guard let mediaKindValue = metadata.metadataItemsFiltered(byIdentifier: MP42MetadataKeyMediaKind).first?.numberValue?.intValue,
|
||||
let mediaKind = MediaKind(rawValue: mediaKindValue)
|
||||
else { return nil }
|
||||
|
||||
let format = outputNameFormat(mediaKind: mediaKind)
|
||||
let separators = CharacterSet(charactersIn: "{}")
|
||||
var name = ""
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ private extension MetadataResult {
|
||||
self[.trackNumber] = episode.episodeNumber
|
||||
|
||||
if let ratingCode = episode.rating?.displayName {
|
||||
self[.rating] = Ratings.shared.rating(storeCode: store.storeCode, media: "TV", code: ratingCode)?.iTunesCode
|
||||
self[.rating] = Ratings.shared.rating(storeCode: store.storeCode, mediaKind: .tvShow, code: ratingCode)?.iTunesCode
|
||||
}
|
||||
|
||||
self[.iTunesURL] = episode.showURL
|
||||
@@ -163,7 +163,7 @@ public struct AppleTV: MetadataService {
|
||||
metadata[.studio] = content.studio
|
||||
|
||||
if metadata[.rating] == nil {
|
||||
metadata[.rating] = Ratings.shared.rating(storeCode: store.storeCode, media: "TV", code: content.rating.displayName)?.iTunesCode
|
||||
metadata[.rating] = Ratings.shared.rating(storeCode: store.storeCode, mediaKind: .tvShow, code: content.rating.displayName)?.iTunesCode
|
||||
}
|
||||
metadata[.cast] = details.roles.filter { $0.type == "Actor" }.map { $0.personName }.joined(separator: ", ") +
|
||||
details.roles.filter { $0.type == "Voice" }.map { $0.personName }.joined(separator: ", ")
|
||||
@@ -200,7 +200,7 @@ public struct AppleTV: MetadataService {
|
||||
metadata[.genre] = content.genres.map { $0.name }.joined(separator: ", ")
|
||||
metadata[.studio] = content.studio
|
||||
|
||||
metadata[.rating] = Ratings.shared.rating(storeCode: store.storeCode, media: "movie", code: content.rating.displayName)?.iTunesCode
|
||||
metadata[.rating] = Ratings.shared.rating(storeCode: store.storeCode, mediaKind: .movie, code: content.rating.displayName)?.iTunesCode
|
||||
|
||||
metadata[.cast] = details.roles.filter { $0.type == "Actor" }.map { $0.personName }.joined(separator: ", ") +
|
||||
details.roles.filter { $0.type == "Voice" }.map { $0.personName }.joined(separator: ", ")
|
||||
|
||||
@@ -68,6 +68,20 @@ public struct Artwork {
|
||||
public let size: ArtworkSize
|
||||
}
|
||||
|
||||
public enum MediaKind: Int {
|
||||
case tvShow = 10
|
||||
case movie = 9
|
||||
|
||||
public var description: String {
|
||||
switch self {
|
||||
case .movie:
|
||||
return "Movie"
|
||||
case .tvShow:
|
||||
return "TV"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private let localizedKeys: [MetadataResult.Key: String] = [
|
||||
.name : NSLocalizedString("Name", comment: "nil"),
|
||||
.composer : NSLocalizedString("Composer", comment: "nil"),
|
||||
@@ -103,7 +117,7 @@ private let localizedKeys: [MetadataResult.Key: String] = [
|
||||
.serviceEpisodeID : NSLocalizedString("Service ID", comment: "nil")
|
||||
]
|
||||
|
||||
public class MetadataResult {
|
||||
public final class MetadataResult {
|
||||
|
||||
public enum Key: String {
|
||||
// Common Keys
|
||||
@@ -263,22 +277,7 @@ public class MetadataResult {
|
||||
}
|
||||
|
||||
private var dictionary: [Key:Any]
|
||||
|
||||
public enum MediaKindType: Int {
|
||||
case tvShow = 10
|
||||
case movie = 9
|
||||
|
||||
public var description: String {
|
||||
switch self {
|
||||
case .movie:
|
||||
return "Movie"
|
||||
case .tvShow:
|
||||
return "TV"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public var mediaKind: MediaKindType
|
||||
public var mediaKind: MediaKind
|
||||
public var contentRating: Int
|
||||
public var remoteArtworks: [Artwork]
|
||||
public var artworks: [MP42Image]
|
||||
|
||||
@@ -19,14 +19,14 @@ final class Ratings {
|
||||
self.countries = countries
|
||||
}
|
||||
|
||||
func rating(countryCode: String, media: String, name: String) -> Rating? {
|
||||
func rating(countryCode: String, mediaKind: MediaKind, name: String) -> Rating? {
|
||||
guard let country = countries.first(where: { $0.displayName == countryCode }) else { return nil }
|
||||
return country.ratings.first(where: {$0.media.contains(media) && $0.displayName == name})
|
||||
return country.ratings.first(where: {$0.media.contains(mediaKind.description) && $0.displayName == name})
|
||||
}
|
||||
|
||||
func rating(storeCode: Int, media: String, code: String) -> Rating? {
|
||||
func rating(storeCode: Int, mediaKind: MediaKind, code: String) -> Rating? {
|
||||
guard let country = countries.first(where: { $0.storeCode == storeCode }) else { return nil }
|
||||
return country.ratings.first(where: {$0.media.contains(media) && $0.code == code})
|
||||
return country.ratings.first(where: {$0.media.contains(mediaKind.description) && $0.code == code})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -178,7 +178,7 @@ public struct TheMovieDB: MetadataService {
|
||||
if let releases = result.releases {
|
||||
for release in releases.countries {
|
||||
if release.iso_3166_1 == "US" {
|
||||
metadata[.rating] = Ratings.shared.rating(countryCode: "USA", media: "movie", name: release.certification)?.iTunesCode
|
||||
metadata[.rating] = Ratings.shared.rating(countryCode: "USA", mediaKind: .movie, name: release.certification)?.iTunesCode
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -320,7 +320,7 @@ public struct TheMovieDB: MetadataService {
|
||||
if let ratings = info.content_ratings?.results {
|
||||
let USRating = ratings.filter { $0.iso_3166_1 == "US" }.compactMap { $0.rating }
|
||||
if let rating = USRating.first {
|
||||
metadata[.rating] = Ratings.shared.rating(countryCode: "USA", media: "TV", name: rating)?.iTunesCode
|
||||
metadata[.rating] = Ratings.shared.rating(countryCode: "USA", mediaKind: .tvShow, name: rating)?.iTunesCode
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -138,7 +138,7 @@ public struct TheTVDB : MetadataService {
|
||||
|
||||
// Rating
|
||||
if let rating = info.rating, rating.count > 0 {
|
||||
result[.rating] = Ratings.shared.rating(countryCode: "USA", media: "TV", name: rating)?.iTunesCode
|
||||
result[.rating] = Ratings.shared.rating(countryCode: "USA", mediaKind: .tvShow, name: rating)?.iTunesCode
|
||||
}
|
||||
|
||||
// Actors
|
||||
|
||||
@@ -608,7 +608,7 @@ public struct iTunesStore: MetadataService {
|
||||
}
|
||||
|
||||
if let contentAdvisoryRating = result.contentAdvisoryRating {
|
||||
metadata[.rating] = Ratings.shared.rating(countryCode: store.country, media: metadata.mediaKind == .movie ? "movie" : "TV", name: contentAdvisoryRating)?.iTunesCode
|
||||
metadata[.rating] = Ratings.shared.rating(countryCode: store.country, mediaKind: metadata.mediaKind, name: contentAdvisoryRating)?.iTunesCode
|
||||
}
|
||||
|
||||
metadata[.iTunesCountry] = store.storeCode
|
||||
@@ -654,7 +654,7 @@ public struct iTunesStore: MetadataService {
|
||||
metadata[.genre] = result.primaryGenreName
|
||||
|
||||
if let contentAdvisoryRating = result.contentAdvisoryRating {
|
||||
metadata[.rating] = Ratings.shared.rating(countryCode: store.country, media: metadata.mediaKind == .movie ? "movie" : "TV", name: contentAdvisoryRating)?.iTunesCode
|
||||
metadata[.rating] = Ratings.shared.rating(countryCode: store.country, mediaKind: metadata.mediaKind, name: contentAdvisoryRating)?.iTunesCode
|
||||
}
|
||||
|
||||
metadata[.iTunesCountry] = store.storeCode
|
||||
|
||||
Reference in new issue
Block a user