diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fe2635..a93be6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,11 +7,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] ### Added - `get_cmap` function +- `Gateway` enum ### Changed - minor edits in `functions.py` - `DEFAULT_CMAP` renamed to `DEFAULT_CMAP_NAME` - `pillow` added to conda dependencies - `codecov` removed from `dev-requirements.txt` +- `gateway` parameter added to `nft_storage` method - Test system modified - `README.md` updated ## [1.1] - 2023-04-05 diff --git a/README.md b/README.md index 61aca99..dcf0ee3 100644 --- a/README.md +++ b/README.md @@ -261,7 +261,15 @@ or {'status': {'image': True, 'data':True}, 'message': {'image':'IMAGE_FILE_LINK', 'data':'DATA_FILE_LINK'} ``` +You have the option to choose a specific IPFS gateway: +```pycon +>>> from samila import Gateway +>>> g.nft_storage(api_key="API_KEY", upload_data=True, gateway=Gateway.DWEB) +{'status': {'image': True, 'data':True}, 'message': {'image':'IMAGE_FILE_LINK', 'data':'DATA_FILE_LINK'} +``` + * Default timeout is **3000** seconds +* Default gateway is **IPFS_IO** ### Save image Save generated image diff --git a/samila/__init__.py b/samila/__init__.py index 894ebbd..e37584c 100644 --- a/samila/__init__.py +++ b/samila/__init__.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- """Samila modules.""" from .genimage import GenerativeImage -from .params import Projection, Marker, VALID_COLORS, SAMILA_VERSION +from .params import Projection, Marker, Gateway, VALID_COLORS, SAMILA_VERSION from .errors import samilaDataError, samilaGenerateError __version__ = SAMILA_VERSION diff --git a/samila/functions.py b/samila/functions.py index aae6409..c73f192 100644 --- a/samila/functions.py +++ b/samila/functions.py @@ -14,7 +14,7 @@ from .params import SAMILA_VERSION from .params import DEFAULT_MARKER, DEFAULT_START, DEFAULT_STOP, DEFAULT_STEP, DEFAULT_COLOR, DEFAULT_IMAGE_SIZE, DEFAULT_DEPTH from .params import DEFAULT_CMAP_NAME, DEFAULT_CMAP_RANGE, DEFAULT_ROTATION from .params import DEFAULT_BACKGROUND_COLOR, DEFAULT_SPOT_SIZE, DEFAULT_PROJECTION, DEFAULT_ALPHA, DEFAULT_LINEWIDTH -from .params import Projection, Marker, VALID_COLORS, HEX_COLOR_PATTERN, NFT_STORAGE_API, NFT_STORAGE_LINK, OVERVIEW +from .params import Projection, Marker, VALID_COLORS, HEX_COLOR_PATTERN, NFT_STORAGE_API, OVERVIEW from .params import DATA_TYPE_ERROR, DATA_FORMAT_ERROR, CONFIG_TYPE_ERROR, CONFIG_FORMAT_ERROR, PLOT_DATA_ERROR, CONFIG_NO_STR_FUNCTION_ERROR from .params import NO_FIG_ERROR_MESSAGE, FIG_SAVE_SUCCESS_MESSAGE, NFT_STORAGE_SUCCESS_MESSAGE, SAVE_NO_DATA_ERROR from .params import INVALID_COLOR_TYPE_ERROR, COLOR_SIZE_ERROR @@ -515,7 +515,7 @@ def _GI_initializer(g, function1, function2): g.missed_points_number = 0 -def nft_storage_upload(api_key, data, timeout): +def nft_storage_upload(api_key, data, timeout, gateway): """ Upload file to nft.storage. @@ -525,6 +525,8 @@ def nft_storage_upload(api_key, data, timeout): :type data: binary :param timeout: upload timeout (in seconds) :type timeout: int + :param gateway: IPFS gateway + :type gateway: Gateway enum :return: result as dict """ result = {"status": True, "message": NFT_STORAGE_SUCCESS_MESSAGE} @@ -537,7 +539,7 @@ def nft_storage_upload(api_key, data, timeout): timeout=timeout) response_json = response.json() if response_json["ok"]: - result["message"] = NFT_STORAGE_LINK.format( + result["message"] = gateway.value.format( response_json['value']['cid']) return result result["status"] = False diff --git a/samila/genimage.py b/samila/genimage.py index 170dfbc..14c884f 100644 --- a/samila/genimage.py +++ b/samila/genimage.py @@ -175,7 +175,8 @@ class GenerativeImage: upload_data=False, upload_config=False, depth=None, - timeout=3000): + timeout=3000, + gateway=Gateway.IPFS_IO): """ Upload image to nft.storage. @@ -189,6 +190,8 @@ class GenerativeImage: :type depth: float :param timeout: upload timeout (in seconds) :type timeout: int + :param gateway: IPFS gateway + :type gateway: Gateway enum :return: result as dict """ save_params_filter(self, depth) @@ -199,7 +202,8 @@ class GenerativeImage: response = nft_storage_upload( api_key=api_key, data=buf.getvalue(), - timeout=timeout) + timeout=timeout, + gateway=gateway) if upload_config == False and upload_data == False: return response result = {key: {'image': value} for key, value in response.items()} @@ -207,14 +211,16 @@ class GenerativeImage: response = nft_storage_upload( api_key=api_key, data=json.dumps(get_config(self)), - timeout=timeout) + timeout=timeout, + gateway=gateway) for key, value in response.items(): result[key]['config'] = value if upload_data: response = nft_storage_upload( api_key=api_key, data=json.dumps(get_data(self)), - timeout=timeout) + timeout=timeout, + gateway=gateway) for key, value in response.items(): result[key]['data'] = value return result diff --git a/samila/params.py b/samila/params.py index 2dda273..fcc84c0 100644 --- a/samila/params.py +++ b/samila/params.py @@ -36,7 +36,6 @@ VALID_COLORS = list( **matplotlib.colors.CSS4_COLORS).keys()) HEX_COLOR_PATTERN = r'^#(?:[0-9a-fA-F]{3}){1,2}$' NFT_STORAGE_API = "https://api.nft.storage/upload" -NFT_STORAGE_LINK = "https://ipfs.io/ipfs/{}" NFT_STORAGE_SUCCESS_MESSAGE = "Everything seems good." FIG_SAVE_SUCCESS_MESSAGE = "Everything seems good." DATA_SAVE_SUCCESS_MESSAGE = "Everything seems good." @@ -56,6 +55,36 @@ BOTH_COLOR_COMPLEMENT_WARNING = "It is not possible to set color and bgcolor to COLOR_NOT_FOUND_WARNING = "color '{0}' not found. Replacing it with '{1}'" +class Gateway(Enum): + """ + IPFS gateway class. + + >>> import samila + >>> gateway = samila.Gateway.IPFS_IO + """ + + CID = "{0}" + IPFS_IO = "https://ipfs.io/ipfs/{0}" + GATEWAY_IPFS_IO = "https://gateway.ipfs.io/ipfs/{0}" + VIA0 = "https://via0.com/ipfs/{0}" + CLOUDFLARE_IPFS = "https://cloudflare-ipfs.com/ipfs/{0}" + HARDBIN = "https://hardbin.com/ipfs/{0}" + PINATA = "https://gateway.pinata.cloud/ipfs/{0}" + JORROPO = "https://jorropo.net/ipfs/{0}" + DWEB = "https://dweb.link/ipfs/{0}" + BEST_PRACTICE = "https://ipfs.best-practice.se/ipfs/{0}" + RUNFISSION = "https://ipfs.runfission.com/ipfs/{0}" + ARAGON_NETWORK = "https://ipfs.eth.aragon.network/ipfs/{0}" + TEXTILE = "https://hub.textile.io/ipfs/{0}" + EVERLAND = "https://{0}.ipfs.4everland.io" + CF_IPFS = "https://{0}.ipfs.cf-ipfs.com" + LITNET = "https://ipfs.litnet.work/ipfs/{0}" + JPU = "https://ipfs.jpu.jp/ipfs/{0}" + CZIP = "https://ipfs.czip.it/ipfs/{0}" + W3S = "https://{0}.ipfs.w3s.link" + GW3 = "https://{0}.ipfs.gw3.io" + + class Projection(Enum): """ Samila Projection type class. diff --git a/test/nft_upload_test.py b/test/nft_upload_test.py index f3ce1e3..e6b7595 100644 --- a/test/nft_upload_test.py +++ b/test/nft_upload_test.py @@ -2,7 +2,7 @@ """ >>> import os >>> import time ->>> from samila import GenerativeImage, Projection +>>> from samila import GenerativeImage, Projection, Gateway >>> from samila.params import VALID_COLORS >>> g = GenerativeImage() >>> g.generate() @@ -42,4 +42,17 @@ True True >>> result['status']["data"] True +>>> status = False +>>> counter = 0 +>>> while(status == False and counter>> result['status']["image"] +True +>>> result['status']["data"] +True +>>> result['status']["config"] +True """