Skip to content

Commit

Permalink
Add extract_meta & create_thumb parameters and fix redundant meta…
Browse files Browse the repository at this point in the history
…data extraction
  • Loading branch information
titusz committed Jan 24, 2024
1 parent 72837e3 commit 08345e0
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 21 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Changelog

## 0.6.1 - Unreleased
- Add `extract_meta` & `create_thumb` parameters
- Fix redundant metadata extraction
- Updated dependencies

## 0.6.0 - 2024-01-22
Expand Down
74 changes: 56 additions & 18 deletions iscc_sdk/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def code_iscc(fp):
with ThreadPoolExecutor() as executor:
instance = executor.submit(code_instance, fp)
data = executor.submit(code_data, fp)
content = executor.submit(code_content, fp)
content = executor.submit(code_content, fp, False, None) # Skip metadata extraction
meta = executor.submit(code_meta, fp)

# Wait for all futures to complete and retrieve their results
Expand Down Expand Up @@ -85,12 +85,14 @@ def code_meta(fp):
return idk.IsccMeta.construct(**meta)


def code_content(fp):
# type: (str) -> idk.IsccMeta
def code_content(fp, extract_meta=None, create_thumb=None):
# type: (str, bool|None, bool|None) -> idk.IsccMeta
"""
Detect mediatype and create corresponding Content-Code.
:param str fp: Filepath
:param bool|None extract_meta: Whether to extract metadata.
:param bool|None create_thumb: Whether to create a thumbnail.
:return: Content-Code wrapped in ISCC metadata.
:rtype: IsccMeta
"""
Expand All @@ -105,13 +107,13 @@ def code_content(fp):
mediatype, mode = idk.mediatype_and_mode(fp)

if mode == "image":
cc = code_image(fp)
cc = code_image(fp, extract_meta, create_thumb)
elif mode == "audio":
cc = code_audio(fp)
cc = code_audio(fp, extract_meta, create_thumb)
elif mode == "video":
cc = code_video(fp)
cc = code_video(fp, extract_meta, create_thumb)
elif mode == "text":
cc = code_text(fp)
cc = code_text(fp, extract_meta, create_thumb)
else: # pragma nocover
raise idk.IsccUnsupportedMediatype(mediatype)

Expand All @@ -122,19 +124,28 @@ def code_content(fp):
return cc


def code_text(fp):
# type: (str) -> idk.IsccMeta
def code_text(fp, extract_meta=None, create_thumb=None):
# type: (str, bool|None, bool|None) -> idk.IsccMeta
"""
Generate Content-Code Text.
:param str fp: Filepath used for Text-Code creation.
:param bool|None extract_meta: Whether to extract metadata.
:param bool|None create_thumb: Whether to create a thumbnail.
:return: ISCC metadata including Text-Code.
:rtype: IsccMeta
"""
meta = dict()

if idk.sdk_opts.extract_metadata:
if extract_meta is None:
extract_meta = idk.sdk_opts.extract_metadata
if create_thumb is None:
create_thumb = idk.sdk_opts.create_thumbnail

if extract_meta:
meta = idk.text_meta_extract(fp)

if create_thumb:
thumbnail_img = idk.text_thumbnail(fp)
if thumbnail_img:
thumbnail_durl = idk.image_to_data_url(thumbnail_img)
Expand All @@ -149,18 +160,27 @@ def code_text(fp):
return idk.IsccMeta.construct(**meta)


def code_image(fp):
# type: (str) -> idk.IsccMeta
def code_image(fp, extract_meta=None, create_thumb=None):
# type: (str, bool|None, bool|None) -> idk.IsccMeta
"""
Generate Content-Code Image.
:param str fp: Filepath used for Image-Code creation.
:param bool|None extract_meta: Whether to extract metadata.
:param bool|None create_thumb: Whether to create a thumbnail.
:return: ISCC metadata including Image-Code.
:rtype: IsccMeta
"""
meta = dict()
if idk.sdk_opts.extract_metadata:

if extract_meta is None:
extract_meta = idk.sdk_opts.extract_metadata
if create_thumb is None:
create_thumb = idk.sdk_opts.create_thumbnail

if extract_meta:
meta = idk.image_meta_extract(fp)
if create_thumb:
thumbnail_img = idk.image_thumbnail(fp)
thumbnail_durl = idk.image_to_data_url(thumbnail_img)
meta["thumbnail"] = thumbnail_durl
Expand All @@ -172,42 +192,60 @@ def code_image(fp):
return idk.IsccMeta.construct(**meta)


