Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from threading import Thread
- import socket
- from time import time, sleep
- import urllib.request
- import urllib.parse
- from pathlib import Path
- from hashlib import sha512
- from json import loads, dumps
- def api(*argv, **kwargs):
- url = "http://127.0.0.1:5001/api/v0/"
- for arg in argv:
- arg = arg.replace(" ", "/")
- if arg[:-1] != "/":
- arg += "/"
- url += arg
- url = url[0:-1]
- if kwargs:
- url+="?"
- for val in kwargs:
- if val != "post":
- url = url + val + "=" + kwargs[val] + "&"
- url = url[0:-1]
- print(url)
- try:
- if "post" in kwargs:
- print("POST DATA")
- with urllib.request.urlopen(url=url, data=urllib.parse.urlencode(kwargs["post"]).encode("ascii")) as response:
- return response.read()
- else:
- with urllib.request.urlopen(url, timeout=300) as response:
- return response.read()
- except:
- return b"""{"ERROR": "CANNOT CONNECT TO IPFS!"}"""
- def get_time(obj):
- return obj["time"]
- class file(object):
- def __init__(self, p):
- self.p = p
- if self.p[0] != "/":
- self.p = "/" + self.p
- def read(self):
- return api("files", "read", arg=self.p).decode()
- def write(self, s, *argv):
- if argv:
- return api("files", "write", arg=self.p, offset=str(argv[0]), create="True", parents="True", post={"Data": s})
- else:
- return api("files", "write", arg=self.p, truncate="True", create="True", parents="True", post={"Data": s})
- class setup():
- def __init__(self):
- Path("profile").mkdir(parents=True, exist_ok=True)
- api("files", "mkdir", arg="/GANN", parents="True")
- self.id = loads(api("id").decode())["ID"]
- self.update_root()
- self.timeline = []
- self.root_hash = ""
- Thread(target=self.cron).start()
- def status(self, s):
- s = {"time": time(), "status": s}
- with open("profile/status", "a") as fob:
- pass
- try:
- with open("profile/status", "r") as fob:
- dict = loads(fob.read())
- dict.append(s)
- dict.sort(key=get_time, reverse=True)
- with open("profile/status", "w") as fob:
- fob.write(dumps(dict))
- except:
- with open("profile/status", "w") as fob:
- fob.write(dumps([s]))
- api("files", "write", arg = str(Path("profile/status").absolute()))
- self.update_root()
- def update_root(self):
- try:
- for entry in loads(api("files", "ls", l="True").decode())["Entries"]:
- if entry["Name"] == "GANN":
- self.root_hash = entry["Hash"]
- api("name", "publish", arg=self.root_hash)
- except:
- return """{"ERROR": "CANNOT FIND ROOT DIRECTORY"}"""
- def cron(self):
- while True:
- #print("Getting Status Updates!")
- sleep(6)
RAW Paste Data
Copied