Skip to content

Commit

Permalink
[deploy] 0.1.3 (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
837477 authored Aug 4, 2022
2 parents cca1b27 + a8e23dc commit bbe57c7
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 2 deletions.
Binary file added examples/nft_image_uploader/images/0001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/nft_image_uploader/images/0002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/nft_image_uploader/images/0003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
124 changes: 124 additions & 0 deletions examples/nft_image_uploader/main.py
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")
36 changes: 36 additions & 0 deletions examples/nft_image_uploader/metadata/0001.json
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
}
]
}
36 changes: 36 additions & 0 deletions examples/nft_image_uploader/metadata/0002.json
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
}
]
}
36 changes: 36 additions & 0 deletions examples/nft_image_uploader/metadata/0003.json
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
}
]
}
7 changes: 7 additions & 0 deletions examples/nft_image_uploader/requirements.txt
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
2 changes: 1 addition & 1 deletion ipyfs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@


__AUTHOR__ = "837477"
__VERSION__ = "0.1.2"
__VERSION__ = "0.1.3"
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bbe57c7

Please sign in to comment.