MCPcopy
hub / github.com/photonixapp/photonix / record_photo

Function record_photo

photonix/photos/utils/db.py:27–194  ·  view source on GitHub ↗
(path, library, inotify_event_type=None)

Source from the content-addressed store, hash-verified

25
26
27def record_photo(path, library, inotify_event_type=None):
28 logger.info(f'Recording photo {path}')
29
30 mimetype = get_mimetype(path)
31
32 if not imghdr.what(path) and not mimetype in MIMETYPE_WHITELIST and subprocess.run(['dcraw', '-i', path]).returncode:
33 logger.error(f'File is not a supported type: {path} ({mimetype})')
34 return None
35
36 if type(library) == Library:
37 library_id = library.id
38 else:
39 library_id = str(library)
40 try:
41 photo_file = PhotoFile.objects.get(path=path)
42 except PhotoFile.DoesNotExist:
43 photo_file = PhotoFile()
44
45 if inotify_event_type in ['DELETE', 'MOVED_FROM']:
46 if PhotoFile.objects.filter(path=path).exists():
47 return delete_photo_record(photo_file)
48 else:
49 return True
50
51 file_modified_at = datetime.fromtimestamp(os.stat(path).st_mtime, tz=utc)
52
53 if photo_file and photo_file.file_modified_at == file_modified_at:
54 return True
55
56 metadata = PhotoMetadata(path)
57 date_taken = None
58 possible_date_keys = ['Create Date', 'Date/Time Original', 'Date Time Original', 'Date/Time', 'Date Time', 'GPS Date/Time', 'File Modification Date/Time']
59 for date_key in possible_date_keys:
60 date_taken = parse_datetime(metadata.get(date_key))
61 if date_taken:
62 break
63 # If EXIF data not found.
64 date_taken = date_taken or datetime.strptime(time.ctime(os.path.getctime(path)), "%a %b %d %H:%M:%S %Y")
65
66 camera = None
67 camera_make = metadata.get('Make', '')[:Camera.make.field.max_length]
68 camera_model = metadata.get('Camera Model Name', '')
69 if camera_model:
70 camera_model = camera_model.replace(camera_make, '').strip()
71 camera_model = camera_model[:Camera.model.field.max_length]
72 if camera_make and camera_model:
73 try:
74 camera = Camera.objects.get(library_id=library_id, make=camera_make, model=camera_model)
75 if date_taken < camera.earliest_photo:
76 camera.earliest_photo = date_taken
77 camera.save()
78 if date_taken > camera.latest_photo:
79 camera.latest_photo = date_taken
80 camera.save()
81 except Camera.DoesNotExist:
82 camera = Camera(library_id=library_id, make=camera_make, model=camera_model,
83 earliest_photo=date_taken, latest_photo=date_taken)
84 camera.save()

Callers 10

import_photos_from_dirFunction · 0.90
import_photos_in_placeFunction · 0.90
record_photo_asyncMethod · 0.90
import_photosMethod · 0.90
photo_fixture_snowFunction · 0.90
photo_fixture_snowFunction · 0.90
photo_fixture_rawFunction · 0.90
photo_fixture_snowFunction · 0.90
photo_fixture_treeFunction · 0.90
defaults_valuesMethod · 0.90

Calls 13

getMethod · 0.95
get_mimetypeFunction · 0.90
PhotoFileClass · 0.90
PhotoMetadataClass · 0.90
parse_datetimeFunction · 0.90
CameraClass · 0.90
LensClass · 0.90
parse_gps_locationFunction · 0.90
PhotoClass · 0.90
TaskClass · 0.90
delete_photo_recordFunction · 0.85
runMethod · 0.80

Tested by 6

photo_fixture_snowFunction · 0.72
photo_fixture_snowFunction · 0.72
photo_fixture_rawFunction · 0.72
photo_fixture_snowFunction · 0.72
photo_fixture_treeFunction · 0.72
defaults_valuesMethod · 0.72