forked from santiago-pesantez/emr-blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinata.py
50 lines (44 loc) · 1.24 KB
/
pinata.py
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
import os
import json
import requests
from dotenv import load_dotenv
load_dotenv()
json_headers = {
"Content-Type": "application/json",
"pinata_api_key": os.getenv("PINATA_API_KEY"),
"pinata_secret_api_key": os.getenv("PINATA_SECRET_API_KEY"),
}
file_headers = {
"pinata_api_key": os.getenv("PINATA_API_KEY"),
"pinata_secret_api_key": os.getenv("PINATA_SECRET_API_KEY"),
}
def convert_data_to_json(content):
data = {"pinataOptions": {"cidVersion": 1}, "pinataContent": content}
return json.dumps(data)
def pin_file_to_ipfs(data):
r = requests.post(
"https://api.pinata.cloud/pinning/pinFileToIPFS",
files={'file': data},
headers=file_headers
)
print(r.json())
ipfs_hash = r.json()["IpfsHash"]
return ipfs_hash
def pin_json_to_ipfs(json):
r = requests.post(
"https://api.pinata.cloud/pinning/pinJSONToIPFS",
data=json,
headers=json_headers
)
print(r.json())
ipfs_hash = r.json()["IpfsHash"]
return ipfs_hash
def hash_json_metadata(json):
r = requests.put(
"https://api.pinata.cloud/pinning/hashMetadata",
data=json,
headers=json_headers
)
# print(r.json())
# ipfs_hash = r.json()["IpfsHash"]
return r.text