class TestPinataPy(unittest.TestCase): def setUp(self): self.PINATA_API_KEY = os.environ.get("PINATA_API_KEY") self.PINATA_SECRET_API_KEY = os.environ.get("PINATA_SECRET_API_KEY") self.pinata = PinataPy(self.PINATA_API_KEY, self.PINATA_SECRET_API_KEY) def test_test_authentication(self): response = self.pinata.test_authentication() expected = { "message": "Congratulations! You are communicating with the Pinata API!" } self.assertEqual(response, expected) def test_remove_pin_from_ipfs(self): pass def test_pin_list(self): options = {"status": "pinned"} res = self.pinata.pin_list(options) self.assertIn("rows", res) def test_user_pinned_data_total(self): res = self.pinata.user_pinned_data_total() self.assertIn("pin_count", res)
def _pin_to_pinata(file_path: str, config: dict) -> None: pinata_api = config["datalog"]["pinata_api"] pinata_secret = config["datalog"]["pinata_secret"] if pinata_secret: try: logger.info("DatalogFeeder: Pinning file to Pinata") pinata = PinataPy(pinata_api, pinata_secret) pinata.pin_file_to_ipfs(file_path) hash = pinata.pin_list()["rows"][0]["ipfs_pin_hash"] logger.info(f"DatalogFeeder: File sent to pinata. Hash is {hash}") except Exception as e: logger.warning(f"DatalogFeeder: Failed while pining file to Pinata. Error: {e}")
def _pin_to_pinata(file_path: str, config): pinata_api = config["datalog"]["pinata_api"] pinata_secret = config["datalog"]["pinata_secret"] if pinata_secret: try: rospy.loginfo("Pinning file to Pinata") pinata = PinataPy(pinata_api, pinata_secret) pinata.pin_file_to_ipfs(file_path) hash = pinata.pin_list()["rows"][0]["ipfs_pin_hash"] rospy.loginfo(f"File sent to pinata. Hash is {hash}") except Exception as e: rospy.loginfo(f"Failed while pining file to Pinata. Error: {e}")
def pin_to_pinata(filename: str, config: tp.Dict[str, tp.Dict[str, tp.Any]]) -> None: """ :param filename: full name of a recorded video :type filename: str :param config: dictionary containing all the configurations :type config: dict pinning files in pinata to make them broadcasted around ipfs """ pinata_api = config["pinata"]["pinata_api"] # pinata credentials pinata_secret_api = config["pinata"]["pinata_secret_api"] if pinata_api and pinata_secret_api: pinata = PinataPy(pinata_api, pinata_secret_api) pinata.pin_file_to_ipfs(filename) # here we actually send the entire file to pinata, not just its hash. It will # remain the same as if published locally, cause the content is the same. logging.info("File published to Pinata")
def __init__(self): types = { 'CameraInfo': CameraInfo, 'Image': Image, 'TFMessage': TFMessage, 'InteractiveMarkerUpdate': InteractiveMarkerUpdate } rospy.init_node(f"rosbag_recorder", anonymous=False) config_path = rospy.get_param("~config") print(config_path) sdk = bosdyn.client.create_standard_sdk('understanding-spot') self.robot = sdk.create_robot('192.168.50.3') with open("/home/spot/config/config") as f: for line in f: line = line.split('/') user = line[0].strip() password = line[1].strip() pinata_pub = line[2].strip() pinata_secret = line[3].strip() self.pinata = PinataPy(pinata_pub, pinata_secret) self.robot.authenticate(user, password) self.state_client = self.robot.ensure_client('robot-state') inf = rosnode.get_node_info_description('/rviz') inf = inf.split() # self.topics_str = '' # types_name = [] # topic = False # for p in inf: # p = p.strip() # if topic: # if p[0] == '/': # self.command_full.append(p) # self.topics_str += f'{p} ' # # if p[0] == '[': # # types_name.append(p.split('/')[-1][:-1]) # if p == "Subscriptions:": # topic = True # if p == "Services:": # topic = False # print(self.command_full) # print(types) self.recording = False i = 0 self.bag = None
def upload(asset_folder): """ Uploads media and metadata from a specific folder to IPFS and blockchain respectively. """ click.echo(f"Starting upload...") click.echo(f"Checking contracts...") if not walletDeployed(): return if not distributorDeployed(): return numAssets = getNumberOfAssets(asset_folder) config = getDistributorConfig() pinata = PinataPy(pinata_api_key=config["pinata_api_key"], pinata_secret_api_key=config["pinata_secret_api_key"]) # Upload files to IPFS first click.echo(f"Checking IPFS uploads...") for i in range(0, numAssets): with open(os.path.join(asset_folder, str(i) + ".json")) as f: data = f.read() meta = json.loads(data) if meta["image"] == "": result = pinata.pin_file_to_ipfs( os.path.join(asset_folder, str(i) + ".png")) if "IpfsHash" not in result: click.echo( f"IPFS upload error! Please check config and run CLI again." ) click.echo(f"Error message: {result}") quit() meta["image"] = "https://gateway.pinata.cloud/ipfs/" + result[ "IpfsHash"] meta["properties"]["files"][0][ "uri"] = "https://gateway.pinata.cloud/ipfs/" + result[ "IpfsHash"] meta["properties"]["files"][0]["type"] = "image/png" meta["properties"]["category"] = "image" jsonFile = os.path.join(asset_folder, str(i) + ".json") with open(jsonFile, "w") as fp: json.dump(meta, fp) # Upload metadatas to blockchain next, start where we left off (check current Distributor contents) msig = getWallet() distributor = getDistributor() info = distributor.getInfo(includeTokens=True, includeWhitelist=False) metadatas = info["tokens"] # Check that all uploaded metadatas are correct click.echo(f"Checking current on-chain metadatas...") for i in range(0, len(metadatas)): with open(os.path.join(asset_folder, str(i) + ".json")) as f: data = f.read() meta = json.loads(data) dumped = json.dumps(meta) if metadatas[i] != dumped: click.echo( f"Metadata mismatch! Index: {i}\n Metadata local: {dumped}\nMetadata remote: {metadatas[i]}" ) click.echo(f"Updating...") distributor.setToken(msig=msig, index=i, metadata=dumped) # Upload the remaining click.echo(f"Uploading remaining metadatas...") metadatasBatch = [] batchSize = 0 for i in range(len(metadatas), numAssets): with open(os.path.join(asset_folder, str(i) + ".json")) as f: data = f.read() meta = json.loads(data) dumped = json.dumps(meta) #distributor.addTokens(msig=msig, metadatas=[dumped]) # create a batch and cap at 12KB, when target capacity is reached upload it to blockchain metadatasBatch.append(dumped) batchSize += len(dumped) if batchSize > 12000: click.echo( f"Uploadting a batch of {len(metadatasBatch)} metadatas...") result = distributor.addTokens(msig=msig, metadatas=metadatasBatch) metadatasBatch = [] batchSize = 0 if batchSize > 0: click.echo(f"Uploadting a batch of {len(metadatasBatch)} metadatas...") distributor.addTokens(msig=msig, metadatas=metadatasBatch) metadatasBatch = [] batchSize = 0 click.echo(f"Upload complete!")
class BagRecorder(): def __init__(self): types = { 'CameraInfo': CameraInfo, 'Image': Image, 'TFMessage': TFMessage, 'InteractiveMarkerUpdate': InteractiveMarkerUpdate } rospy.init_node(f"rosbag_recorder", anonymous=False) config_path = rospy.get_param("~config") print(config_path) sdk = bosdyn.client.create_standard_sdk('understanding-spot') self.robot = sdk.create_robot('192.168.50.3') with open("/home/spot/config/config") as f: for line in f: line = line.split('/') user = line[0].strip() password = line[1].strip() pinata_pub = line[2].strip() pinata_secret = line[3].strip() self.pinata = PinataPy(pinata_pub, pinata_secret) self.robot.authenticate(user, password) self.state_client = self.robot.ensure_client('robot-state') inf = rosnode.get_node_info_description('/rviz') inf = inf.split() # self.topics_str = '' # types_name = [] # topic = False # for p in inf: # p = p.strip() # if topic: # if p[0] == '/': # self.command_full.append(p) # self.topics_str += f'{p} ' # # if p[0] == '[': # # types_name.append(p.split('/')[-1][:-1]) # if p == "Subscriptions:": # topic = True # if p == "Services:": # topic = False # print(self.command_full) # print(types) self.recording = False i = 0 self.bag = None # for top in topics: # rospy.Subscriber(top, types[f'{types_name[i]}'], self.callback) # i += 1 # def callback(self, data): # #print(data._connection_header['topic']) # if self.bag is not None: # self.bag.write(data._connection_header['topic'], data) def record(self): rospy.loginfo('Starting recorging') date = time.strftime('%x') date = date.split('/') file_name_full = f'/home/spot/rosbags/lesson_one_full_{date[0]}_{date[1]}_{date[2]}_{time.time()}' self.command_full = [ 'rosbag', 'record', f'--output-name={file_name_full}', '/spot/depth/back/camera_info', '/spot/depth/back/image', '/spot/depth/frontleft/camera_info', '/spot/depth/frontleft/image', '/spot/depth/frontright/camera_info', '/spot/depth/frontright/image', '/spot/depth/left/camera_info', '/spot/depth/left/image', '/spot/depth/right/camera_info', '/spot/depth/right/image', '/tf', '/tf_static', '/twist_marker_server/update' ] file_name = f'/home/spot/rosbags/lesson_one_{date[0]}_{date[1]}_{date[2]}_{time.time()}' self.command = [ 'rosbag', 'record', f'--output-name={file_name}', '/tf', '/tf_static' ] rosbag_proc = subprocess.Popen(self.command) rosbag_proc_full = subprocess.Popen(self.command_full) power_on = True while power_on: power_on = self.robot.is_powered_on() time.sleep(1) #power_on = False rosbag_proc.terminate() rosbag_proc_full.terminate() rospy.loginfo('Finished recording') time.sleep(2) res = self.pinata.pin_file_to_ipfs(f'{file_name}.bag') rospy.loginfo(f"Published to IPFS with hash: {res['IpfsHash']}") def spin(self): while True: try: power = self.robot.is_powered_on() #power = True if power: self.record() except KeyboardInterrupt: exit()
def setUp(self): self.PINATA_API_KEY = os.environ.get("PINATA_API_KEY") self.PINATA_SECRET_API_KEY = os.environ.get("PINATA_SECRET_API_KEY") self.pinata = PinataPy(self.PINATA_API_KEY, self.PINATA_SECRET_API_KEY)