tidy up code, only interested in images
- ID
ac5741b- date
2023-05-13 12:48:22+00:00- author
Alex Chan <alex@alexwlchan.net>- parent
462c2db- message
tidy up code, only interested in images- changed files
6 files, 88 additions, 156 deletions
Changed files
actions/get_structural_metadata.swift (0) → actions/get_structural_metadata.swift (1400)
diff --git a/actions/get_structural_metadata.swift b/actions/get_structural_metadata.swift
new file mode 100644
index 0000000..dcd0075
--- /dev/null
+++ b/actions/get_structural_metadata.swift
@@ -0,0 +1,60 @@
+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: String?
+}
+
+var allAssets: [AssetData] = []
+
+PHAsset
+ .fetchAssets(with: PHAssetMediaType.image, options: nil)
+ .enumerateObjects({ (asset, _, _) in
+ print(asset)
+ allAssets.append(
+ AssetData(
+ localIdentifier: asset.localIdentifier,
+ creationDate: asset.creationDate?.ISO8601Format()
+ )
+ )
+ })
+
+struct Response: Codable {
+ var albums: [AlbumData]
+ var assets: [AssetData]
+}
+
+let jsonEncoder = JSONEncoder()
+let jsonData = try jsonEncoder.encode(
+ Response(albums: allAlbums, assets: allAssets)
+)
+let json = String(data: jsonData, encoding: String.Encoding.utf8)
+print(json!)
get_structural_metadata.swift (3963) → get_structural_metadata.swift (0)
diff --git a/get_structural_metadata.swift b/get_structural_metadata.swift
deleted file mode 100644
index cd52d19..0000000
--- a/get_structural_metadata.swift
+++ /dev/null
@@ -1,137 +0,0 @@
-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: String?
-}
-
-var allAssets: [AssetData] = []
-
-PHAsset
- .fetchAssets(with: nil)
- .enumerateObjects({ (asset, _, _) in
- allAssets.append(
- AssetData(
- localIdentifier: asset.localIdentifier,
- creationDate: asset.creationDate?.ISO8601Format()
- )
- )
- })
-
-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
requirements.in (0) → requirements.in (13)
diff --git a/requirements.in b/requirements.in
new file mode 100644
index 0000000..31717ce
--- /dev/null
+++ b/requirements.in
@@ -0,0 +1 @@
+Flask==2.0.1
requirements.txt (0) → requirements.txt (340)
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..227e17a
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,20 @@
+#
+# This file is autogenerated by pip-compile with Python 3.9
+# by the following command:
+#
+# pip-compile
+#
+click==8.1.3
+ # via flask
+flask==2.0.1
+ # via -r requirements.in
+itsdangerous==2.1.2
+ # via flask
+jinja2==3.1.2
+ # via flask
+markupsafe==2.1.2
+ # via
+ # jinja2
+ # werkzeug
+werkzeug==2.3.4
+ # via flask
server.py (5591) → server.py (5461)
diff --git a/server.py b/server.py
index 09a2f70..9475a42 100755
--- a/server.py
+++ b/server.py
@@ -29,7 +29,7 @@ def get_asset_state(asset):
class PhotosData:
def __init__(self):
- data = json.loads(subprocess.check_output(['swift', 'get_structural_metadata.swift']))
+ data = json.loads(subprocess.check_output(['swift', 'actions/get_structural_metadata.swift']))
all_assets = sorted(data['assets'], key=lambda a: a['creationDate'])
self.all_positions = {asset['localIdentifier']: i for i, asset in enumerate(all_assets)}
@@ -163,10 +163,7 @@ def _perform_action(request, callback):
callback(local_identifier)
- if request.args['direction'] == 'left':
- redirect_to = photos_data.all_assets[position - 1]['localIdentifier']
- else:
- redirect_to = photos_data.all_assets[position + 1]['localIdentifier']
+ redirect_to = photos_data.all_assets[position - 1]['localIdentifier']
return redirect(url_for('index', localIdentifier=redirect_to))
@@ -186,8 +183,6 @@ def needs_action():
return _perform_action(request, photos_data.needs_action)
-
-
@app.route('/image')
def image():
local_identifier = request.args['localIdentifier']
templates/index.html (8259) → templates/index.html (7942)
diff --git a/templates/index.html b/templates/index.html
index e7987d6..ad4a00c 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -190,27 +190,20 @@
<img id="big" src="{{ url_for('image', localIdentifier=this_asset['localIdentifier']) }}">
-
-<!--{% for a in assets %}
- <img src="{{ url_for('thumbnail', localIdentifier=a['localIdentifier']) }}">
-{% endfor %} -->
-
<script>
document.onkeydown = function(e) {
- const direction = new URLSearchParams(window.location.search).get('direction') || 'left';
-
if (e.key === "ArrowLeft") {
- window.location = "/?localIdentifier={{ prev_five[-1]['localIdentifier'] }}&direction=left";
+ window.location = "/?localIdentifier={{ prev_five[-1]['localIdentifier'] }}";
} else if (e.key === "ArrowRight") {
{% if next_five %}
- window.location = "/?localIdentifier={{ next_five[0]['localIdentifier'] }}&direction=right";
+ window.location = "/?localIdentifier={{ next_five[0]['localIdentifier'] }}";
{% endif %}
} else if (e.key === "1") {
- window.location = "/actions/flag?localIdentifier={{ this_asset['localIdentifier'] }}&direction=" + direction;
+ window.location = "/actions/flag?localIdentifier={{ this_asset['localIdentifier'] }}";
} else if (e.key === "2") {
- window.location = "/actions/reject?localIdentifier={{ this_asset['localIdentifier'] }}&direction=" + direction;
+ window.location = "/actions/reject?localIdentifier={{ this_asset['localIdentifier'] }}";
} else if (e.key === "3") {
- window.location = "/actions/needs_action?localIdentifier={{ this_asset['localIdentifier'] }}&direction=" + direction;
+ window.location = "/actions/needs_action?localIdentifier={{ this_asset['localIdentifier'] }}";
}
}
</script>