make it pin all existing packs in the contract
Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
parent
1d2bdfc741
commit
f012289b99
19
ipfs.py
19
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)
|
||||
|
|
17
main.py
17
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)
|
||||
|
|
4
pack.py
4
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 '<Pack hash={} imgs={}>'.format(
|
||||
self.content_hash, len(self.image_hashes))
|
||||
|
|
Loading…
Reference in New Issue