Skip to content

Commit

Permalink
Merge pull request #127 from ArchiveLabs/add-mediatypes
Browse files Browse the repository at this point in the history
Add mediatypes
  • Loading branch information
hadro authored Jul 27, 2023
2 parents 8bba7f6 + 657ecf7 commit 367ebe9
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 16 deletions.
83 changes: 67 additions & 16 deletions iiify/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,28 @@ def getids(q, limit=1000, cursor=''):
def to_mimetype(format):
formats = {
"VBR MP3": "audio/mp3",
"32Kbps MP3": "audio/mp3",
"56Kbps MP3": "audio/mp3",
"64Kbps MP3": "audio/mp3",
"96Kbps MP3": "audio/mp3",
"128Kbps MP3": "audio/mp3",
"Flac": "audio/flac",
"Ogg Vorbis": "audio/ogg",
"Ogg Video": "video/ogg",
"WAVE": "audio/wav",
"MPEG4": "video/mp4",
"24bit Flac": "audio/flac",
'Shorten': "audio/shn"
'Shorten': "audio/shn",
"MPEG2": "video/mpeg",
"512Kb MPEG4": "video/mpeg",
"HiRes MPEG4": "video/mpeg",
"h.264 MPEG4": "video/mpeg",
"h.264": "video/mpeg",
"Matroska": "video/x-matroska",
"Cinepack": "video/x-msvideo",
"AIFF": "audio/aiff",
"Apple Lossless Audio": "audio/x-m4a",
"MPEG-4 Audio": "audio/mp4"
}
return formats.get(format, "application/octet-stream")

Expand Down Expand Up @@ -366,7 +382,7 @@ def create_manifest3(identifier, domain=None, page=None):
originals = []
derivatives = {}
for f in metadata['files']:
if f['source'] == 'derivative':
if f['source'] == 'derivative' and not isinstance(f['original'], list):
if f['original'] in derivatives:
derivatives[f['original']][f['format']] = f
else:
Expand All @@ -375,7 +391,7 @@ def create_manifest3(identifier, domain=None, page=None):
originals.append(f)

