Search metadata directly from IDs embedded in file names

ParseFilename's movie-IMDB pattern now recognizes parenthesized years and
brace-wrapped service IDs, so names like "Movie (2024) {tmdb-1015812}" no
longer truncate to "Movie (2024) {tmdb". The ID kind is captured (imdb,
tmdb, tvdb, tt; IDs widened to 7-9 digits for modern IMDb IDs) and
emitted as an "id:<kind>:<value>" line.

Parsed IDs ride along in MetadataSearchTerms.movie as a MetadataFileID.
TheMovieDB answers direct lookups (movie/{id} for TMDb IDs, find +
external_source=imdb_id for IMDb IDs) as a movieIDSearch; the search
sheet, the queue metadata action, and the AppleScript fetch-metadata
command try the direct lookup first and fall back to the title search
when the ID is unknown or the provider doesn't support it. Index-based
AppleScript result commands keep the name search by design.

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
Scott GarrettandClaude Code committed 2026-11-28 13:10:25 -05:00
1 parent 401584a376
commit f20ad7ee45
10 files changed
+203 -60

No files matched your search

+35 -2
View File
@@ -9,9 +9,18 @@ import Foundation
// MARK: - Filename
/// A metadata service ID embedded in the file name (e.g. `{tmdb-1015812}`).
public enum MetadataFileID: Equatable {
case tmdb(Int)
case tvdb(Int)
/// Normalized to the `tt…` form, leading zeros preserved.
case imdb(String)
}
public enum MetadataSearchTerms {
case none
case movie(title: String)
/// `id` is a metadata service ID parsed from the file name, when present.
case movie(title: String, id: MetadataFileID? = nil)
case tvShow(seriesName: String, season: Int?, episode: Int?)
public var isMovie: Bool { get {
@@ -54,6 +63,30 @@ private func parseAnimeFilename(_ filename: String) -> MetadataSearchTerms? {
return result
}
/// Parses the `id:<kind>:<value>` line emitted by ParseFilename.pl, when present.
private func parseFileID(_ lines: [Substring]) -> MetadataFileID? {
guard let idLine = lines.first(where: { $0.hasPrefix("id:") }) else { return nil }
let components = idLine.dropFirst(3).split(separator: ":", omittingEmptySubsequences: false)
guard components.count == 2, components[1].isEmpty == false else { return nil }
let kind = components[0]
let value = components[1]
switch kind {
case "tmdb":
return Int(value).map(MetadataFileID.tmdb)
case "tvdb":
return Int(value).map(MetadataFileID.tvdb)
case "imdb", "tt":
// The parser strips the `tt` prefix from IMDb IDs; the find API wants it back.
return .imdb("tt" + value)
default:
// A bare number with no known prefix is too ambiguous for a direct lookup.
return nil
}
}
private func parseFilename(_ filename: String) -> MetadataSearchTerms? {
guard let path = Bundle.main.path(forResource: "ParseFilename", ofType: "") else { return nil }
@@ -86,7 +119,7 @@ private func parseFilename(_ filename: String) -> MetadataSearchTerms? {
.replacingOccurrences(of: "[", with: " ")
.replacingOccurrences(of: "]", with: " ")
.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
return MetadataSearchTerms.movie(title: newTitle)
return MetadataSearchTerms.movie(title: newTitle, id: parseFileID(lines))
}
}