Skip to content

Commit

Permalink
Merge pull request #39 from internetarchive/fix-v2-single-image
Browse files Browse the repository at this point in the history
Fix v2 single image
  • Loading branch information
hadro authored Oct 26, 2023
2 parents 7b92a69 + 419ff20 commit f8a4734
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 29 deletions.
80 changes: 53 additions & 27 deletions iiify/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ def create_collection3(identifier, domain, page=1, rows=1000):
child = CollectionRef(id=f"{domain}{identifier}/{page}/collection.json", type="Collection", label={ "en": [f"Page {page} of {pages}"]})
collection.add_item(child)

print ('Returning collection')
return json.loads(collection.jsonld())

def manifest_page(identifier, label='', page='', width='', height='', metadata=None):
Expand Down Expand Up @@ -228,35 +227,64 @@ def create_manifest(identifier, domain=None, page=None):
'itemPath': subPrefix,
'itemId': identifier
})
data = r.json()

manifest.update({
'label': data['title'],
'thumbnail': {
'@id': data['previewImage']
},
})

if page:
if r.status_code != 200:
# If the bookdata failed then treat as a single image
fileName = ""
for f in resp['files']:
if valid_filetype(f['name']) \
and f['source'].lower() == 'original' \
and 'thumb' not in f['name']:
fileName = f['name']
break

if not fileName:
# Original wasn't an image
for f in resp['files']:
if '_jp2.zip' in f['name']:
fileName = f"{f['name']}/{f['name'].replace('.zip','')}/{f['name'].replace('jp2.zip','0000.jp2')}"

imgId = f"{identifier}/{fileName}".replace('/','%2f')
info_resp = requests.get(f"{IMG_SRV}/2/{imgId}/info.json")
info = info_resp.json()
manifest['sequences'][0]['canvases'].append(
manifest_page(
identifier = "%s%s$%s" % (domain, identifier, page),
label=data['pageNums'][page],
width=data['pageWidths'][page],
height=data['pageHeights'][page]
identifier= info['@id'],
label=metadata['title'],
width=info['width'],
height=info['height'],
metadata=metadata
)
)
return manifest

for page in range(0, len(data.get('leafNums', []))):
manifest['sequences'][0]['canvases'].append(
manifest_page(
identifier = "%s%s$%s" % (domain, identifier, page),
label=data['pageNums'][page],
width=data['pageWidths'][page],
height=data['pageHeights'][page]
else:
data = r.json()

manifest.update({
'label': data['title'],
'thumbnail': {
'@id': data['previewImage']
},
})

if page:
manifest['sequences'][0]['canvases'].append(
manifest_page(
identifier = "%s%s$%s" % (domain, identifier, page),
label=data['pageNums'][page],
width=data['pageWidths'][page],
height=data['pageHeights'][page]
)
)
return manifest

for page in range(0, len(data.get('leafNums', []))):
manifest['sequences'][0]['canvases'].append(
manifest_page(
identifier = "%s%s$%s" % (domain, identifier, page),
label=data['pageNums'][page],
width=data['pageWidths'][page],
height=data['pageHeights'][page]
)
)
)
return manifest

def singleImage(metadata, identifier, manifest, uri):
Expand Down Expand Up @@ -361,7 +389,6 @@ def create_manifest3(identifier, domain=None, page=None):

bookReaderURL = f"https://{metadata.get('server')}/BookReader/BookReaderJSIA.php?id={identifier}&itemPath={metadata.get('dir')}&server={metadata.get('server')}&format=jsonp&subPrefix={subprefix}"

print (f'Book reader url: {bookReaderURL}')
bookreader = requests.get(bookReaderURL).json()
if 'error' in bookreader:
# Image stack not found. Maybe a single image
Expand Down Expand Up @@ -602,7 +629,6 @@ def ia_resolver(identifier):
and 'thumb' not in f['name'] )
itempath = os.path.join(identifier, f['name'])
url = '%s/download/%s' % (ARCHIVE, itempath)
print(url)
r = requests.get(url, stream=True, allow_redirects=True)

elif mediatype.lower() == 'texts' and leaf:
Expand Down
10 changes: 8 additions & 2 deletions tests/test_manifests_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ class TestManifests(unittest.TestCase):
def setUp(self) -> None:
self.test_app = FlaskClient(app)

def test_v3_image_manifest(self):
def test_v2_image_manifest(self):
resp = self.test_app.get("/iiif/2/rashodgson68/manifest.json")
self.assertEqual(resp.status_code, 200)
manifest = resp.json

self.assertEqual(manifest['@id'], 'https://iiif.archivelab.org/iiif/rashodgson68/manifest.json', 'V2 Manifest ID has changed')
self.assertEqual(manifest['@type'], "sc:Manifest", f"Unexpected type. Expected Manifest got {manifest['@type']}")
self.assertEqual(len(manifest['sequences'][0]['canvases']),32,f"Expected 32 canvases but got: {len(manifest['sequences'][0]['canvases'])}")
self.assertEqual(manifest['sequences'][0]['canvases'][0]['@id'],"https://iiif.archivelab.org/iiif/rashodgson68$0/canvas",f"v2 canvas id has changed")
self.assertEqual(manifest['sequences'][0]['canvases'][0]['@id'],"https://iiif.archivelab.org/iiif/rashodgson68$0/canvas",f"v2 canvas id has changed")


def test_text_which_is_image(self):
resp = self.test_app.get("/iiif/2/z-2-2-18-14-9/manifest.json")

self.assertEqual(resp.status_code, 200)

0 comments on commit f8a4734

Please sign in to comment.