Skip to content

Commit

Permalink
Add extra metadata to media.
Browse files Browse the repository at this point in the history
  • Loading branch information
wragge committed Apr 3, 2022
1 parent faa4209 commit b8daf93
Show file tree
Hide file tree
Showing 6 changed files with 442 additions and 145 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,4 @@ checklink/cookies.txt
.gitconfig

*.sqlite
omeka_s_tools.code-workspace
285 changes: 206 additions & 79 deletions api.ipynb

Large diffs are not rendered by default.

254 changes: 205 additions & 49 deletions docs/api.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion omeka_s_tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.1"
__version__ = "0.2.0"
43 changes: 28 additions & 15 deletions omeka_s_tools/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ def add_item(self, payload, media_files=None, template_id=None, class_id=None, i
'''
if template_id:
payload['o:resource_template'] = self.format_resource_id(template_id, 'resource_templates')
# If class is not set explicitly, use class associated with template
if not class_id:
template = self.get_resource_by_id(template_id, 'resource_templates')
class_id = template['o:resource_class']['o:id']
if class_id:
payload['o:resource_class'] = self.format_resource_id(class_id, 'resource_classes')
if item_set_id:
Expand Down Expand Up @@ -447,38 +451,47 @@ def update_resource(self, payload, resource_type='items'):
response = self.s.put(f'{self.api_url}/{resource_type}/{payload["o:id"]}', json=payload, params=self.params)
return response.json()

def add_media_to_item(self, item_id, media_file):
def add_media_to_item(self, item_id, media_file, payload={}, template_id=None, class_id=None):
'''
Upload a media file and associate it with an existing item.
Parameters:
* `item_id` - the Omeka id of the item this media file should be added to
* `media_file` - the media file to be uploaded, c
The value of `media_file` can be either:
* a path to an image/media file (filename is used as title)
* a dict containing `title` and `path` values
The path values can either be strings or pathlib Paths.
* `media_path` - a path to an image/media file (string or pathlib Path)
* `payload` (optional) - metadata to attach to media object, either
a dict generated by `prepare_item_payload()` or `prepare_item_payload_using_template()`,
or a string which is used as the value for `dcterms:title`.
* `template_id` - internal Omeka identifier of a resource template you want to attach to this item
* `class_id` - internal Omeka identifier of a resource class you want to attach to this item
Returns:
* a dict providing a JSON-LD representation of the new media object
'''
files = {}
# For backwards compatibility
if isinstance(media_file, dict):
title = media_file['title']
path = Path(media_file['path'])
else:
path = Path(media_file)
title = path.name
path = media_file['path']
payload = media_file['title']
# Make sure path is a Path object
path = Path(media_file)
if isinstance(payload, str):
payload = self.prepare_item_payload({'dcterms:title': [payload]})
if template_id:
payload['o:resource_template'] = self.format_resource_id(template_id, 'resource_templates')
if not class_id:
template = self.get_resource_by_id(template_id, 'resource_templates')
class_id = template['o:resource_class']['o:id']
if class_id:
payload['o:resource_class'] = self.format_resource_id(class_id, 'resource_classes')
file_data = {
'o:ingester': 'upload',
'file_index': '0',
'o:source': path.name,
'o:item': {'o:id': item_id},
'dcterms:title': [{'property_id': 1, '@value': title, 'type': 'literal'}]
}
payload.update(file_data)
files[f'file[0]'] = path.read_bytes()
files['data'] = (None, json.dumps(file_data), 'application/json')
files['data'] = (None, json.dumps(payload), 'application/json')
response = self.s.post(f'{self.api_url}/media', files=files, params=self.params)
return response.json()

Expand Down
2 changes: 1 addition & 1 deletion settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ author = Tim Sherratt
author_email = [email protected]
copyright = Tim Sherratt
branch = master
version = 0.1.1
version = 0.2.0
min_python = 3.8
audience = Developers
language = English
Expand Down

0 comments on commit b8daf93

Please sign in to comment.