-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinata.py
45 lines (39 loc) · 1.42 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
import requests
import json
import logging
PINATA_API_KEY=json.load(open("keys.json", "r"))["pinata_api_key"]
PINATA_SECRET_API_KEY=json.load(open("keys.json", "r"))["pinata_secret_api_key"]
def add_to_ipfs(data_dict, name) :
headers = {
'Content-Type': 'application/json',
'pinata_api_key': PINATA_API_KEY,
'pinata_secret_api_key': PINATA_SECRET_API_KEY}
data = {
'pinataMetadata' : {
'name' : name
},
'pinataContent' : data_dict
}
r = requests.post('https://api.pinata.cloud/pinning/pinJSONToIPFS', data=json.dumps(data), headers=headers)
if not 199<r.status_code<300 :
logging.warning("POST access to Pinatta refused")
return None
else :
return r.json()['IpfsHash']
def add_file_to_pinata (filename) :
try :
this_file = open(filename, mode='rb') # b is important -> binary
except IOError :
logging.error('IOEroor open file ')
return None
headers = { 'pinata_api_key': PINATA_API_KEY,
'pinata_secret_api_key': PINATA_SECRET_API_KEY}
payload = { 'file' : this_file.read()}
try :
response = requests.post('https://api.pinata.cloud/pinning/pinFileToIPFS', files=payload, headers=headers)
except :
logging.error('IPFS connexion problem')
return None
this_file.close()
return response.json()['IpfsHash']
print(add_file_to_pinata("nft.png"))