Guest User

library.py

a guest
Jun 10th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.04 KB | None | 0 0
  1. from threading import Thread
  2. import socket
  3. from time import time, sleep
  4. import urllib.request
  5. import urllib.parse
  6. from pathlib import Path
  7. from hashlib import sha512
  8. from json import loads, dumps
  9.  
  10. def api(*argv, **kwargs):
  11.     url = "http://127.0.0.1:5001/api/v0/"
  12.     for arg in argv:
  13.         arg = arg.replace(" ", "/")
  14.         if arg[:-1] != "/":
  15.             arg += "/"
  16.         url += arg
  17.     url = url[0:-1]
  18.     if kwargs:
  19.         url+="?"
  20.         for val in kwargs:
  21.             if val != "post":
  22.                 url = url + val + "=" + kwargs[val] + "&"
  23.         url = url[0:-1]
  24.         print(url)
  25.     try:
  26.         if "post" in kwargs:
  27.             print("POST DATA")
  28.             with urllib.request.urlopen(url=url, data=urllib.parse.urlencode(kwargs["post"]).encode("ascii")) as response:
  29.                 return response.read()
  30.         else:
  31.             with urllib.request.urlopen(url, timeout=300) as response:
  32.                 return response.read()
  33.     except:
  34.         return b"""{"ERROR": "CANNOT CONNECT TO IPFS!"}"""
  35.  
  36. def get_time(obj):
  37.     return obj["time"]
  38.  
  39. class file(object):
  40.  
  41.     def __init__(self, p):
  42.         self.p = p
  43.         if self.p[0] != "/":
  44.             self.p = "/" + self.p
  45.  
  46.     def read(self):
  47.         return api("files", "read", arg=self.p).decode()
  48.  
  49.     def write(self, s, *argv):
  50.         if argv:
  51.             return api("files", "write", arg=self.p, offset=str(argv[0]), create="True", parents="True", post={"Data": s})
  52.         else:
  53.             return api("files", "write", arg=self.p, truncate="True", create="True", parents="True", post={"Data": s})
  54.  
  55. class setup():
  56.  
  57.     def __init__(self):
  58.         Path("profile").mkdir(parents=True, exist_ok=True)
  59.  
  60.         api("files", "mkdir", arg="/GANN", parents="True")
  61.         self.id = loads(api("id").decode())["ID"]
  62.         self.update_root()
  63.  
  64.         self.timeline = []
  65.         self.root_hash = ""
  66.  
  67.         Thread(target=self.cron).start()
  68.  
  69.     def status(self, s):
  70.         s = {"time": time(), "status": s}
  71.  
  72.         with open("profile/status", "a") as fob:
  73.             pass
  74.  
  75.         try:
  76.             with open("profile/status", "r") as fob:
  77.                 dict = loads(fob.read())
  78.                 dict.append(s)
  79.                 dict.sort(key=get_time, reverse=True)
  80.             with open("profile/status", "w") as fob:
  81.                 fob.write(dumps(dict))
  82.         except:
  83.             with open("profile/status", "w") as fob:
  84.                 fob.write(dumps([s]))
  85.  
  86.         api("files", "write", arg = str(Path("profile/status").absolute()))
  87.  
  88.         self.update_root()
  89.  
  90.     def update_root(self):
  91.         try:
  92.             for entry in loads(api("files", "ls", l="True").decode())["Entries"]:
  93.                 if entry["Name"] == "GANN":
  94.                     self.root_hash = entry["Hash"]
  95.             api("name", "publish", arg=self.root_hash)
  96.         except:
  97.             return """{"ERROR": "CANNOT FIND ROOT DIRECTORY"}"""
  98.  
  99.     def cron(self):
  100.         while True:
  101.             #print("Getting Status Updates!")
  102.             sleep(6)
Add Comment
Please, Sign In to add comment