Skip to content

Commit

Permalink
Merge pull request #414 from Freika/fix/immich-search-endpoint-import
Browse files Browse the repository at this point in the history
Use POST /api/search/metadata endpoint to get geodata from Immich
  • Loading branch information
Freika authored Nov 18, 2024
2 parents f24d47c + 8891313 commit 7fca95a
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .app_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.16.4
0.16.5
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

# 0.16.5 - 2024-11-18

### Changed

- Dawarich now uses `POST /api/search/metadata` endpoint to get geodata from Immich.

# 0.16.4 - 2024-11-12

### Added
Expand Down
41 changes: 34 additions & 7 deletions app/services/immich/import_geodata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Immich::ImportGeodata

def initialize(user)
@user = user
@immich_api_base_url = "#{user.settings['immich_url']}/api"
@immich_api_base_url = "#{user.settings['immich_url']}/api/search/metadata"
@immich_api_key = user.settings['immich_api_key']
end

Expand Down Expand Up @@ -40,17 +40,44 @@ def headers
end

def retrieve_immich_data
1970.upto(Time.zone.today.year).flat_map do |year|
(1..12).map do |month_number|
url = "#{immich_api_base_url}/timeline/bucket?size=MONTH&timeBucket=#{year}-#{month_number}-01"
page = 1
data = []
max_pages = 1000 # Prevent infinite loop

JSON.parse(HTTParty.get(url, headers:).body)
end
while page <= max_pages
Rails.logger.debug "Retrieving next page: #{page}"
body = request_body(page)
response = JSON.parse(HTTParty.post(immich_api_base_url, headers: headers, body: body).body)

items = response.dig('assets', 'items')
Rails.logger.debug "#{items.size} items found"

break if items.empty?

data << items

Rails.logger.debug "next_page: #{response.dig('assets', 'nextPage')}"

page += 1

Rails.logger.debug "#{data.flatten.size} data size"
end

data.flatten
end

def request_body(page)
{
createdAfter: '1970-01-01',
size: 1000,
page: page,
order: 'asc',
withExif: true
}
end

def parse_immich_data(immich_data)
geodata = immich_data.flatten.map do |asset|
geodata = immich_data.map do |asset|
next unless valid?(asset)

extract_geodata(asset)
Expand Down
69 changes: 63 additions & 6 deletions spec/services/immich/import_geodata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,68 @@
end
let(:immich_data) do
{
"exifInfo": {
"dateTimeOriginal": '2022-12-31T23:21:53.140Z',
"latitude": 52.0000,
"longitude": 13.0000
"albums": {
"total": 0,
"count": 0,
"items": [],
"facets": []
},
"assets": {
"total": 1000,
"count": 1000,
"items": [
{
"id": "7fe486e3-c3ba-4b54-bbf9-1281b39ed15c",
"deviceAssetId": "IMG_9913.jpeg-1168914",
"ownerId": "f579f328-c355-438c-a82c-fe3390bd5f08",
"deviceId": "CLI",
"libraryId": nil,
"type": "IMAGE",
"originalPath": "upload/library/admin/2023/2023-06-08/IMG_9913.jpeg",
"originalFileName": "IMG_9913.jpeg",
"originalMimeType": "image/jpeg",
"thumbhash": "4RgONQaZqYaH93g3h3p3d6RfPPrG",
"fileCreatedAt": "2023-06-08T07:58:45.637Z",
"fileModifiedAt": "2023-06-08T09:58:45.000Z",
"localDateTime": "2023-06-08T09:58:45.637Z",
"updatedAt": "2024-08-24T18:20:47.965Z",
"isFavorite": false,
"isArchived": false,
"isTrashed": false,
"duration": "0:00:00.00000",
"exifInfo": {
"make": "Apple",
"model": "iPhone 12 Pro",
"exifImageWidth": 4032,
"exifImageHeight": 3024,
"fileSizeInByte": 1168914,
"orientation": "6",
"dateTimeOriginal": "2023-06-08T07:58:45.637Z",
"modifyDate": "2023-06-08T07:58:45.000Z",
"timeZone": "Europe/Berlin",
"lensModel": "iPhone 12 Pro back triple camera 4.2mm f/1.6",
"fNumber": 1.6,
"focalLength": 4.2,
"iso": 320,
"exposureTime": "1/60",
"latitude": 52.11,
"longitude": 13.22,
"city": "Johannisthal",
"state": "Berlin",
"country": "Germany",
"description": "",
"projectionType": nil,
"rating": nil
},
"livePhotoVideoId": nil,
"people": [],
"checksum": "aL1edPVg4ZpEnS6xCRWNUY0pUS8=",
"isOffline": false,
"hasMetadata": true,
"duplicateId": "88a34bee-783d-46e4-aa52-33b75ffda375",
"resized": true
}
]
}
}.to_json
end
Expand All @@ -23,8 +81,7 @@
before do
stub_request(
:any,
%r{http://immich\.app/api/timeline/bucket\?size=MONTH&timeBucket=(19[7-9][0-9]|20[0-9]{2})-(0?[1-9]|1[0-2])-(0?[1-9]|[12][0-9]|3[01])}
).to_return(status: 200, body: immich_data, headers: {})
'http://immich.app/api/search/metadata').to_return(status: 200, body: immich_data, headers: {})
end

it 'creates import' do
Expand Down

0 comments on commit 7fca95a

Please sign in to comment.