Spaces:
Paused
Paused
| 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 | |
| } |