Implement importing geodata from photoprism

This commit is contained in:
Eugene Burmakin 2024-12-03 15:59:34 +01:00
parent e17b671c9c
commit ba2a95233c
4 changed files with 31 additions and 25 deletions

View file

@ -24,12 +24,12 @@ class Imports::Create
def parser(source)
# Bad classes naming by the way, they are not parsers, they are point creators
case source
when 'google_semantic_history' then GoogleMaps::SemanticHistoryParser
when 'google_phone_takeout' then GoogleMaps::PhoneTakeoutParser
when 'owntracks' then OwnTracks::ExportParser
when 'gpx' then Gpx::TrackParser
when 'immich_api' then Immich::ImportParser
when 'geojson' then Geojson::ImportParser
when 'google_semantic_history' then GoogleMaps::SemanticHistoryParser
when 'google_phone_takeout' then GoogleMaps::PhoneTakeoutParser
when 'owntracks' then OwnTracks::ExportParser
when 'gpx' then Gpx::TrackParser
when 'geojson' then Geojson::ImportParser
when 'immich_api', 'photoprism_api' then Photos::ImportParser
end
end

View file

@ -11,23 +11,29 @@ class Photoprism::ImportGeodata
def call
photoprism_data = retrieve_photoprism_data
return log_no_data if photoprism_data.empty?
log_no_data and return if photoprism_data.empty?
photoprism_data_json = parse_photoprism_data(photoprism_data)
file_name = file_name(photoprism_data_json)
import = user.imports.find_or_initialize_by(name: file_name, source: :photoprism_api)
create_import_failed_notification(import.name) and return unless import.new_record?
import.raw_data = photoprism_data_json
import.save!
ImportJob.perform_later(user.id, import.id)
json_data = parse_photoprism_data(photoprism_data)
create_and_process_import(json_data)
end
private
def create_and_process_import(json_data)
import = find_or_create_import(json_data)
return create_import_failed_notification(import.name) unless import.new_record?
import.update!(raw_data: json_data)
ImportJob.perform_later(user.id, import.id)
end
def find_or_create_import(json_data)
user.imports.find_or_initialize_by(
name: file_name(json_data),
source: :photoprism_api
)
end
def retrieve_photoprism_data
Photoprism::RequestPhotos.new(user, start_date:, end_date:).call
end
@ -52,9 +58,9 @@ class Photoprism::ImportGeodata
def extract_geodata(asset)
{
latitude: asset.dig('exifInfo', 'latitude'),
longitude: asset.dig('exifInfo', 'longitude'),
timestamp: Time.zone.parse(asset.dig('exifInfo', 'dateTimeOriginal')).to_i
latitude: asset['Lat'],
longitude: asset['Lng'],
timestamp: Time.zone.parse(asset['TakenAt']).to_i
}
end
@ -72,8 +78,8 @@ class Photoprism::ImportGeodata
end
def file_name(photoprism_data_json)
from = Time.zone.at(photoprism_data_json.first[:timestamp]).to_date
to = Time.zone.at(photoprism_data_json.last[:timestamp]).to_date
from = Time.zone.at(photoprism_data_json.first[:timestamp]).to_date
to = Time.zone.at(photoprism_data_json.last[:timestamp]).to_date
"photoprism-geodata-#{user.email}-from-#{from}-to-#{to}.json"
end

View file

@ -1,6 +1,6 @@
# frozen_string_literal: true
class Immich::ImportParser
class Photos::ImportParser
include Imports::Broadcaster
attr_reader :import, :json, :user_id

View file

@ -2,7 +2,7 @@
require 'rails_helper'
RSpec.describe Immich::ImportParser do
RSpec.describe Photos::ImportParser do
describe '#call' do
subject(:service) { described_class.new(import, user.id).call }