-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
241 additions
and
2 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
|
||
""" | ||
Example of NFT Metadata Upload | ||
""" | ||
from ipyfs import Files | ||
from time import sleep | ||
from tqdm import tqdm | ||
|
||
import json | ||
import os | ||
import re | ||
|
||
|
||
class NFTMetadataUploader: | ||
def __init__( | ||
self, | ||
host: str, | ||
port: int, | ||
metadata_folder: str, | ||
images_folder: str, | ||
ipfs_folder: str, | ||
network_interval_seconds: float): | ||
|
||
self.files = Files(host, port) | ||
self.ipfs_folder = ipfs_folder | ||
self.metadata_folder = metadata_folder | ||
self.images_folder = images_folder | ||
self.ipfs_metadata_folder = os.path.join(ipfs_folder, | ||
metadata_folder) | ||
self.ipfs_images_folder = os.path.join(ipfs_folder, images_folder) | ||
self.network_interval_seconds = network_interval_seconds | ||
|
||
def check_folders(self): | ||
""" | ||
Checks if folders exist and creates them if they don't | ||
""" | ||
try: | ||
self.files.stat(f'/{self.ipfs_folder}') | ||
except Exception as e: | ||
if e.args[0]['Code'] == 0: | ||
print('IPFS({}) Folder does not exist. Creating...'.format( | ||
self.ipfs_folder)) | ||
self.files.mkdir(f'/{self.ipfs_folder}') | ||
|
||
try: | ||
self.files.stat(f'/{self.ipfs_images_folder}') | ||
except Exception as e: | ||
if e.args[0]['Code'] == 0: | ||
print('Image folder does not exist. Creating...') | ||
self.files.mkdir(f'/{self.ipfs_images_folder}') | ||
|
||
try: | ||
self.files.stat(f'/{self.ipfs_metadata_folder}') | ||
except Exception as e: | ||
if e.args[0]['Code'] == 0: | ||
print('Metadata folder does not exist. Creating...') | ||
self.files.mkdir(f'/{self.ipfs_metadata_folder}') | ||
|
||
def get_all_metadata(self) -> list: | ||
""" | ||
Returns all metadata files in the metadata folder | ||
""" | ||
return sorted([x for x in | ||
os.listdir(self.metadata_folder) if ".json" in x]) | ||
|
||
def get_images(self) -> dict: | ||
""" | ||
Returns all images in the images folder | ||
""" | ||
return {x.split('.')[0]: x | ||
for x in os.listdir(self.images_folder) if '.' in x} | ||
|
||
def upload(self, create=True): | ||
""" | ||
Uploads all metadata and images to IPFS | ||
""" | ||
all_metadata = self.get_all_metadata() | ||
images = self.get_images() | ||
for metadata_file in tqdm(all_metadata): | ||
with open(os.path.join( | ||
self.metadata_folder, metadata_file), "r") as f_meta: | ||
# Upload image to IPFS | ||
count_str = re.findall(r'(\d+)', metadata_file)[0] | ||
image_name = images[count_str] | ||
image_path = os.path.join(self.images_folder, image_name) | ||
with open(image_path, "rb") as f_image: | ||
ipfs_f_image_path = os.path.join( | ||
self.ipfs_images_folder, image_name) | ||
self.files.write( | ||
path=f"/{ipfs_f_image_path}", | ||
file=f_image, | ||
create=create | ||
) | ||
sleep(self.network_interval_seconds) | ||
info = self.files.stat(f'/{ipfs_f_image_path}') | ||
|
||
# Upload metadata to IPFS | ||
metadata = json.load(f_meta) | ||
metadata['name'] = metadata['name'].format( | ||
count=int(count_str)) | ||
metadata['image'] = metadata['image'].format( | ||
image_hash=info['result']['Hash']) | ||
ipfs_f_meta_path = os.path.join( | ||
self.ipfs_metadata_folder, metadata_file) | ||
self.files.write( | ||
path=f"/{ipfs_f_meta_path}", | ||
file=json.dumps(metadata), | ||
create=True | ||
) | ||
sleep(self.network_interval_seconds) | ||
|
||
|
||
if __name__ == "__main__": | ||
nftmu = NFTMetadataUploader( | ||
host="http://localhost", | ||
port=5001, | ||
metadata_folder="metadata", | ||
images_folder="images", | ||
ipfs_folder="test01", | ||
network_interval_seconds=0.03) | ||
|
||
nftmu.check_folders() | ||
nftmu.upload() | ||
print("Done") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"name": "Sample NFT #{count}", | ||
"description": "Sample NFT Description", | ||
"image": "ipfs://{image_hash}", | ||
"attributes": [ | ||
{ | ||
"trait_type": "Base", | ||
"value": "Starfish" | ||
}, | ||
{ | ||
"trait_type": "Eyes", | ||
"value": "Big" | ||
}, | ||
{ | ||
"trait_type": "Mouth", | ||
"value": "Surprised" | ||
}, | ||
{ | ||
"trait_type": "Level", | ||
"value": 5 | ||
}, | ||
{ | ||
"trait_type": "Stamina", | ||
"value": 1.4 | ||
}, | ||
{ | ||
"trait_type": "Personality", | ||
"value": "Sad" | ||
}, | ||
{ | ||
"display_type": "boost_number", | ||
"trait_type": "Aqua Power", | ||
"value": 40 | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"name": "Sample NFT #{count}", | ||
"description": "Sample NFT Description", | ||
"image": "ipfs://{image_hash}", | ||
"attributes": [ | ||
{ | ||
"trait_type": "Base", | ||
"value": "Starfish" | ||
}, | ||
{ | ||
"trait_type": "Eyes", | ||
"value": "Big" | ||
}, | ||
{ | ||
"trait_type": "Mouth", | ||
"value": "Surprised" | ||
}, | ||
{ | ||
"trait_type": "Level", | ||
"value": 5 | ||
}, | ||
{ | ||
"trait_type": "Stamina", | ||
"value": 1.4 | ||
}, | ||
{ | ||
"trait_type": "Personality", | ||
"value": "Sad" | ||
}, | ||
{ | ||
"display_type": "boost_number", | ||
"trait_type": "Aqua Power", | ||
"value": 40 | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"name": "Sample NFT #{count}", | ||
"description": "Sample NFT Description", | ||
"image": "ipfs://{image_hash}", | ||
"attributes": [ | ||
{ | ||
"trait_type": "Base", | ||
"value": "Starfish" | ||
}, | ||
{ | ||
"trait_type": "Eyes", | ||
"value": "Big" | ||
}, | ||
{ | ||
"trait_type": "Mouth", | ||
"value": "Surprised" | ||
}, | ||
{ | ||
"trait_type": "Level", | ||
"value": 5 | ||
}, | ||
{ | ||
"trait_type": "Stamina", | ||
"value": 1.4 | ||
}, | ||
{ | ||
"trait_type": "Personality", | ||
"value": "Sad" | ||
}, | ||
{ | ||
"display_type": "boost_number", | ||
"trait_type": "Aqua Power", | ||
"value": 40 | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
certifi==2022.6.15 | ||
charset-normalizer==2.1.0 | ||
idna==3.3 | ||
ipyfs==0.1.2 | ||
requests==2.28.1 | ||
tqdm==4.64.0 | ||
urllib3==1.26.10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,4 @@ | |
|
||
|
||
__AUTHOR__ = "837477" | ||
__VERSION__ = "0.1.2" | ||
__VERSION__ = "0.1.3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ certifi==2022.6.15 | |
charset-normalizer==2.1.0 | ||
idna==3.3 | ||
requests==2.28.1 | ||
urllib3==1.26.10 | ||
urllib3==1.26.10 |