-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
219 lines (171 loc) · 6.62 KB
/
main.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import os
import requests
from flask import Flask, redirect, url_for, render_template, request, flash, jsonify
from threading import Thread
nftportal_key = os.environ['nftportal_key']
opensea_key = os.environ['opensea_key']
akapi_key = os.environ['akapi_key']
app = Flask(__name__, template_folder='html')
#send html page in response
@app.route('/')
def index():
return render_template('index.html', api_key=akapi_key)
# return "NFT Viewer App - Soon"
def run():
app.run(host='0.0.0.0', port=8080)
# A route to return all of the available entries in our catalog.
@app.route('/v1/address', methods=['GET'])
def api_all():
reply = []
owner = '0x72d3a53f709652ee730ef9807fb4ab8771485a92'
url = f"https://api.opensea.io/api/v1/assets?owner={owner}&order_direction=desc&offset=0&limit=20"
response = requests.request("GET", url, headers={
"X-API-KEY": opensea_key
}).json()
try:
total_assets = len(response['assets'])
reply.append({'total_assets': total_assets})
for x in range(total_assets):
print(response['assets'][x]['image_url'])
data = {
'name': response['assets'][x]['name'],
'img': response['assets'][x]['image_url']
}
reply.append(data)
if total_assets == 0:
print('No owned NFTs')
reply.append('No owned NFTs')
except:
print(response)
reply.append(response)
print(reply)
return jsonify(reply)
@app.route('/v1/opensea', methods=['POST'])
def api_post():
data = request.json
assets = []
owner = data["address"]
url = f"https://api.opensea.io/api/v1/assets?owner={owner}&order_direction=desc&offset=0&limit=20"
response = requests.request("GET", url, headers={
"X-API-KEY": opensea_key
}).json()
try:
total_assets = len(response['assets'])
assets.append({'total_assets': total_assets})
for x in range(total_assets):
print(response['assets'][x]['image_url'])
data = {
'name': response['assets'][x]['name'],
'img': response['assets'][x]['image_url']
}
assets.append(data)
if total_assets == 0:
print('No owned NFTs')
assets.append('')
except:
print(response)
assets.append(response)
print(assets)
return jsonify(assets)
@app.route('/NFTPortal', methods=['POST'])
def nftportal_api_post():
data = request.json
assets = []
#if the data doesn't contain key named address then it's not a valid request
if 'address' not in data:
return 'Err: Invalid request. Provide Address.'
else:
owner = data["address"]
if 'auth' not in data:
return 'Err: Invalid request. Provide Authentication Key.'
else:
auth = data["auth"]
if auth == akapi_key:
url = f"https://api.nftport.xyz/v0/accounts/{owner}"
response_eth = requests.request("GET",
url,
headers={
'Content-Type': "application/json",
'Authorization': nftportal_key
},
params={
"chain": "ethereum"
}).json()
response_poly = requests.request("GET",
url,
headers={
'Content-Type':
"application/json",
'Authorization': nftportal_key
},
params={
"chain": "polygon"
}).json()
#loop for ethereum chain NFTs
try:
total_assets = len(response_eth['nfts'])
assets.append({'total_assets': total_assets})
for x in range(total_assets):
print(response_eth['nfts'][x]['file_url'])
data = {
'description': response_eth['nfts'][x]['description'],
'img': response_eth['nfts'][x]['file_url'],
'name': response_eth['nfts'][x]['name'],
'chain': 'Ethereum'
}
assets.append(data)
if total_assets == 0:
print('No owned NFTs on ETH Chain')
assets.append('')
#change link
for x in range(1, len(assets)):
if 'img' in assets[x]:
if assets[x]['img'][:4] == 'ipfs':
assets[x]['img'] = ipfs_link_changer(assets[x]['img'])
print(assets[x]['img'])
else:
print('Not a IPFS link')
except:
print(response_eth)
assets.append(response_eth)
#loop for polygon chain NFTs
try:
total_assets_poly = len(response_poly['nfts'])
assets[0]['total_assets'] += total_assets_poly
for x in range(total_assets_poly):
print(response_poly['nfts'][x]['file_url'])
data = {
'description': response_poly['nfts'][x]['description'],
'img': response_poly['nfts'][x]['file_url'],
'name': response_poly['nfts'][x]['name'],
'chain': 'Polygon'
}
assets.append(data)
if total_assets_poly == 0:
print('No owned NFTs on Polygon Chain')
assets.append('')
except:
print(response_poly)
assets.append(response_poly)
for x in range(1, len(assets)):
if 'img' in assets[x]:
try:
if assets[x]['img'][:4] == 'ipfs':
assets[x]['img'] = ipfs_link_changer(assets[x]['img'])
print(assets[x]['img'])
else:
print('Not a IPFS link')
except:
pass
return jsonify(assets)
else:
return jsonify('Auth failed')
t = Thread(target=run)
t.start()
def ipfs_link_changer(url):
#ipfs link changer from ipfs:// to https://ipfs.io/
try:
url = url.replace('ipfs://', 'https://ipfs.io/ipfs/')
return url
except:
print('Invalid IPFS links')