From f012289b99c263e427433926594526f286c22c27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Mon, 15 Jun 2020 16:57:15 +0200 Subject: [PATCH] make it pin all existing packs in the contract MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jakub SokoĊ‚owski --- ipfs.py | 19 +++++++------------ main.py | 17 +++++++++++++---- pack.py | 4 ++++ 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/ipfs.py b/ipfs.py index 18d101f..309d1c1 100644 --- a/ipfs.py +++ b/ipfs.py @@ -4,15 +4,6 @@ import requests import content_hash # https://pypi.org/project/content-hash import ipfscluster # https://pypi.org/project/ipfscluster -# TODO parametrize -IPFS_CLUSTER_ADDR = 'http://localhost:9094' - -# TODO parametrize -STICKER_PACKS_META_URLS = [ - "https://cloudflare-ipfs.com/ipfs/QmWVVLwVKCwkVNjYJrRzQWREVvEk917PhbHYAUhA1gECTM", - "https://cloudflare-ipfs.com/ipfs/QmWpG2Q5NB472KLgFysdCjB8D1Qf9hxR2KNJvtCJQJufDj", -] - # Converts binary content hash to text verison, see EIP-1577 def ipfsBinToText(text): return content_hash.decode(text) @@ -22,10 +13,14 @@ class IpfsPinner: def __init__(self, addr=ipfscluster.DEFAULT_ADDR): self.client = ipfscluster.connect(addr) - def is_pinned(self, chash): + def statuses(self, chash): resp = self.client.pins.ls(chash) - statuses = [peer['status'] for peer in resp['peer_map'].values()] - return all(s == 'pinned' for s in statuses), statuses + return [peer['status'] for peer in resp['peer_map'].values()] + + def is_pinned(self, chash): + return all(s == 'pinned' for s in self.statuses(chash)) def pin(self, chash): + if self.is_pinned(chash): + return True return self.client.pins.add(chash) diff --git a/main.py b/main.py index 012f032..a9907e4 100755 --- a/main.py +++ b/main.py @@ -9,15 +9,24 @@ from ipfs import IpfsPinner from pack import StickerPack, ipfsBinToText from contract import StickerPackContract -STICKER_PACK_CONTRACT = "0x0577215622f43a39F4Bc9640806DFea9b10D2A36" +SPACK_CONTRACT = "0x0577215622f43a39F4Bc9640806DFea9b10D2A36" with open("./abi.json", "r") as f: STICKER_PACK_ABI = json.load(f) w3 = Web3(Web3.HTTPProvider("http://localhost:8545")) -s = StickerPackContract(STICKER_PACK_CONTRACT, STICKER_PACK_ABI, w3) +ipfs = IpfsPinner() -pack_hashes = s.getAllPackHashes() +sPack = StickerPackContract(SPACK_CONTRACT, STICKER_PACK_ABI, w3) + +pack_hashes = sPack.getAllPackHashes() packs = [StickerPack(ipfsBinToText(h)) for h in pack_hashes] -print(packs) + +for pack in packs: + print("pack:", pack) + rval = ipfs.pin(pack.content_hash) + for chash in pack.image_hashes: + print("image:", chash) + ipfs.pin(pack.content_hash) + print("rval:", rval) diff --git a/pack.py b/pack.py index e7759f3..0872d44 100644 --- a/pack.py +++ b/pack.py @@ -24,3 +24,7 @@ class StickerPack: matches = re.findall(StickerPack.content_hash_rgx, data) # IPFS can't handle EIP-1577 content hashes return [ipfsBinToText(ch) for ch in matches] + + def __repr__(self): + return ''.format( + self.content_hash, len(self.image_hashes))