Skip to content

Commit

Permalink
Add wallpapper-exif application
Browse files Browse the repository at this point in the history
  • Loading branch information
mczachurski committed Jan 22, 2021
1 parent 3a63762 commit b42ab55
Show file tree
Hide file tree
Showing 28 changed files with 509 additions and 52 deletions.
8 changes: 6 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import PackageDescription
let package = Package(
name: "wallpapper",
products: [
.executable(name: "wallpapper", targets: ["wallpapper"]),
.library(name: "WallpapperLib", targets: ["WallpapperLib"]),
.executable(name: "wallpapper", targets: ["Wallpapper"]),
.executable(name: "wallpapper-exif", targets: ["WallpapperExif"])
],
dependencies: [],
targets: [
.target(name: "wallpapper", dependencies: [])
.target(name: "WallpapperLib", dependencies: []),
.target(name: "Wallpapper", dependencies: ["WallpapperLib"]),
.target(name: "WallpapperExif", dependencies: ["WallpapperLib"])
]
)
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import WallpapperLib

class Program {

Expand Down Expand Up @@ -62,8 +63,8 @@ class Program {
self.consoleIO.writeMessage("Reading HEIC file: '\(fileURL.absoluteString)'...", to: .debug)
let inputFileContents = try Data(contentsOf: fileURL)

let metadataExtractor = MetadataExtractor()
try metadataExtractor.extract(imageData: inputFileContents)
let dynamicWallpaperExtractor = DynamicWallpaperExtractor()
try dynamicWallpaperExtractor.extract(imageData: inputFileContents)
} catch {
self.consoleIO.writeMessage("Error occurs during metadata extraction: \(error)", to: .error)
return false
Expand Down Expand Up @@ -134,7 +135,7 @@ class Program {
}

private func printVersion() {
self.consoleIO.writeMessage("1.6.0")
self.consoleIO.writeMessage("1.7.0")
}

private func printUsage() {
Expand Down
File renamed without changes.
23 changes: 23 additions & 0 deletions Sources/WallpapperExif/OptionType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// OptionType.swift
// wallpapper-exif
//
// Created by Marcin Czachurski on 21/10/2021.
// Copyright © 2021 Marcin Czachurski. All rights reserved.
//

import Foundation

enum OptionType: String {
case help = "-h"
case version = "-v"
case unknown

init(value: String) {
switch value {
case "-h": self = .help
case "-v": self = .version
default: self = .unknown
}
}
}
110 changes: 110 additions & 0 deletions Sources/WallpapperExif/Program.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//
// Program.swift
// wallpapper-exif
//
// Created by Marcin Czachurski on 21/10/2021.
// Copyright © 2021 Marcin Czachurski. All rights reserved.
//

import Foundation
import WallpapperLib

class Program {
let consoleIO = ConsoleIO()
var inputFileNames: [String] = []
var wallpapperItems: [WallpapperItem] = []

func run() throws -> Bool {

let (shouldBreak, resultCode) = self.proceedCommandLineArguments()
if shouldBreak {
return resultCode
}

for inputFileName in inputFileNames {
let fileURL = try getPathToInputFile(inputFileName: inputFileName)
let inputFileContents = try Data(contentsOf: fileURL)

let locationExtractor = LocationExtractor()
let imageLocation = try locationExtractor.extract(imageData: inputFileContents)

let sc = SunCalculations(date: imageLocation.createDate,
latitude: imageLocation.latitude,
longitude: imageLocation.longitude)

let position = sc.getSunPosition()

let wallpapperItem = WallpapperItem(fileName: inputFileName,
altitude: position.altitude,
azimuth: position.azimuth)

wallpapperItems.append(wallpapperItem)
}

let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let jsonData = try encoder.encode(wallpapperItems)
let jsonOptionslString = String(bytes: jsonData, encoding: .utf8)

guard let jsonString = jsonOptionslString else {
return false
}

self.consoleIO.writeMessage(jsonString, to: .standard)
return true
}

private func getPathToInputFile(inputFileName: String) throws -> URL {
return URL(fileURLWithPath: inputFileName)
}

private func proceedCommandLineArguments() -> (Bool, Bool) {
if CommandLine.arguments.count == 1 {
self.printUsage()
return (true, false)
}

var optionIndex = 1
while optionIndex < CommandLine.arguments.count {

let option = CommandLine.arguments[optionIndex]
let optionType = OptionType(value: option)

switch optionType {
case .help:
self.printUsage()
return (true, true)
case .version:
self.printVersion()
return (true, true)
default:
let fileName = CommandLine.arguments[optionIndex]
inputFileNames.append(fileName)
break;
}

optionIndex = optionIndex + 1
}

if self.inputFileNames.count == 0 {
self.consoleIO.writeMessage("unknown input file names.", to: .error)
return (true, false)
}

return (false, false)
}

private func printVersion() {
self.consoleIO.writeMessage("1.0.0")
}

private func printUsage() {

let executableName = (CommandLine.arguments[0] as NSString).lastPathComponent

self.consoleIO.writeMessage("\(executableName): [file1] [file2]")
self.consoleIO.writeMessage("Command options are:")
self.consoleIO.writeMessage(" -h\t\t\tshow this message and exit")
self.consoleIO.writeMessage(" -v\t\t\tshow program version and exit")
}
}
14 changes: 14 additions & 0 deletions Sources/WallpapperExif/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// main.swift
// wallpapper-exif
//
// Created by Marcin Czachurski on 21/10/2021.
// Copyright © 2021 Marcin Czachurski. All rights reserved.
//

import Foundation

let program = Program()
let result = try program.run()

exit(result ? EXIT_SUCCESS : EXIT_FAILURE)
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
//
// ConsoleIO.swift
// wallpapper
// WallpapperLib
//
// Created by Marcin Czachurski on 03/07/2018.
// Copyright © 2018 Marcin Czachurski. All rights reserved.
// Created by Marcin Czachurski on 22/01/2021.
// Copyright © 2021 Marcin Czachurski. All rights reserved.
//

import Foundation

class ConsoleIO {
func writeMessage(_ message: String, to: OutputType = .standard) {
public class ConsoleIO {

public init() {
}

public func writeMessage(_ message: String, to: OutputType = .standard) {
switch to {
case .standard:
print(message)
Expand Down
15 changes: 15 additions & 0 deletions Sources/WallpapperLib/ConsoleOutput/OutputType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// OutputType.swift
// WallpapperLib
//
// Created by Marcin Czachurski on 22/01/2021.
// Copyright © 2021 Marcin Czachurski. All rights reserved.
//

import Foundation

public enum OutputType {
case error
case standard
case debug
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//
// ImageMetadataGeneratorError.swift
// wallpapper
// WallpapperLib
//
// Created by Marcin Czachurski on 20/01/2021.
// Copyright © 2021 Marcin Czachurski. All rights reserved.
//

import Foundation

enum ImageMetadataGeneratorError: Error {
public enum ImageMetadataGeneratorError: Error {
case addTagIntoImageFailed
case imageNotFinalized
case namespaceNotRegistered
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//
// ImageMetadata.swift
// wallpapper
// ImageMetadataGenerator.swift
// WallpapperLib
//
// Created by Marcin Czachurski on 10/05/2020.
// Copyright © 2020 Marcin Czachurski. All rights reserved.
//

import Foundation

class ImageMetadataGenerator {
public class ImageMetadataGenerator {
private let pictureInfos: [PictureInfo]

public lazy var images: [String] = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//
// Apperance.swift
// wallpapper
// WallpapperLib
//
// Created by Marcin Czachurski on 31/07/2018.
// Copyright © 2018 Marcin Czachurski. All rights reserved.
//

import Foundation

class Apperance: Codable {
public class Apperance: Codable {
enum CodingKeys: String, CodingKey {
case darkIndex = "d"
case lightIndex = "l"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//
// PictureInfo.swift
// wallpapper
// WallpapperLib
//
// Created by Marcin Czachurski on 02/07/2018.
// Copyright © 2018 Marcin Czachurski. All rights reserved.
//

import Foundation

struct PictureInfo: Decodable {
public struct PictureInfo: Decodable {
var fileName: String
var isPrimary: Bool?
var isForLight: Bool?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//
// SequenceInfo.swift
// wallpapper
// WallpapperLib
//
// Created by Marcin Czachurski on 03/07/2018.
// Copyright © 2018 Marcin Czachurski. All rights reserved.
//

import Foundation

class SequenceInfo : Codable {
public class SequenceInfo : Codable {
enum CodingKeys: String, CodingKey {
case sequenceItems = "si"
case timeItems = "ti"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//
// SequenceItem.swift
// wallpapper
// WallpapperLib
//
// Created by Marcin Czachurski on 03/07/2018.
// Copyright © 2018 Marcin Czachurski. All rights reserved.
//

import Foundation

class SequenceItem : Codable {
public class SequenceItem : Codable {
enum CodingKeys: String, CodingKey {
case altitude = "a"
case azimuth = "z"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//
// TimeItem.swift
// wallpapper
// WallpapperLib
//
// Created by Marcin Czachurski on 01/07/2019.
// Copyright © 2019 Marcin Czachurski. All rights reserved.
//

import Foundation

class TimeItem : Codable {
public class TimeItem : Codable {
enum CodingKeys: String, CodingKey {
case time = "t"
case imageIndex = "i"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Generator.swift
// wallpapper
// WallpaperGenerator.swift
// WallpapperLib
//
// Created by Marcin Czachurski on 02/07/2018.
// Copyright © 2018 Marcin Czachurski. All rights reserved.
Expand All @@ -10,7 +10,10 @@ import Foundation
import AppKit
import AVFoundation

class WallpaperGenerator {
public class WallpaperGenerator {

public init() {
}

public func generate(pictureInfos: [PictureInfo], baseURL: URL, outputFileName: String) throws {
let consoleIO = ConsoleIO()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// NSImage.swift
// wallpapper
// WallpapperLib
//
// Created by Marcin Czachurski on 11/07/2018.
// Copyright © 2018 Marcin Czachurski. All rights reserved.
Expand Down
Loading

0 comments on commit b42ab55

Please sign in to comment.