File size: 1,741 Bytes
11bdd8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import requests
import json
import urllib.parse
# safe_string = urllib.parse.quote_plus(...)


BASE_URL = "http://127.0.0.1:5001/api/v0/"

def cat(cid):
    return requests.post(f"{BASE_URL}cat?arg={cid}").text


# curl -X POST -F file=@myfile "http://127.0.0.1:5001/api/v0/add?quiet=<value>&quieter=<value>&silent=<value>&progress=<value>&trickle=<value>&only-hash=<value>&wrap-with-directory=<value>&chunker=<value>&raw-leaves=<value>&nocopy=<value>&fscache=<value>&cid-version=<value>&hash=<value>&inline=<value>&inline-limit=32&pin=true&to-files=<value>"

def add(relative_path, filename, folder=""):
    with open(relative_path, "rb") as file:
        files = {"file": (urllib.parse.quote_plus(folder), file, "application/octet-stream")}
        response = requests.post(f"{BASE_URL}add", files=files)
        
        # Split the response into two separate strings
        responses = response.text.split('\n')
        responses = [response for response in responses if response]

        # Parse each string into JSON
        json_responses = [json.loads(response) for response in responses]

    return json_responses
    # return requests.post(f"{BASE_URL}add", files={"file": open(filename, "rb")}).json()

def files_ls():
    return requests.post(f"{BASE_URL}files/ls").json()

files = {
    "ls": files_ls
}

def pin_ls():
    return requests.post(f"{BASE_URL}pin/ls").json()

def unpin(cid: str):
    return requests.post(f"{BASE_URL}pin/rm?arg={cid}").json()

def add_pin(cid: str):
    return requests.post(f"{BASE_URL}pin/add?arg={cid}").json()


#  /api/v0/stats/bw

def stats_bw():
    return requests.post(f"{BASE_URL}stats/bw").json()


stats = {
    "bw": stats_bw
}