2020-06-08 10:45:38 +00:00
|
|
|
import re
|
|
|
|
import json
|
|
|
|
import requests
|
2020-06-17 12:23:15 +00:00
|
|
|
import multiaddr
|
2020-06-08 10:45:38 +00:00
|
|
|
import content_hash # https://pypi.org/project/content-hash
|
|
|
|
import ipfscluster # https://pypi.org/project/ipfscluster
|
|
|
|
|
2020-06-09 16:59:27 +00:00
|
|
|
# Converts binary content hash to text verison, see EIP-1577
|
|
|
|
def ipfsBinToText(text):
|
|
|
|
return content_hash.decode(text)
|
|
|
|
|
2020-06-08 10:45:38 +00:00
|
|
|
class IpfsPinner:
|
2020-06-17 08:10:22 +00:00
|
|
|
|
2020-06-08 10:45:38 +00:00
|
|
|
|
2020-06-17 12:23:15 +00:00
|
|
|
def __init__(self, addr='/dns/localhost/tcp/9094/http'):
|
|
|
|
self.address = multiaddr.Multiaddr(addr)
|
2020-06-17 08:10:22 +00:00
|
|
|
self.cluster = ipfscluster.connect(addr)
|
2020-06-08 10:45:38 +00:00
|
|
|
|
2020-06-15 14:57:15 +00:00
|
|
|
def statuses(self, chash):
|
2020-06-15 21:59:30 +00:00
|
|
|
return [
|
|
|
|
peer['status'] for
|
|
|
|
peer in self.ls(chash)['peer_map'].values()
|
|
|
|
]
|
2020-06-15 14:57:15 +00:00
|
|
|
|
2020-07-27 11:51:24 +00:00
|
|
|
def status(self, chash):
|
|
|
|
statuses = self.statuses(chash)
|
|
|
|
if all(s == 'pinned' for s in statuses):
|
|
|
|
return 'pinned'
|
|
|
|
elif all(s == 'unpinned' for s in statuses):
|
|
|
|
return 'unpinned'
|
|
|
|
elif all(s == 'pinning' for s in statuses):
|
|
|
|
return 'pinning'
|
|
|
|
elif all(s == 'pin_queued' for s in statuses):
|
|
|
|
return 'pin_queued'
|
|
|
|
elif all(s == 'pin_error' for s in statuses):
|
|
|
|
return 'failed'
|
|
|
|
elif any(s == 'pin_error' for s in statuses):
|
|
|
|
return 'errors'
|
|
|
|
else:
|
|
|
|
return 'mixed'
|
|
|
|
|
2020-06-15 14:57:15 +00:00
|
|
|
def is_pinned(self, chash):
|
2020-07-27 11:51:24 +00:00
|
|
|
statuses = self.statuses(chash)
|
|
|
|
return all(s == 'pinned' for s in statuses)
|
2020-06-08 10:45:38 +00:00
|
|
|
|
|
|
|
def pin(self, chash):
|
2020-06-15 14:57:15 +00:00
|
|
|
if self.is_pinned(chash):
|
2020-07-27 11:51:24 +00:00
|
|
|
return 'pinned'
|
2020-06-15 21:59:30 +00:00
|
|
|
|
2020-06-17 08:10:22 +00:00
|
|
|
rval = self.cluster.pins.add(chash)
|
2020-07-27 11:51:24 +00:00
|
|
|
return self.status(chash)
|
2020-06-15 21:59:30 +00:00
|
|
|
|
|
|
|
def ls(self, chash):
|
2020-06-17 08:10:22 +00:00
|
|
|
return self.cluster.pins.ls(chash)
|