Skip to content

Commit

Permalink
Added CSV export profile (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
orchetect committed Jan 1, 2024
1 parent f5b334c commit 5fea811
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Sources/MarkersExtractor/Export/ExportProfileFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Foundation

public enum ExportProfileFormat: String, CaseIterable, Equatable, Hashable {
case airtable
case csv
case midi
case notion
}
Expand All @@ -17,6 +18,8 @@ extension ExportProfileFormat {
switch self {
case .airtable:
return "Airtable"
case .csv:
return "CSV"
case .midi:
return "MIDI File"
case .notion:
Expand All @@ -28,6 +31,8 @@ extension ExportProfileFormat {
switch self {
case .airtable:
return AirtableExportProfile.self
case .csv:
return CSVProfile.self
case .midi:
return MIDIFileExportProfile.self
case .notion:
Expand Down
108 changes: 108 additions & 0 deletions Sources/MarkersExtractor/Export/Profile/CSV/CSVProfile Export.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//
// CSVProfile Export.swift
// MarkersExtractor • https://github.com/TheAcharya/MarkersExtractor
// Licensed under MIT License
//

import AVFoundation
import CodableCSV
import Foundation
import Logging
import OrderedCollections
import TimecodeKit

extension CSVProfile {
public func prepareMarkers(
markers: [Marker],
idMode: MarkerIDMode,
tcStringFormat: Timecode.StringFormat,
payload: Payload,
mediaInfo: ExportMarkerMediaInfo?
) -> [PreparedMarker] {
markers.map {
PreparedMarker(
$0,
idMode: idMode,
mediaInfo: mediaInfo, tcStringFormat: tcStringFormat
)
}
}

public func writeManifests(
_ preparedMarkers: [PreparedMarker],
payload: Payload,
noMedia: Bool
) throws {
try csvWriteManifest(
csvPath: payload.csvPath,
noMedia: noMedia,
preparedMarkers
)
}

public func resultFileContent(payload: Payload) throws -> ExportResult.ResultDictionary {
[
.csvManifestPath: .url(payload.csvPath)
]
}

public func tableManifestFields(
for marker: PreparedMarker,
noMedia: Bool
) -> OrderedDictionary<ExportField, String> {
var dict: OrderedDictionary<ExportField, String> = [:]

dict[.id] = marker.id
dict[.name] = marker.name
dict[.type] = marker.type
dict[.checked] = marker.checked
dict[.status] = marker.status
dict[.notes] = marker.notes
dict[.position] = marker.position
dict[.clipType] = marker.clipType
dict[.clipName] = marker.clipName
dict[.clipDuration] = marker.clipDuration
dict[.videoRole] = marker.videoRole
dict[.audioRole] = marker.audioRole.flat
dict[.eventName] = marker.eventName
dict[.projectName] = marker.projectName
dict[.libraryName] = marker.libraryName
// no iconImage

if !noMedia {
dict[.imageFileName] = marker.imageFileName
}

return dict
}

public func nestedManifestFields(
for marker: PreparedMarker,
noMedia: Bool
) -> OrderedDictionary<ExportField, ExportFieldValue> {
var dict: OrderedDictionary<ExportField, ExportFieldValue> = [:]

dict[.id] = .string(marker.id)
dict[.name] = .string(marker.name)
dict[.type] = .string(marker.type)
dict[.checked] = .string(marker.checked)
dict[.status] = .string(marker.status)
dict[.notes] = .string(marker.notes)
dict[.position] = .string(marker.position)
dict[.clipType] = .string(marker.clipType)
dict[.clipName] = .string(marker.clipName)
dict[.clipDuration] = .string(marker.clipDuration)
dict[.videoRole] = .string(marker.videoRole)
dict[.audioRole] = .array(marker.audioRole.array)
dict[.eventName] = .string(marker.eventName)
dict[.projectName] = .string(marker.projectName)
dict[.libraryName] = .string(marker.libraryName)
// no iconImage

if !noMedia {
dict[.imageFileName] = .string(marker.imageFileName)
}

return dict
}
}
26 changes: 26 additions & 0 deletions Sources/MarkersExtractor/Export/Profile/CSV/CSVProfile.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// CSVProfile.swift
// MarkersExtractor • https://github.com/TheAcharya/MarkersExtractor
// Licensed under MIT License
//

import Foundation
import Logging

public class CSVProfile: NSObject, ProgressReporting, ExportProfile {
// ExportProfile
public typealias Payload = CSVExportPayload
public typealias Icon = EmptyExportIcon
public typealias PreparedMarker = StandardExportMarker
public static let profile: ExportProfileFormat = .csv
public static let isMediaCapable: Bool = false
public var logger: Logger?

// ProgressReporting
public let progress: Progress

public required init(logger: Logger? = nil) {
self.logger = logger
progress = Self.defaultProgress
}
}
13 changes: 13 additions & 0 deletions Sources/MarkersExtractor/MarkersExtractor Export.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ extension MarkersExtractor {
payload: .init(projectName: projectName, outputURL: outputURL),
parentProgress: parentProgress
)
case .csv:
return try await export(
for: CSVProfile.self,
media: media,
markers: markers,
outputURL: outputURL,
payload: .init(
projectName: projectName,
outputURL: outputURL
),
parentProgress: parentProgress
)

case .midi:
return try await export(
for: MIDIFileExportProfile.self,
Expand Down

0 comments on commit 5fea811

Please sign in to comment.