def code_audio(fp):
# type: (str) -> idk.IsccMeta
def code_audio(fp, extract_meta=None, create_thumb=None):
# type: (str, bool|None, bool|None) -> idk.IsccMeta
"""
Generate Content-Code Audio.
:param str fp: Filepath used for Audio-Code creation.
:param bool|None extract_meta: Whether to extract metadata.
:param bool|None create_thumb: Whether to create a thumbnail.
:return: ISCC metadata including Audio-Code.
:rtype: IsccMeta
"""
meta = dict()
if idk.sdk_opts.extract_metadata:

if extract_meta is None:
extract_meta = idk.sdk_opts.extract_metadata
if create_thumb is None:
create_thumb = idk.sdk_opts.create_thumbnail

if extract_meta:
meta = idk.audio_meta_extract(fp)
if create_thumb:
thumbnail_img = idk.audio_thumbnail(fp)
if thumbnail_img:
thumbnail_durl = idk.image_to_data_url(thumbnail_img)
meta["thumbnail"] = thumbnail_durl

features = idk.audio_features_extract(fp)
code_obj = ic.gen_audio_code_v0(features["fingerprint"], bits=idk.core_opts.audio_bits)
meta.update(code_obj)

return idk.IsccMeta.construct(**meta)


def code_video(fp):
def code_video(fp, extract_meta=None, create_thumb=None):
# type: (str) -> idk.IsccMeta
"""
Generate Content-Code Video.
:param str fp: Filepath used for Video-Code creation.
:param bool|None extract_meta: Whether to extract metadata.
:param bool|None create_thumb: Whether to create a thumbnail.
:return: ISCC metadata including Image-Code.
:rtype: IsccMeta
"""
meta = dict()

if idk.sdk_opts.extract_metadata:
if extract_meta is None:
extract_meta = idk.sdk_opts.extract_metadata
if create_thumb is None:
create_thumb = idk.sdk_opts.create_thumbnail

if extract_meta:
meta = idk.video_meta_extract(fp)
if create_thumb:
thumbnail_image = idk.video_thumbnail(fp)
if thumbnail_image is not None:
thumbnail_durl = idk.image_to_data_url(thumbnail_image)
Expand Down
5 changes: 5 additions & 0 deletions iscc_sdk/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class Config:
description="ISCC_EXTRACT_METADATA - Extract metadata from digital assets (defaut: True)",
)

create_thumbnail: bool = Field(
True,
description="ISCC_CREATE_THUMBNAIL - Create thumbail for digital assets (defaut: True)",
)

image_exif_transpose: bool = Field(
True,
description="ISCC_SDK_IMAGE_EXIF_TRANSPOSE - Transpose image according to EXIF Orientation tag",
Expand Down
4 changes: 3 additions & 1 deletion tests/test_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,10 @@ def test_audio_embed_title(mp3_cover):
assert meta.dict() == {"creator": "Test Artist", "duration": 15, "name": "Embedded Title"}


def test_code_audio_metadata_extraction_disabled(mp3_cover):
def test_code_audio_metadata_meta_thumb_disabled(mp3_cover):
idk.sdk_opts.extract_metadata = False
idk.sdk_opts.create_thumbnail = False
meta = idk.code_audio(mp3_cover)
assert meta.dict() == {"iscc": "ISCC:EIAWUJFCEZZOJYVD"}
idk.sdk_opts.extract_metadata = True
idk.sdk_opts.create_thumbnail = True
4 changes: 3 additions & 1 deletion tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,10 @@ def test_embed_metadata_non_uri(jpg_file):
}


def test_code_image_no_metadata(jpg_file):
def test_code_image_nometa_nothumb(jpg_file):
idk.sdk_opts.extract_metadata = False
idk.sdk_opts.create_thumbnail = False
meta = idk.code_image(jpg_file)
assert meta.dict() == {"iscc": "ISCC:EEA4GQZQTY6J5DTH"}
idk.sdk_opts.extract_metadata = True
idk.sdk_opts.create_thumbnail = True
4 changes: 3 additions & 1 deletion tests/test_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ def test_video_features_extract_store(mp4_file):
idk.sdk_opts.video_store_mp7sig = True


def test_code_video_no_meta_extract(mp4_file):
def test_code_video_nometa_nothumb(mp4_file):
idk.sdk_opts.extract_metadata = False
idk.sdk_opts.create_thumbnail = False
meta = idk.code_video(mp4_file)
assert meta.dict() == {"iscc": "ISCC:EMAV4DUD6QORW4X4"}
idk.sdk_opts.extract_metadata = True
idk.sdk_opts.create_thumbnail = True

0 comments on commit 08345e0

Please sign in to comment.