Skip to content

Commit

Permalink
BACK-2798: fix nouns parser
Browse files Browse the repository at this point in the history
  • Loading branch information
zylora committed Apr 27, 2024
1 parent e8bc81c commit 28b7485
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 30 deletions.
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.3.4

- Fix Nouns parser to make sure image uri is properly base64-encoded svg

## v0.3.3

- Fix an issue in `OpenseaParser` where the plain-text svg wouldn't be recognized as valid image uri
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Getting Started

Documentation for version: **v0.3.3**
Documentation for version: **v0.3.4**

## Overview

Expand Down
60 changes: 44 additions & 16 deletions offchain/metadata/parsers/collection/nouns.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import asyncio
from dataclasses import dataclass
from typing import Optional
from urllib.parse import quote

from offchain.constants.addresses import CollectionAddress
from offchain.metadata.constants.nouns import ACCESSORY, BACKGROUND, BODY, GLASSES, HEAD
Expand All @@ -28,11 +27,32 @@ def from_raw( # type: ignore[no-untyped-def]
head_index: int,
glasses_index: int,
):
background = BACKGROUND[background_index]
body = BODY[body_index]
accessory = ACCESSORY[accessory_index]
head = HEAD[head_index]
glasses = GLASSES[glasses_index]
# indexes here come from contract calls and can be out of bounds
background = (
BACKGROUND[background_index]
if 0 <= background_index < len(BACKGROUND)
else f"unknown({background_index})"
)
body = (
BODY[body_index]
if 0 <= body_index < len(BODY)
else f"unknown({body_index})"
)
accessory = (
ACCESSORY[accessory_index]
if 0 <= accessory_index < len(ACCESSORY)
else f"unknown({accessory_index})"
)
head = (
HEAD[head_index]
if 0 <= head_index < len(HEAD)
else f"unknown({head_index})"
)
glasses = (
GLASSES[glasses_index]
if 0 <= glasses_index < len(GLASSES)
else f"unknown({glasses_index})"
)

return Seeds(background, body, accessory, head, glasses)

Expand All @@ -46,18 +66,20 @@ class NounsParser(CollectionParser):

def get_image(self, raw_data: dict) -> Optional[MediaDetails]: # type: ignore[type-arg] # noqa: E501
raw_image_uri = raw_data.get("image")
image_uri = quote(self.fetcher.fetch_content(raw_image_uri)) # type: ignore[arg-type] # noqa: E501
mime_type, size = self.fetcher.fetch_mime_type_and_size(raw_image_uri) # type: ignore[arg-type]
image_uri = raw_image_uri

return MediaDetails(
uri=image_uri, size=None, sha256=None, mime_type="image/svg+xml"
uri=image_uri, size=size, sha256=None, mime_type="image/svg+xml"
) # noqa: E501

async def gen_image(self, raw_data: dict) -> Optional[MediaDetails]: # type: ignore[type-arg] # noqa: E501
raw_image_uri = raw_data.get("image")
image_uri = quote(await self.fetcher.gen_fetch_content(raw_image_uri)) # type: ignore[arg-type] # noqa: E501
mime_type, size = await self.fetcher.gen_fetch_mime_type_and_size(raw_image_uri) # type: ignore[arg-type]
image_uri = raw_image_uri

return MediaDetails(
uri=image_uri, size=None, sha256=None, mime_type="image/svg+xml"
uri=image_uri, size=size, sha256=None, mime_type="image/svg+xml"
) # noqa: E501

def seeds(self, token: Token) -> Optional[Seeds]:
Expand Down Expand Up @@ -165,9 +187,11 @@ def normalize_value(value: str) -> str:
]

def parse_metadata(self, token: Token, raw_data: dict, *args, **kwargs) -> Metadata: # type: ignore[no-untyped-def, type-arg] # noqa: E501
token.uri = self.get_uri(token)
if token.uri is None:
token.uri = self.get_uri(token)

raw_data = self.fetcher.fetch_content(token.uri) # type: ignore[arg-type, assignment] # noqa: E501
if not isinstance(raw_data, dict):
raw_data = self.fetcher.fetch_content(token.uri) # type: ignore[arg-type, assignment] # noqa: E501
mime_type, _ = self.fetcher.fetch_mime_type_and_size(token.uri) # type: ignore[arg-type] # noqa: E501

return Metadata(
Expand All @@ -181,14 +205,18 @@ def parse_metadata(self, token: Token, raw_data: dict, *args, **kwargs) -> Metad
)

async def _gen_parse_metadata_impl(self, token: Token, raw_data: dict, *args, **kwargs) -> Metadata: # type: ignore[no-untyped-def, type-arg] # noqa: E501
token.uri = await self.gen_uri(token)
if token.uri is None:
token.uri = await self.gen_uri(token)

if not isinstance(raw_data, dict):
raw_data = await self.fetcher.gen_fetch_content(token.uri)

raw_data, mime_type_and_size, attributes = await asyncio.gather(
self.fetcher.gen_fetch_content(token.uri), # type: ignore[arg-type, assignment] # noqa: E501
mime_type_and_size, attributes, image = await asyncio.gather(
self.fetcher.gen_fetch_mime_type_and_size(token.uri), # type: ignore[arg-type] # noqa: E501
self.gen_seed_attributes(token),
self.gen_image(raw_data),
)
image = await self.gen_image(raw_data)

mime_type, _ = mime_type_and_size

return Metadata(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "offchain"
version = "0.3.3"
version = "0.3.4"
description = "Open source metadata processing framework"
authors = ["Zora eng <[email protected]>"]
readme = "README.md"
Expand Down
Loading

0 comments on commit 28b7485

Please sign in to comment.