Skip to content

Commit 85aa49f

Browse files
authored
Merge pull request #102 from ourzora/BACK-2798
BACK-2798: fix nouns parser
2 parents e8bc81c + 28b7485 commit 85aa49f

File tree

6 files changed

+62
-30
lines changed

6 files changed

+62
-30
lines changed

docs/changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v0.3.4
4+
5+
- Fix Nouns parser to make sure image uri is properly base64-encoded svg
6+
37
## v0.3.3
48

59
- Fix an issue in `OpenseaParser` where the plain-text svg wouldn't be recognized as valid image uri

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Getting Started
22

3-
Documentation for version: **v0.3.3**
3+
Documentation for version: **v0.3.4**
44

55
## Overview
66

offchain/metadata/parsers/collection/nouns.py

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import asyncio
22
from dataclasses import dataclass
33
from typing import Optional
4-
from urllib.parse import quote
54

65
from offchain.constants.addresses import CollectionAddress
76
from offchain.metadata.constants.nouns import ACCESSORY, BACKGROUND, BODY, GLASSES, HEAD
@@ -28,11 +27,32 @@ def from_raw( # type: ignore[no-untyped-def]
2827
head_index: int,
2928
glasses_index: int,
3029
):
31-
background = BACKGROUND[background_index]
32-
body = BODY[body_index]
33-
accessory = ACCESSORY[accessory_index]
34-
head = HEAD[head_index]
35-
glasses = GLASSES[glasses_index]
30+
# indexes here come from contract calls and can be out of bounds
31+
background = (
32+
BACKGROUND[background_index]
33+
if 0 <= background_index < len(BACKGROUND)
34+
else f"unknown({background_index})"
35+
)
36+
body = (
37+
BODY[body_index]
38+
if 0 <= body_index < len(BODY)
39+
else f"unknown({body_index})"
40+
)
41+
accessory = (
42+
ACCESSORY[accessory_index]
43+
if 0 <= accessory_index < len(ACCESSORY)
44+
else f"unknown({accessory_index})"
45+
)
46+
head = (
47+
HEAD[head_index]
48+
if 0 <= head_index < len(HEAD)
49+
else f"unknown({head_index})"
50+
)
51+
glasses = (
52+
GLASSES[glasses_index]
53+
if 0 <= glasses_index < len(GLASSES)
54+
else f"unknown({glasses_index})"
55+
)
3656

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

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

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

5172
return MediaDetails(
52-
uri=image_uri, size=None, sha256=None, mime_type="image/svg+xml"
73+
uri=image_uri, size=size, sha256=None, mime_type="image/svg+xml"
5374
) # noqa: E501
5475

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

5981
return MediaDetails(
60-
uri=image_uri, size=None, sha256=None, mime_type="image/svg+xml"
82+
uri=image_uri, size=size, sha256=None, mime_type="image/svg+xml"
6183
) # noqa: E501
6284

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

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

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

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

183207
async def _gen_parse_metadata_impl(self, token: Token, raw_data: dict, *args, **kwargs) -> Metadata: # type: ignore[no-untyped-def, type-arg] # noqa: E501
184-
token.uri = await self.gen_uri(token)
208+
if token.uri is None:
209+
token.uri = await self.gen_uri(token)
210+
211+
if not isinstance(raw_data, dict):
212+
raw_data = await self.fetcher.gen_fetch_content(token.uri)
185213

186-
raw_data, mime_type_and_size, attributes = await asyncio.gather(
187-
self.fetcher.gen_fetch_content(token.uri), # type: ignore[arg-type, assignment] # noqa: E501
214+
mime_type_and_size, attributes, image = await asyncio.gather(
188215
self.fetcher.gen_fetch_mime_type_and_size(token.uri), # type: ignore[arg-type] # noqa: E501
189216
self.gen_seed_attributes(token),
217+
self.gen_image(raw_data),
190218
)
191-
image = await self.gen_image(raw_data)
219+
192220
mime_type, _ = mime_type_and_size
193221

194222
return Metadata(

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "offchain"
3-
version = "0.3.3"
3+
version = "0.3.4"
44
description = "Open source metadata processing framework"
55
authors = ["Zora eng <[email protected]>"]
66
readme = "README.md"

0 commit comments

Comments
 (0)