Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/glacials/placeframe. a google timeline → photo EXIF data synchronizer
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195import Foundation
public final class ProcessingPipeline: Sendable { private let importer: any TimelineImporting private let reader: any PhotoLibraryReading private let geocoder: any ReverseGeocoding private let matcher: TimelineMatcher private let labelFormatter: LocationLabelFormatter private let policy: MatchPolicy private let captureTimeOffsetAnalyzer: CaptureTimeOffsetAnalyzer
public init( importer: any TimelineImporting, reader: any PhotoLibraryReading, geocoder: any ReverseGeocoding, matcher: TimelineMatcher = TimelineMatcher(), labelFormatter: LocationLabelFormatter = LocationLabelFormatter(), policy: MatchPolicy = MatchPolicy(), captureTimeOffsetAnalyzer: CaptureTimeOffsetAnalyzer = CaptureTimeOffsetAnalyzer() ) { self.importer = importer self.reader = reader self.geocoder = geocoder self.matcher = matcher self.labelFormatter = labelFormatter self.policy = policy self.captureTimeOffsetAnalyzer = captureTimeOffsetAnalyzer }
public func prepareReview( from data: Data, onStageChange: @escaping @Sendable (ProcessingStage) -> Void ) async throws -> PreparedReview { onStageChange(.readingTimeline) let timeline = try importer.loadTimeline(from: data)
onStageChange(.scanningPhotosLibrary) let candidateRange = timeline.range.expanded(by: policy.candidatePadding) let assets = try await reader.fetchCandidateAssets(in: candidateRange)
return await prepareReview( timeline: timeline, assets: assets, captureTimeOffset: 0, onStageChange: onStageChange ) }
public func prepareReview( timeline: ImportedTimeline, assets: [PhotoAsset], captureTimeOffset: TimeInterval = 0 ) async -> PreparedReview { await prepareReview( timeline: timeline, assets: assets, captureTimeOffset: captureTimeOffset, onStageChange: nil ) }
public func prepareReview( timeline: ImportedTimeline, assets: [PhotoAsset], captureTimeOffsetsByDayStart: [Date: TimeInterval] ) async -> PreparedReview { await prepareReview( timeline: timeline, assets: assets, captureTimeOffsetsByDayStart: captureTimeOffsetsByDayStart, onStageChange: nil ) }
private func prepareReview( timeline: ImportedTimeline, assets: [PhotoAsset], captureTimeOffset: TimeInterval, onStageChange: (@Sendable (ProcessingStage) -> Void)? ) async -> PreparedReview { onStageChange?(.matchingLocations) let matches = matcher.match(assets: assets, timeline: timeline, captureTimeOffset: captureTimeOffset) let captureTimeOffsetAnalysis = captureTimeOffsetAnalyzer.analyze( timeline: timeline, assets: assets, currentOffset: captureTimeOffset )
onStageChange?(.reverseGeocodingPlaces) let items = await buildReviewItems(from: matches)
onStageChange?(.preparingReview) let summary = ReviewSummary( totalAssets: items.count, autoSuggested: matches.filter { $0.disposition == .autoSuggested }.count, ambiguous: matches.filter { $0.disposition == .ambiguous }.count, unmatched: matches.filter { $0.disposition == .unmatched }.count )
return PreparedReview( timeline: timeline, candidateAssets: assets, items: items, summary: summary, captureTimeOffset: captureTimeOffset, captureTimeOffsetAnalysis: captureTimeOffsetAnalysis ) }
private func prepareReview( timeline: ImportedTimeline, assets: [PhotoAsset], captureTimeOffsetsByDayStart: [Date: TimeInterval], onStageChange: (@Sendable (ProcessingStage) -> Void)? ) async -> PreparedReview { onStageChange?(.matchingLocations)
let calendar = Calendar.autoupdatingCurrent let groupedAssets = Dictionary(grouping: assets) { asset in captureTimeOffsetsByDayStart[calendar.startOfDay(for: asset.creationDate)] ?? 0 } let matches = groupedAssets .flatMap { offset, groupedAssets in matcher.match(assets: groupedAssets, timeline: timeline, captureTimeOffset: offset) } .sorted { $0.asset.creationDate < $1.asset.creationDate }
onStageChange?(.reverseGeocodingPlaces) let items = await buildReviewItems(from: matches)
onStageChange?(.preparingReview) let summary = ReviewSummary( totalAssets: items.count, autoSuggested: matches.filter { $0.disposition == .autoSuggested }.count, ambiguous: matches.filter { $0.disposition == .ambiguous }.count, unmatched: matches.filter { $0.disposition == .unmatched }.count )
return PreparedReview( timeline: timeline, candidateAssets: assets, items: items, summary: summary, captureTimeOffset: 0, captureTimeOffsetAnalysis: nil ) }
private func buildReviewItems(from matches: [MatchCandidate]) async -> [ReviewItem] { var built: [ReviewItem] = [] built.reserveCapacity(matches.count)
for match in matches { guard match.disposition != .unmatched, let point = match.point else { continue }
let resolvedLocation = await geocoder.resolveLocation(for: point.coordinate) let defaultOption = resolvedLocation.defaultOption ?? LocationOption( precision: .exact, coordinate: point.coordinate, label: labelFormatter.string(for: point.coordinate) ) let decision = MatchDecision( assetID: match.asset.id, captureDate: match.asset.creationDate, coordinate: defaultOption.coordinate, label: defaultOption.label, confidence: match.confidence, precision: defaultOption.precision ) built.append( ReviewItem( asset: match.asset, proposedCoordinate: defaultOption.coordinate, locationLabel: defaultOption.label, confidence: match.confidence, timeDelta: match.timeDelta, disposition: match.disposition, suggestedDecision: match.disposition == .unmatched ? nil : decision, availableLocationOptions: resolvedLocation.options ) ) }
return built.sorted { $0.asset.creationDate < $1.asset.creationDate } }}
private extension ClosedRange where Bound == Date { func expanded(by seconds: TimeInterval) -> ClosedRange<Date> { lowerBound.addingTimeInterval(-seconds)...upperBound.addingTimeInterval(seconds) }}