From b49e6e395d0487a39aec9510c244f720d3871799 Mon Sep 17 00:00:00 2001 From: Glen Robson Date: Fri, 9 Feb 2024 11:00:26 +0000 Subject: [PATCH 1/8] Handling object which has multiple images but no image stack --- iiify/resolver.py | 39 ++++++++++++++++++++++++++++++++++++++- tests/test_manifests.py | 7 +++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/iiify/resolver.py b/iiify/resolver.py index 986c538..d65a0b0 100644 --- a/iiify/resolver.py +++ b/iiify/resolver.py @@ -37,6 +37,27 @@ def getids(q, limit=1000, cursor=''): }, allow_redirects=True, timeout=None) return r.json() +def checkMultiItem(metadata): + # Maybe add call to book stack to see if that works first + + # Count the number of each original file + file_types = {} + for file in metadata['files']: + if file['source'] == "original": + if file['format'] not in file_types: + file_types[file['format']] = 0 + + file_types[file['format']] += 1 + + # If there is multiple files of the same type then return the first format + # Will have to see if there are objects with multiple images and formats + for format in file_types: + if file_types[format] > 1 and format.lower() in valid_filetypes: + return (True, format) + + return (False, None) + + def to_mimetype(format): formats = { "VBR MP3": "audio/mp3", @@ -363,6 +384,7 @@ def addMetadata(item, identifier, metadata, collection=False): item.metadata = manifest_metadata + def create_manifest3(identifier, domain=None, page=None): # Get item metadata metadata = requests.get('%s/metadata/%s' % (ARCHIVE, identifier)).json() @@ -448,7 +470,22 @@ def create_manifest3(identifier, domain=None, page=None): elif mediatype == 'image': - singleImage(metadata, identifier, manifest, uri) + (multiFile, format) = checkMultiItem(metadata) + print (f"Checking multiFile {multiFile} {format}") + if multiFile: + # Create multi file manifest + for file in metadata['files']: + if file['source'] == "original" and file['format'] == format: + imgId = f"{identifier}/{file['name']}".replace('/','%2f') + imgURL = f"{IMG_SRV}/3/{imgId}" + + manifest.make_canvas_from_iiif(url=imgURL, + id=f"{URI_PRIFIX}/{identifier}/canvas", + label="1", + anno_page_id=f"{uri}/annotationPage/1", + anno_id=f"{uri}/annotation/1") + else: + singleImage(metadata, identifier, manifest, uri) elif mediatype == 'audio' or mediatype == 'etree': # sort the files into originals and derivatives, splitting the derivatives into buckets based on the original originals = [] diff --git a/tests/test_manifests.py b/tests/test_manifests.py index 3fa25ba..826ca39 100644 --- a/tests/test_manifests.py +++ b/tests/test_manifests.py @@ -144,6 +144,13 @@ def test_metadata_array(self): manifest = resp.json self.assertTrue(len(manifest['summary']['none']) > 1, f"Expected multiple summary values, but got {manifest['summary']['none']}") + def test_multi_file(self): + resp = self.test_app.get("/iiif/3/arkivkopia.se-lms-G70-48.3/manifest.json") + self.assertEqual(resp.status_code, 200) + manifest = resp.json + self.assertEqual(len(manifest['items']),3, f"Expected three canvases, but got {len(manifest['items'])}") + + ''' to test: kaled_jalil (no derivatives) From 28423ce13b91d3fe4b294288183cb8b107317ab3 Mon Sep 17 00:00:00 2001 From: Glen Robson Date: Fri, 9 Feb 2024 12:27:27 +0000 Subject: [PATCH 2/8] Tackling #46 returning empty canvas if image request failed --- iiify/resolver.py | 14 ++++++++++++-- tests/test_manifests.py | 5 +++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/iiify/resolver.py b/iiify/resolver.py index d65a0b0..df4bb84 100644 --- a/iiify/resolver.py +++ b/iiify/resolver.py @@ -48,6 +48,7 @@ def checkMultiItem(metadata): file_types[file['format']] = 0 file_types[file['format']] += 1 + print (file_types) # If there is multiple files of the same type then return the first format # Will have to see if there are objects with multiple images and formats @@ -472,6 +473,8 @@ def create_manifest3(identifier, domain=None, page=None): elif mediatype == 'image': (multiFile, format) = checkMultiItem(metadata) print (f"Checking multiFile {multiFile} {format}") + # st-anthony-relics-01%2fAuronzo-ComuneCortina.jpeg + # st-anthony-relics-01%2FStAnthony-Relics_01.jpeg if multiFile: # Create multi file manifest for file in metadata['files']: @@ -479,11 +482,18 @@ def create_manifest3(identifier, domain=None, page=None): imgId = f"{identifier}/{file['name']}".replace('/','%2f') imgURL = f"{IMG_SRV}/3/{imgId}" - manifest.make_canvas_from_iiif(url=imgURL, + try: + manifest.make_canvas_from_iiif(url=imgURL, id=f"{URI_PRIFIX}/{identifier}/canvas", - label="1", + label=f"{file['name']}", anno_page_id=f"{uri}/annotationPage/1", anno_id=f"{uri}/annotation/1") + except requests.exceptions.HTTPError as error: + print (f'Failed to get {imgURL}') + manifest.make_canvas(label=f"Failed to load {file['name']} from Image Server", + summary=f"Got {error}", + id=f"{URI_PRIFIX}/{identifier}/canvas", + height=1800, width=1200) else: singleImage(metadata, identifier, manifest, uri) elif mediatype == 'audio' or mediatype == 'etree': diff --git a/tests/test_manifests.py b/tests/test_manifests.py index 826ca39..18a73b2 100644 --- a/tests/test_manifests.py +++ b/tests/test_manifests.py @@ -150,6 +150,11 @@ def test_multi_file(self): manifest = resp.json self.assertEqual(len(manifest['items']),3, f"Expected three canvases, but got {len(manifest['items'])}") + def test_multi_file(self): + resp = self.test_app.get("/iiif/3/st-anthony-relics-01/manifest.json") + self.assertEqual(resp.status_code, 200) + manifest = resp.json + self.assertEqual(len(manifest['items']),5, f"Expected five canvases, but got {len(manifest['items'])}") ''' to test: From 4b9c44988677a7ef000c424c58af005a55f0c4bb Mon Sep 17 00:00:00 2001 From: Glen Robson Date: Fri, 9 Feb 2024 12:35:06 +0000 Subject: [PATCH 3/8] Removing debug --- iiify/resolver.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/iiify/resolver.py b/iiify/resolver.py index df4bb84..e81d030 100644 --- a/iiify/resolver.py +++ b/iiify/resolver.py @@ -48,7 +48,7 @@ def checkMultiItem(metadata): file_types[file['format']] = 0 file_types[file['format']] += 1 - print (file_types) + #print (file_types) # If there is multiple files of the same type then return the first format # Will have to see if there are objects with multiple images and formats @@ -472,9 +472,7 @@ def create_manifest3(identifier, domain=None, page=None): elif mediatype == 'image': (multiFile, format) = checkMultiItem(metadata) - print (f"Checking multiFile {multiFile} {format}") - # st-anthony-relics-01%2fAuronzo-ComuneCortina.jpeg - # st-anthony-relics-01%2FStAnthony-Relics_01.jpeg + #print (f"Checking multiFile {multiFile} {format}") if multiFile: # Create multi file manifest for file in metadata['files']: From 64c70f7bde46989690d8ffb63266fc7f8eaab6fc Mon Sep 17 00:00:00 2001 From: Glen Robson Date: Thu, 25 Apr 2024 23:27:44 +0100 Subject: [PATCH 4/8] Ensuring canvas_id is unique --- iiify/resolver.py | 6 ++++-- tests/test_manifests.py | 8 ++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/iiify/resolver.py b/iiify/resolver.py index e81d030..c84d6ff 100644 --- a/iiify/resolver.py +++ b/iiify/resolver.py @@ -472,17 +472,19 @@ def create_manifest3(identifier, domain=None, page=None): elif mediatype == 'image': (multiFile, format) = checkMultiItem(metadata) - #print (f"Checking multiFile {multiFile} {format}") + print (f"Checking multiFile {multiFile} {format}") if multiFile: # Create multi file manifest + pageCount = 0 for file in metadata['files']: if file['source'] == "original" and file['format'] == format: imgId = f"{identifier}/{file['name']}".replace('/','%2f') imgURL = f"{IMG_SRV}/3/{imgId}" + pageCount += 1 try: manifest.make_canvas_from_iiif(url=imgURL, - id=f"{URI_PRIFIX}/{identifier}/canvas", + id=f"{URI_PRIFIX}/{identifier}${pageCount}/canvas", label=f"{file['name']}", anno_page_id=f"{uri}/annotationPage/1", anno_id=f"{uri}/annotation/1") diff --git a/tests/test_manifests.py b/tests/test_manifests.py index 18a73b2..a899002 100644 --- a/tests/test_manifests.py +++ b/tests/test_manifests.py @@ -144,17 +144,21 @@ def test_metadata_array(self): manifest = resp.json self.assertTrue(len(manifest['summary']['none']) > 1, f"Expected multiple summary values, but got {manifest['summary']['none']}") - def test_multi_file(self): + def test_multi_file_image(self): resp = self.test_app.get("/iiif/3/arkivkopia.se-lms-G70-48.3/manifest.json") self.assertEqual(resp.status_code, 200) manifest = resp.json self.assertEqual(len(manifest['items']),3, f"Expected three canvases, but got {len(manifest['items'])}") + firstCanvasId = manifest['items'][0]['id'] + for i in range(1, len(manifest['items'])): + self.assertNotEqual(manifest['items'][i]['id'], firstCanvasId, 'Canvas Ids need to be unique') + def test_multi_file(self): resp = self.test_app.get("/iiif/3/st-anthony-relics-01/manifest.json") self.assertEqual(resp.status_code, 200) manifest = resp.json - self.assertEqual(len(manifest['items']),5, f"Expected five canvases, but got {len(manifest['items'])}") + self.assertEqual(len(manifest['items']),6, f"Expected five canvases, but got {len(manifest['items'])}") ''' to test: From 9fc71efe88bb0f3aaf1cade781941e1e4af97c54 Mon Sep 17 00:00:00 2001 From: Glen Robson Date: Fri, 9 Feb 2024 11:00:26 +0000 Subject: [PATCH 5/8] Handling object which has multiple images but no image stack --- iiify/resolver.py | 39 ++++++++++++++++++++++++++++++++++++++- tests/test_manifests.py | 7 +++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/iiify/resolver.py b/iiify/resolver.py index 986c538..d65a0b0 100644 --- a/iiify/resolver.py +++ b/iiify/resolver.py @@ -37,6 +37,27 @@ def getids(q, limit=1000, cursor=''): }, allow_redirects=True, timeout=None) return r.json() +def checkMultiItem(metadata): + # Maybe add call to book stack to see if that works first + + # Count the number of each original file + file_types = {} + for file in metadata['files']: + if file['source'] == "original": + if file['format'] not in file_types: + file_types[file['format']] = 0 + + file_types[file['format']] += 1 + + # If there is multiple files of the same type then return the first format + # Will have to see if there are objects with multiple images and formats + for format in file_types: + if file_types[format] > 1 and format.lower() in valid_filetypes: + return (True, format) + + return (False, None) + + def to_mimetype(format): formats = { "VBR MP3": "audio/mp3", @@ -363,6 +384,7 @@ def addMetadata(item, identifier, metadata, collection=False): item.metadata = manifest_metadata + def create_manifest3(identifier, domain=None, page=None): # Get item metadata metadata = requests.get('%s/metadata/%s' % (ARCHIVE, identifier)).json() @@ -448,7 +470,22 @@ def create_manifest3(identifier, domain=None, page=None): elif mediatype == 'image': - singleImage(metadata, identifier, manifest, uri) + (multiFile, format) = checkMultiItem(metadata) + print (f"Checking multiFile {multiFile} {format}") + if multiFile: + # Create multi file manifest + for file in metadata['files']: + if file['source'] == "original" and file['format'] == format: + imgId = f"{identifier}/{file['name']}".replace('/','%2f') + imgURL = f"{IMG_SRV}/3/{imgId}" + + manifest.make_canvas_from_iiif(url=imgURL, + id=f"{URI_PRIFIX}/{identifier}/canvas", + label="1", + anno_page_id=f"{uri}/annotationPage/1", + anno_id=f"{uri}/annotation/1") + else: + singleImage(metadata, identifier, manifest, uri) elif mediatype == 'audio' or mediatype == 'etree': # sort the files into originals and derivatives, splitting the derivatives into buckets based on the original originals = [] diff --git a/tests/test_manifests.py b/tests/test_manifests.py index 3fa25ba..826ca39 100644 --- a/tests/test_manifests.py +++ b/tests/test_manifests.py @@ -144,6 +144,13 @@ def test_metadata_array(self): manifest = resp.json self.assertTrue(len(manifest['summary']['none']) > 1, f"Expected multiple summary values, but got {manifest['summary']['none']}") + def test_multi_file(self): + resp = self.test_app.get("/iiif/3/arkivkopia.se-lms-G70-48.3/manifest.json") + self.assertEqual(resp.status_code, 200) + manifest = resp.json + self.assertEqual(len(manifest['items']),3, f"Expected three canvases, but got {len(manifest['items'])}") + + ''' to test: kaled_jalil (no derivatives) From f673d68556a706b1dbdf38377f2468c813ef4553 Mon Sep 17 00:00:00 2001 From: Glen Robson Date: Fri, 9 Feb 2024 12:27:27 +0000 Subject: [PATCH 6/8] Tackling #46 returning empty canvas if image request failed --- iiify/resolver.py | 14 ++++++++++++-- tests/test_manifests.py | 5 +++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/iiify/resolver.py b/iiify/resolver.py index d65a0b0..df4bb84 100644 --- a/iiify/resolver.py +++ b/iiify/resolver.py @@ -48,6 +48,7 @@ def checkMultiItem(metadata): file_types[file['format']] = 0 file_types[file['format']] += 1 + print (file_types) # If there is multiple files of the same type then return the first format # Will have to see if there are objects with multiple images and formats @@ -472,6 +473,8 @@ def create_manifest3(identifier, domain=None, page=None): elif mediatype == 'image': (multiFile, format) = checkMultiItem(metadata) print (f"Checking multiFile {multiFile} {format}") + # st-anthony-relics-01%2fAuronzo-ComuneCortina.jpeg + # st-anthony-relics-01%2FStAnthony-Relics_01.jpeg if multiFile: # Create multi file manifest for file in metadata['files']: @@ -479,11 +482,18 @@ def create_manifest3(identifier, domain=None, page=None): imgId = f"{identifier}/{file['name']}".replace('/','%2f') imgURL = f"{IMG_SRV}/3/{imgId}" - manifest.make_canvas_from_iiif(url=imgURL, + try: + manifest.make_canvas_from_iiif(url=imgURL, id=f"{URI_PRIFIX}/{identifier}/canvas", - label="1", + label=f"{file['name']}", anno_page_id=f"{uri}/annotationPage/1", anno_id=f"{uri}/annotation/1") + except requests.exceptions.HTTPError as error: + print (f'Failed to get {imgURL}') + manifest.make_canvas(label=f"Failed to load {file['name']} from Image Server", + summary=f"Got {error}", + id=f"{URI_PRIFIX}/{identifier}/canvas", + height=1800, width=1200) else: singleImage(metadata, identifier, manifest, uri) elif mediatype == 'audio' or mediatype == 'etree': diff --git a/tests/test_manifests.py b/tests/test_manifests.py index 826ca39..18a73b2 100644 --- a/tests/test_manifests.py +++ b/tests/test_manifests.py @@ -150,6 +150,11 @@ def test_multi_file(self): manifest = resp.json self.assertEqual(len(manifest['items']),3, f"Expected three canvases, but got {len(manifest['items'])}") + def test_multi_file(self): + resp = self.test_app.get("/iiif/3/st-anthony-relics-01/manifest.json") + self.assertEqual(resp.status_code, 200) + manifest = resp.json + self.assertEqual(len(manifest['items']),5, f"Expected five canvases, but got {len(manifest['items'])}") ''' to test: From be2e866830a0b73c97abc58f13c754970bc9dbb4 Mon Sep 17 00:00:00 2001 From: Glen Robson Date: Fri, 9 Feb 2024 12:35:06 +0000 Subject: [PATCH 7/8] Removing debug --- iiify/resolver.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/iiify/resolver.py b/iiify/resolver.py index df4bb84..e81d030 100644 --- a/iiify/resolver.py +++ b/iiify/resolver.py @@ -48,7 +48,7 @@ def checkMultiItem(metadata): file_types[file['format']] = 0 file_types[file['format']] += 1 - print (file_types) + #print (file_types) # If there is multiple files of the same type then return the first format # Will have to see if there are objects with multiple images and formats @@ -472,9 +472,7 @@ def create_manifest3(identifier, domain=None, page=None): elif mediatype == 'image': (multiFile, format) = checkMultiItem(metadata) - print (f"Checking multiFile {multiFile} {format}") - # st-anthony-relics-01%2fAuronzo-ComuneCortina.jpeg - # st-anthony-relics-01%2FStAnthony-Relics_01.jpeg + #print (f"Checking multiFile {multiFile} {format}") if multiFile: # Create multi file manifest for file in metadata['files']: From e514fced520b527f49dbe14c151575ee699a5f80 Mon Sep 17 00:00:00 2001 From: Glen Robson Date: Thu, 25 Apr 2024 23:27:44 +0100 Subject: [PATCH 8/8] Ensuring canvas_id is unique --- iiify/resolver.py | 6 ++++-- tests/test_manifests.py | 8 ++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/iiify/resolver.py b/iiify/resolver.py index e81d030..c84d6ff 100644 --- a/iiify/resolver.py +++ b/iiify/resolver.py @@ -472,17 +472,19 @@ def create_manifest3(identifier, domain=None, page=None): elif mediatype == 'image': (multiFile, format) = checkMultiItem(metadata) - #print (f"Checking multiFile {multiFile} {format}") + print (f"Checking multiFile {multiFile} {format}") if multiFile: # Create multi file manifest + pageCount = 0 for file in metadata['files']: if file['source'] == "original" and file['format'] == format: imgId = f"{identifier}/{file['name']}".replace('/','%2f') imgURL = f"{IMG_SRV}/3/{imgId}" + pageCount += 1 try: manifest.make_canvas_from_iiif(url=imgURL, - id=f"{URI_PRIFIX}/{identifier}/canvas", + id=f"{URI_PRIFIX}/{identifier}${pageCount}/canvas", label=f"{file['name']}", anno_page_id=f"{uri}/annotationPage/1", anno_id=f"{uri}/annotation/1") diff --git a/tests/test_manifests.py b/tests/test_manifests.py index 18a73b2..a899002 100644 --- a/tests/test_manifests.py +++ b/tests/test_manifests.py @@ -144,17 +144,21 @@ def test_metadata_array(self): manifest = resp.json self.assertTrue(len(manifest['summary']['none']) > 1, f"Expected multiple summary values, but got {manifest['summary']['none']}") - def test_multi_file(self): + def test_multi_file_image(self): resp = self.test_app.get("/iiif/3/arkivkopia.se-lms-G70-48.3/manifest.json") self.assertEqual(resp.status_code, 200) manifest = resp.json self.assertEqual(len(manifest['items']),3, f"Expected three canvases, but got {len(manifest['items'])}") + firstCanvasId = manifest['items'][0]['id'] + for i in range(1, len(manifest['items'])): + self.assertNotEqual(manifest['items'][i]['id'], firstCanvasId, 'Canvas Ids need to be unique') + def test_multi_file(self): resp = self.test_app.get("/iiif/3/st-anthony-relics-01/manifest.json") self.assertEqual(resp.status_code, 200) manifest = resp.json - self.assertEqual(len(manifest['items']),5, f"Expected five canvases, but got {len(manifest['items'])}") + self.assertEqual(len(manifest['items']),6, f"Expected five canvases, but got {len(manifest['items'])}") ''' to test: