continue sorting out the scripts I need
- ID
12212bb- date
2023-05-12 07:58:38+00:00- author
Alex Chan <alex@alexwlchan.net>- parent
fce131a- message
continue sorting out the scripts I need- changed files
3 files, 174 additions, 97 deletions
Changed files
get_asset_data.swift (0) → get_asset_data.swift (1491)
diff --git a/get_asset_data.swift b/get_asset_data.swift
new file mode 100644
index 0000000..1bb6250
--- /dev/null
+++ b/get_asset_data.swift
@@ -0,0 +1,37 @@
+import Cocoa
+import Photos
+
+let assetId = "55CF4C5B-8BE3-4216-B158-3EF86AAAC5C5/L0/001"
+
+let asset = PHAsset.fetchAssets(withLocalIdentifiers: [assetId], options: nil).firstObject!
+
+// https://stackoverflow.com/a/48755517/1558022
+func getAssetThumbnail(asset: PHAsset, size: Double) -> NSImage {
+ let manager = PHImageManager.default()
+ let option = PHImageRequestOptions()
+ var thumbnail = NSImage()
+ option.isSynchronous = true
+ manager.requestImage(for: asset, targetSize: CGSize(width: size, height: size), contentMode: .aspectFit, options: option, resultHandler: {(result, info)->Void in
+ thumbnail = result!
+ })
+ return thumbnail
+}
+
+func jpegDataFrom(image:NSImage) -> Data {
+ let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil)!
+ let bitmapRep = NSBitmapImageRep(cgImage: cgImage)
+ let jpegData = bitmapRep.representation(using: NSBitmapImageRep.FileType.jpeg, properties: [:])!
+ return jpegData
+}
+
+let thumbnailPath = "/tmp/photos-reviewer/\(asset.localIdentifier.prefix(1))/\(asset.localIdentifier)_65.jpg"
+
+if !FileManager.default.fileExists(atPath: thumbnailPath) {
+ let jpegData = jpegDataFrom(image: getAssetThumbnail(asset: asset, size: 65.0))
+
+ try! FileManager.default.createDirectory(atPath: NSString(string: thumbnailPath).deletingLastPathComponent, withIntermediateDirectories: true, attributes: nil)
+
+ try! jpegData.write(to: URL(fileURLWithPath: thumbnailPath), options: [])
+}
+
+print(thumbnailPath)
get_photos.swift (2873) → get_photos.swift (0)
diff --git a/get_photos.swift b/get_photos.swift
deleted file mode 100644
index c67ee14..0000000
--- a/get_photos.swift
+++ /dev/null
@@ -1,97 +0,0 @@
-#!/usr/bin/env swift
-// Print a list of album names that contain this photo.
-//
-// This takes one arguments: the UUID of the photo.
-//
-// == Usage ==
-//
-// $ swift get_albums_for_photo.swift 9D28ABBE-79F6-402F-8750-8674840EDA3D
-// ["Flagged"]
-//
-
-import Photos
-
-/// Returns a list of album names for albums containing this asset.
-func getAlbumsContainingAsset(asset: PHAsset) -> [String] {
- let collections = PHAssetCollection.fetchAssetCollectionsContaining(
- asset, with: .album, options: nil
- )
-
- var titles: [String] = []
-
- collections.enumerateObjects({ (album, index, stop) in
- if (album.localizedTitle != nil) {
- titles.append(album.localizedTitle!)
- }
- })
-
- return titles
-}
-
-let options = PHFetchOptions()
-options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
-
-let all_assets = PHAsset.fetchAssets(with: options)
-
-let index = IndexSet(integersIn: 0...5)
-
-struct PhotoData: Codable {
- var uuid: String
- var albums: [String]
- var thumbnailPath: String
- // var thumbnail: String
-}
-
-import Cocoa
-
-typealias UIImage = NSImage
-
-// https://stackoverflow.com/a/48755517/1558022
-func getAssetThumbnail(asset: PHAsset, size: Double) -> NSImage {
- let manager = PHImageManager.default()
- let option = PHImageRequestOptions()
- var thumbnail = UIImage()
- option.isSynchronous = true
- manager.requestImage(for: asset, targetSize: CGSize(width: size, height: size), contentMode: .aspectFit, options: option, resultHandler: {(result, info)->Void in
- thumbnail = result!
- })
- return thumbnail
-}
-
-func jpegDataFrom(image:NSImage) -> Data {
- let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil)!
- let bitmapRep = NSBitmapImageRep(cgImage: cgImage)
- let jpegData = bitmapRep.representation(using: NSBitmapImageRep.FileType.jpeg, properties: [:])!
- return jpegData
-}
-
-var response: [PhotoData] = []
-
-for asset in all_assets.objects(at: index) {
- let thumbnailPath = "/tmp/photos-reviewer/\(asset.localIdentifier)_65.jpg"
-
- let data = PhotoData(
- uuid: asset.localIdentifier,
- albums: getAlbumsContainingAsset(asset: asset),
- thumbnailPath: thumbnailPath
- // thumbnail: getAssetThumbnail(asset: all_assets.firstObject!, size: 65.0).base64String!
- )
-
- if !FileManager.default.fileExists(atPath: thumbnailPath) {
- let jpegData = jpegDataFrom(image: getAssetThumbnail(asset: asset, size: 65.0))
-
- try! FileManager.default.createDirectory(atPath: NSString(string: thumbnailPath).deletingLastPathComponent, withIntermediateDirectories: true, attributes: nil)
-
- try! jpegData.write(to: URL(fileURLWithPath: thumbnailPath), options: [])
- }
-
- response.append(data)
-}
-
-let jsonEncoder = JSONEncoder()
-let jsonData = try jsonEncoder.encode(response)
-let json = String(data: jsonData, encoding: String.Encoding.utf8)
-print(json!)
-
-// print()
-//
\ No newline at end of file
get_structural_metadata.swift (0) → get_structural_metadata.swift (3944)
diff --git a/get_structural_metadata.swift b/get_structural_metadata.swift
new file mode 100644
index 0000000..57ac5de
--- /dev/null
+++ b/get_structural_metadata.swift
@@ -0,0 +1,137 @@
+import Photos
+
+struct AlbumData: Codable {
+ var localIdentifier: String
+ var localizedTitle: String?
+ var assetIdentifiers: [String]
+}
+
+var allAlbums: [AlbumData] = []
+
+PHAssetCollection
+ .fetchAssetCollections(with: .album, subtype: .albumRegular, options: nil)
+ .enumerateObjects({ (album, _, _) in
+ var assetIdentifiers: [String] = []
+
+ PHAsset
+ .fetchAssets(in: album, options: nil)
+ .enumerateObjects({ (asset, _, _) in
+ assetIdentifiers.append(asset.localIdentifier)
+ })
+
+ allAlbums.append(
+ AlbumData(
+ localIdentifier: album.localIdentifier,
+ localizedTitle: album.localizedTitle,
+ assetIdentifiers: assetIdentifiers
+ )
+ )
+ })
+
+struct AssetData: Codable {
+ var localIdentifier: String
+ var creationDate: Date?
+}
+
+var allAssets: [AssetData] = []
+
+PHAsset
+ .fetchAssets(with: nil)
+ .enumerateObjects({ (asset, _, _) in
+ allAssets.append(
+ AssetData(
+ localIdentifier: asset.localIdentifier,
+ creationDate: asset.creationDate
+ )
+ )
+ })
+
+struct Response: Codable {
+ var albums: [AlbumData]
+ var assets: [AssetData]
+}
+
+// /// Returns a list of album names for albums containing this asset.
+// func getAlbumsContainingAsset(asset: PHAsset) -> [String] {
+// let collections = PHAssetCollection.fetchAssetCollectionsContaining(
+// asset, with: .album, options: nil
+// )
+//
+// var titles: [String] = []
+//
+// collections.enumerateObjects({ (album, index, stop) in
+// if (album.localizedTitle != nil) {
+// titles.append(album.localizedTitle!)
+// }
+// })
+//
+// return titles
+// }
+//
+// let options = PHFetchOptions()
+// options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
+//
+// let all_assets = PHAsset.fetchAssets(with: options)
+// //
+// let index = IndexSet(integersIn: 0...5)
+//
+
+//
+// import Cocoa
+//
+// typealias UIImage = NSImage
+//
+// // https://stackoverflow.com/a/48755517/1558022
+// func getAssetThumbnail(asset: PHAsset, size: Double) -> NSImage {
+// let manager = PHImageManager.default()
+// let option = PHImageRequestOptions()
+// var thumbnail = UIImage()
+// option.isSynchronous = true
+// manager.requestImage(for: asset, targetSize: CGSize(width: size, height: size), contentMode: .aspectFit, options: option, resultHandler: {(result, info)->Void in
+// thumbnail = result!
+// })
+// return thumbnail
+// }
+//
+// func jpegDataFrom(image:NSImage) -> Data {
+// let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil)!
+// let bitmapRep = NSBitmapImageRep(cgImage: cgImage)
+// let jpegData = bitmapRep.representation(using: NSBitmapImageRep.FileType.jpeg, properties: [:])!
+// return jpegData
+// }
+//
+// var response: [PhotoData] = []
+//
+// for asset in all_assets.objects(at: index) {
+// let thumbnailPath = "/tmp/photos-reviewer/\(asset.localIdentifier)_65.jpg"
+//
+// let data = PhotoData(
+// uuid: asset.localIdentifier,
+// albums: getAlbumsContainingAsset(asset: asset),
+// thumbnailPath: thumbnailPath
+// // thumbnail: getAssetThumbnail(asset: all_assets.firstObject!, size: 65.0).base64String!
+// )
+//
+// // if !FileManager.default.fileExists(atPath: thumbnailPath) {
+// // let jpegData = jpegDataFrom(image: getAssetThumbnail(asset: asset, size: 65.0))
+// //
+// // try! FileManager.default.createDirectory(atPath: NSString(string: thumbnailPath).deletingLastPathComponent, withIntermediateDirectories: true, attributes: nil)
+// //
+// // try! jpegData.write(to: URL(fileURLWithPath: thumbnailPath), options: [])
+// // }
+//
+// response.append(data)
+// }
+//
+// print(NSDate().timeIntervalSince1970)
+// print(response)
+//
+let jsonEncoder = JSONEncoder()
+let jsonData = try jsonEncoder.encode(
+ Response(albums: allAlbums, assets: allAssets)
+)
+let json = String(data: jsonData, encoding: String.Encoding.utf8)
+print(json!)
+//
+// // print()
+// //
\ No newline at end of file