11import asyncio
22from dataclasses import dataclass
33from typing import Optional
4- from urllib .parse import quote
54
65from offchain .constants .addresses import CollectionAddress
76from 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 (
0 commit comments