# create the canvases for each original
for file in [f for f in originals if f['format'] in ['VBR MP3', 'Flac', 'Ogg Vorbis', 'WAVE', '24bit Flac', 'Shorten']]:
for file in [f for f in originals if f['format'] in ['VBR MP3', '32Kbps MP3', '56Kbps MP3', '64Kbps MP3', '96Kbps MP3', '128Kbps MP3', 'MPEG-4 Audio', 'Flac', 'AIFF', 'Apple Lossless Audio', 'Ogg Vorbis', 'WAVE', '24bit Flac', 'Shorten']]:
normalised_id = file['name'].rsplit(".", 1)[0]
slugged_id = normalised_id.replace(" ", "-")
c_id = f"https://iiif.archivelab.org/iiif/{identifier}/{slugged_id}/canvas"
Expand All @@ -389,7 +405,7 @@ def create_manifest3(identifier, domain=None, page=None):
if file['name'] in derivatives:
body = Choice(items=[])
# add the choices in order per https://github.com/ArchiveLabs/iiif.archivelab.org/issues/77#issuecomment-1499672734
for format in ['VBR MP3', 'Flac', 'Ogg Vorbis', 'WAVE', '24bit Flac', 'Shorten']:
for format in ['VBR MP3', '32Kbps MP3', '56Kbps MP3', '64Kbps MP3', '96Kbps MP3', '128Kbps MP3', 'MPEG-4 Audio', 'Flac', 'AIFF', 'Apple Lossless Audio', 'Ogg Vorbis', 'WAVE', '24bit Flac', 'Shorten']:
if format in derivatives[file['name']]:
r = ResourceItem(id=f"https://archive.org/download/{identifier}/{derivatives[file['name']][format]['name'].replace(' ', '%20')}",
type='Audio',
Expand All @@ -415,24 +431,59 @@ def create_manifest3(identifier, domain=None, page=None):
manifest.add_item(c)

elif mediatype == "movies":
canvas_files = [f for f in metadata['files'] if f['source'].lower() == 'original' and f['format'] == "MPEG4"]
for file in canvas_files:
# sort the files into originals and derivatives, splitting the derivatives into buckets based on the original
originals = []
derivatives = {}
for f in metadata['files']:
if f['source'] == 'derivative':
if f['original'] in derivatives:
derivatives[f['original']][f['format']] = f
else:
derivatives[f['original']] = {f['format']: f}
elif f['source'] == 'original':
originals.append(f)

# create the canvases for each original
for file in [f for f in originals if f['format'] in ['MPEG4', 'h.264 MPEG4', '512Kb MPEG4', 'HiRes MPEG4', 'MPEG2', 'h.264', 'Matroska', 'Ogg Video', 'Ogg Theora', 'WebM', 'Windows Media', 'Cinepack']]:
normalised_id = file['name'].rsplit(".", 1)[0]
slugged_id = normalised_id.replace(" ", "-")
c_id = f"https://iiif.archivelab.org/iiif/{identifier}/{slugged_id}/canvas"
c = Canvas(id=c_id, label=normalised_id, duration=float(file['length']), height=int(file['height']), width=int(file['width']))

# create intermediary objects
ap = AnnotationPage(id=f"https://iiif.archivelab.org/iiif/{identifier}/{slugged_id}/page")
anno = Annotation(id=f"https://iiif.archivelab.org/iiif/{identifier}/{slugged_id}/annotation", motivation="painting", target=c.id)
r = ResourceItem(
id=f"https://archive.org/download/{identifier}/{file['name'].replace(' ', '%20')}",
type='Video',
format=to_mimetype(file['format']),
label={"none": [file['format']]},
duration=float(file['length']),
height=int(file['height']),
width=int(file['width'])
)
anno.body = r

# create body based on whether there are derivatives or not:
if file['name'] in derivatives:
body = Choice(items=[])
# add the choices in order per https://github.com/ArchiveLabs/iiif.archivelab.org/issues/77#issuecomment-1499672734
for format in ['MPEG4', 'h.264 MPEG4', '512Kb MPEG4', 'HiRes MPEG4', 'MPEG2', 'h.264', 'Matroska', 'Ogg Video', 'Ogg Theora', 'WebM', 'Windows Media', 'Cinepack']:
if format in derivatives[file['name']]:
r = ResourceItem(id=f"https://archive.org/download/{identifier}/{derivatives[file['name']][format]['name'].replace(' ', '%20')}",
type='Video',
format=to_mimetype(format),
label={"none": [format]},
duration=float(file['length']),
height=int(file['height']),
width=int(file['width']),
)
body.items.append(r)
elif file['format'] == format:
r = ResourceItem(
id=f"https://archive.org/download/{identifier}/{file['name'].replace(' ', '%20')}",
type='Video',
format=to_mimetype(format),
label={"none": [format]},
duration=float(file['length']),
height=int(file['height']),
width=int(file['width']))
body.items.append(r)
else:
# todo: deal with instances where there are no derivatives for whatever reason
pass

anno.body = body
ap.add_item(anno)
c.add_item(ap)
manifest.add_item(c)
Expand Down
52 changes: 52 additions & 0 deletions tests/test_manifests.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,57 @@ def test_v3_vermont_Life_Magazine(self):

self.assertEqual(len(manifest['items']),116,f"Expected 116 canvas but got: {len(manifest['items'])}")

def test_v3_single_video_manifest(self):
resp = self.test_app.get("/iiif/3/youtube-7w8F2Xi3vFw/manifest.json")
self.assertEqual(resp.status_code, 200)
manifest = resp.json

self.assertEqual(len(manifest['items']),1,f"Expected 1 canvas but got: {len(manifest['items'])}")

#logic to cover etree mediatype github issue #123
def test_v3_etree_mediatype(self):
resp = self.test_app.get("/iiif/3/gd72-04-14.aud.vernon.23662.sbeok.shnf/manifest.json")
self.assertEqual(resp.status_code, 200)
manifest = resp.json

self.assertEqual(len(manifest['items']),36,f"Expected 36 canvases but got: {len(manifest['items'])}")


def test_v3_64Kbps_MP3(self):
resp = self.test_app.get("/iiif/3/TvQuran.com__Alafasi/manifest.json")
self.assertEqual(resp.status_code, 200)
manifest = resp.json
self.assertEqual(len(manifest['items']),114,f"Expected 114 canvases but got: {len(manifest['items'])}")
self.assertEqual("64Kbps MP3".lower() in resp.text.lower(), True, f"Expected the string '64Kbps MP3'")


def test_v3_128Kbps_MP3(self):
resp = self.test_app.get("/iiif/3/alice_in_wonderland_librivox/manifest.json")
self.assertEqual(resp.status_code, 200)
manifest = resp.json
self.assertEqual(len(manifest['items']),12,f"Expected 12 canvases but got: {len(manifest['items'])}")
self.assertEqual("128kbps mp3".lower() in resp.text.lower(), True, f"Expected the string '128kbps mp3'")

def test_v3_h264_MPEG4_OGG_Theora(self):
resp = self.test_app.get("/iiif/3/taboca_201002_03/manifest.json")
self.assertEqual(resp.status_code, 200)
manifest = resp.json
self.assertEqual(len(manifest['items']),251,f"Expected 251 canvases but got: {len(manifest['items'])}")
self.assertEqual("h.264 MPEG4".lower() in resp.text.lower(), True, f"Expected the string 'h.264 MPEG4'")
self.assertEqual("OGG Theora".lower() in resp.text.lower(), True, f"Expected the string 'OGG Theora'")

def test_v3_aiff(self):
resp = self.test_app.get("/iiif/3/PDextend_AIFF/manifest.json")
self.assertEqual(resp.status_code, 200)
manifest = resp.json
self.assertEqual(len(manifest['items']),38,f"Expected 38 canvases but got: {len(manifest['items'])}")
self.assertEqual("AIFF".lower() in resp.text.lower(), True, f"Expected the string 'AIFF'")

''' to test:
kaled_jalil (no derivatives)
Dokku_obrash (geo-restricted?)
m4a filetypes (No length to files?)
'''

if __name__ == '__main__':
unittest.main()

0 comments on commit 367ebe9

Please sign in to comment.