-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLstarAccount_text.py
336 lines (268 loc) · 14.6 KB
/
LstarAccount_text.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# we use solscan api to get all account,thanks solscan give me convenient
# solscan url is 'https://public-api.solscan.io/docs/#/Token/get_token_list',see this
# for any NFT and user data I use mysql to restore. So before you use this,make sure you mysql server has run.
import requests
import json
import pymysql
import time
# base url
tokenAddress = 'C6qep3y7tCZUJYDXHiwuK46Gt6FsoxLi8qV1bTCRYaY1'
baseURL = 'public-api.solscan.io/'
header = {
'authority':'public-api.solscan.io',
'method': 'GET',
'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Mobile Safari/537.36'
}
with open("data.txt","w") as data:
data.write('')
with open("erroruserAccount.txt","w") as erroruserAccount:
erroruserAccount.write("这是个测试!")
erroruserAccount.write("这是个测试!")
print(erroruserAccount,'hello')
with open("errorNFT.txt","w") as errorNFT:
errorNFT.write("这是个测试!")
def mysql_insert_test(useraccount = 'C6qep3y7tCZUJYDXHiwuK46Gt6FsoxLi8qV1bTCRYaY1',usdc=100,lstar=1312.1,nftaddress='DDDsbU5q717qG4pop8dpNvtso5P1UvhQpiWCtQMBhDzm'):
conn = pymysql.connect(host='127.0.0.1', user='root', password='1416615127dj', database='letmespeak')
# cursor=pymysql.cursors.DictCursor,是为了将数据作为一个字典返回
cursor = conn.cursor()
sql = "insert into Userdata(useraccount,usdc,lstar,nftaddress) values('%s','%s','%s','%s')" % \
(useraccount, usdc, lstar, nftaddress)
print(sql)
rows = cursor.execute(sql)
print(rows)
result = cursor.fetchall()
print(result)
# 这里一定要提交
conn.commit()
cursor.close()
conn.close()
def get_last_block_test():
response = requests.get(url='https://public-api.solscan.io/block/last?limit=10',headers = header)
print(response.content)
def _get_account_num_by_token(address):
'''
:param address:
:return:
'''
url = 'https://' + baseURL + 'token/holders?tokenAddress=' + address
response = requests.get(url=url, headers=header)
total_address_account = json.loads(response.content.decode())['total']
print('total_address_account is ', total_address_account)
return total_address_account
def get_account_address_by_token(address):
total_address_account = _get_account_num_by_token(address)
print('please waite , it need time to waite get reponse data')
url = 'https://' + baseURL + 'token/holders?tokenAddress=' + address + '&limit='+str(total_address_account)
try:
response = requests.get(url=url, headers=header)
all_account_address = json.loads(response.content.decode())['data']
for i in all_account_address:
get_NFT_token_list_by_owner(i['owner'])
#print(i['owner'])
print('from onwer we can get SPL token balance,it include LSTAR and NFT')
except:
print('error network,please try again')
def get_NFT_token_list_by_owner_test(ownerAddress = '5Xz9hBP75payPpxxtMNtPDyNLMq8WL5hC57MPGJVYPft' ):
url = 'https://' + baseURL + 'account/tokens?account=' + ownerAddress
# we will get the 'USD Coin' and 'Learning Star'
USDC = 0
LSTAR = 0
try:
response = requests.get(url=url, headers=header)
token_list = json.loads(response.content.decode())
print('this owner token list follow this:')
for i in token_list:
# print(i)
if i['tokenAmount']['amount'] == '1' and i['tokenAmount']['decimals'] == 0 and i['tokenAmount']['uiAmountString'] == '1' and i['tokenName'] =='' and i['tokenIcon'] == '':
print('NFT address is',i['tokenAddress'])
elif i['tokenName'] == 'USD Coin':
print('USD Coin balance is ',i['tokenAmount']['uiAmountString'])
elif i['tokenName'] == 'Learning Star':
print('Learning Sta balance is ', i['tokenAmount']['uiAmountString'])
except:
print('error network,please try again')
def get_NFT_token_list_by_owner(ownerAddress):
url = 'https://' + baseURL + 'account/tokens?account=' + ownerAddress
# we will get the 'USD Coin' and 'Learning Star'
USDC = 0
LSTAR = 0
nft = ''
try:
response = requests.get(url=url, headers=header)
token_list = json.loads(response.content.decode())
print('this owner token list follow this:')
#first to get usdc and lstar
for i in token_list:
if i['tokenName'] == 'USD Coin':
USDC = i['tokenAmount']['uiAmountString']
#print('USD Coin balance is ',USDC)
if i['tokenName'] == 'Learning Star':
LSTAR =i['tokenAmount']['uiAmountString']
# second get the NFT address
for i in token_list:
# print(i)
#time.sleep(1)
if i['tokenAmount']['amount'] == '1' and i['tokenAmount']['decimals'] == 0 and i['tokenAmount']['uiAmountString'] == '1' and i['tokenName'] =='' and i['tokenIcon'] == '':
nft =i['tokenAddress']
#print('NFT address is',nft)
try:
url = get_NFT_uri(nft)
(name, number, talent, activated, rarity, currency_reward,
learning_speed, visa_total, visa_left, xp_level, invites_total, invites_left, banned) = get_NFT_totalInfo_by_Uri(url)
except:
print('uri or NFT info error')
#continue
else:
continue
# storage to mysql database
# print(name, number, talent, activated, rarity, currency_reward,
# learning_speed, visa_total, visa_left, xp_level, invites_total, invites_left, banned)
print('插入成功1', ownerAddress, USDC, LSTAR, nft, name, number, talent, activated, rarity, currency_reward,
learning_speed, visa_total, visa_left, xp_level, invites_total, invites_left, banned)
# we use txt to restored
data.writelines(ownerAddress + '\t' + USDC + '\t' + LSTAR + '\t' + nft + '\t' + name + '\t' + number + '\t' + talent + '\t' + activated + '\t' + rarity + '\t' + currency_reward + '\t' + learning_speed + '\t' + visa_total + '\t' + visa_left + '\t' + xp_level + '\t' + invites_total + '\t' + invites_left + '\t' + banned + '\n')
print('插入成功', ownerAddress, USDC, LSTAR, nft, name, number, talent, activated, rarity, currency_reward,
learning_speed, visa_total, visa_left, xp_level, invites_total, invites_left, banned)
errorNFT.write(nft + '\n')
print('get item error', i['tokenAddress'])
except:
print('有错误')
erroruserAccount.write(ownerAddress)
print('error network,please try again-- ',ownerAddress,' -- had to list, Already have ')
def get_NFT_uri_test(NFTaddress = '9D16eYPYJcJv7z3251b1QLp72cZDonnW1eRA9VDep6Rz'):
# there had issue, if we want to get NFT info the base url not work,we should use
# https://api.solscan.io/account?address = + 'NFT address' ,it work
url = 'https://api.solscan.io/account?address=' + NFTaddress
print(url)
try:
response = requests.get(url=url, headers=header)
print(response)
NFT_info = json.loads(response.content.decode())
print('this NFT info follow this:')
print(NFT_info['data']['tokenInfo'])
print(NFT_info['data']['metadata']['data'])
print(NFT_info['data']['metadata']['data']['uri'])
except:
print('error network,please try again')
def get_NFT_uri(NFTaddress):
# there had issue, if we want to get NFT info the base url not work,we should use
# https://api.solscan.io/account?address = + 'NFT address' ,it work
url = 'https://api.solscan.io/account?address=' + NFTaddress
# print(url)
try:
response = requests.get(url=url, headers=header)
# print(response)
NFT_info = json.loads(response.content.decode())
# print('this NFT info follow this:')
#
# print(NFT_info['data']['tokenInfo'])
# print(NFT_info['data']['metadata']['data'])
# print(NFT_info['data']['metadata']['data']['uri'])
except:
print('GET Uri error')
return
return NFT_info['data']['metadata']['data']['uri']
def get_NFT_totalInfo_by_Uri(uri):
# uri info NFT all info ,it pretty important. {"name":"#782107","symbol":"","description":"","seller_fee_basis_points":0,"image":"https://letmespeak.akamaized.net/avatars/6d074e78-35d2-4092-8012-0770fb40f425.png","external_url":"https://letmespeak.org","properties":{"files":[{"uri":"https://letmespeak.akamaized.net/avatars/6d074e78-35d2-4092-8012-0770fb40f425.png","type":"image/png"}],"category":"image","creators":[{"share":100,"address":"Fbm31wFSEkL33JT7UaYac9ZJeDjAFMfUHi4wzZEmojKy","verified":1}]},"attributes":[{"trait_type":"name","value":"Kingg"},{"trait_type":"number","value":782107},{"trait_type":"talent","value":31},{"trait_type":"activated","value":true},{"trait_type":"rarity","value":3},{"trait_type":"currency_reward","value":1},{"trait_type":"learning_speed","value":106},{"trait_type":"visa_total","value":150},{"trait_type":"visa_left","value":142},{"trait_type":"xp_level","value":10},{"trait_type":"xp","value":16490},{"trait_type":"invites_total","value":6},{"trait_type":"invites_left","value":6},{"trait_type":"skill_vocabulary","value":97},{"trait_type":"skill_pronunciation","value":68},{"trait_type":"skill_listening","value":79},{"trait_type":"skill_grammar","value":62},{"trait_type":"banned","value":false}]}
# some data need to record,follow this:
name = ''
number = 0
talent = 0
activated = None
rarity = 0
currency_reward = 0
learning_speed = 0
visa_total = 0
visa_left = 0
xp_level = 0
invites_total = 0
invites_left = 0
banned = None
try:
response = requests.get(url=uri, headers=header)
NFT_info = json.loads(response.content.decode())
# print(NFT_info['attributes'])
for i in NFT_info['attributes']:
# print(i)
if i['trait_type'] == 'name':
name = i['value']
# print(i['value'])
elif i['trait_type'] == 'number':
number = i['value']
# print(i['value'])
elif i['trait_type'] == 'talent':
talent = i['value']
# print(i['value'])
elif i['trait_type'] == 'activated':
activated = i['value']
# print(i['value'])
elif i['trait_type'] == 'rarity':
rarity = i['value']
# print(i['value'])
elif i['trait_type'] == 'currency_reward':
currency_reward = i['value']
# print(i['value'])
elif i['trait_type'] == 'learning_speed':
learning_speed = i['value']
# print(i['value'])
elif i['trait_type'] == 'visa_total':
visa_total = i['value']
# print(i['value'])
elif i['trait_type'] == 'visa_left':
visa_left = i['value']
# print(i['value'])
elif i['trait_type'] == 'xp_level':
xp_level = i['value']
# print(i['value'])
elif i['trait_type'] == 'invites_total':
invites_total = i['value']
# print(i['value'])
elif i['trait_type'] == 'invites_left':
invites_left = i['value']
# print(i['value'])
elif i['trait_type'] == 'banned':
banned = i['value']
# print(i['value'])
return [name,number,talent,activated ,rarity ,currency_reward ,learning_speed ,visa_total,visa_left ,xp_level ,invites_total ,invites_left , banned]
except:
print('get nft info error')
return 'error'
def get_NFT_totalInfo_by_Uri_test(uri = 'https://api2.letmespeak.pro/api/1.0/metadata/8b630bc7-8e91-47f8-b969-0acaa7616587'):
# uri info NFT all info ,it pretty important. {"name":"#782107","symbol":"","description":"","seller_fee_basis_points":0,"image":"https://letmespeak.akamaized.net/avatars/6d074e78-35d2-4092-8012-0770fb40f425.png","external_url":"https://letmespeak.org","properties":{"files":[{"uri":"https://letmespeak.akamaized.net/avatars/6d074e78-35d2-4092-8012-0770fb40f425.png","type":"image/png"}],"category":"image","creators":[{"share":100,"address":"Fbm31wFSEkL33JT7UaYac9ZJeDjAFMfUHi4wzZEmojKy","verified":1}]},"attributes":[{"trait_type":"name","value":"Kingg"},{"trait_type":"number","value":782107},{"trait_type":"talent","value":31},{"trait_type":"activated","value":true},{"trait_type":"rarity","value":3},{"trait_type":"currency_reward","value":1},{"trait_type":"learning_speed","value":106},{"trait_type":"visa_total","value":150},{"trait_type":"visa_left","value":142},{"trait_type":"xp_level","value":10},{"trait_type":"xp","value":16490},{"trait_type":"invites_total","value":6},{"trait_type":"invites_left","value":6},{"trait_type":"skill_vocabulary","value":97},{"trait_type":"skill_pronunciation","value":68},{"trait_type":"skill_listening","value":79},{"trait_type":"skill_grammar","value":62},{"trait_type":"banned","value":false}]}
# some data need to record,follow this:
# trait_type 'activated'
# trait_type 'rarity'
# trait_type': 'currency_reward'
# trait_type': 'learning_speed'
# 'trait_type': 'visa_total'
# 'trait_type': 'visa_left'
# 'trait_type': 'invites_total'
# 'trait_type': 'invites_left'
# 'trait_type': 'banned'
try:
response = requests.get(url=uri, headers=header)
NFT_info = json.loads(response.content.decode())
print(NFT_info['attributes'])
for i in NFT_info['attributes']:
print(i)
# if i['trait_type'] == 'visa_left':
# print(i['value'])
except:
print('error network,please try again')
def get_mysql_inser_different_boo_int_char_test(banned=True, nftaddress='D16eYPYJcJv7z3251b1QLp72cZDonnW1eRA9VDep6Rz', lstar=111):
conn = pymysql.connect(host='127.0.0.1', user='root', password='1416615127dj', database='letmespeak')
# cursor=pymysql.cursors.DictCursor,是为了将数据作为一个字典返回
cursor = conn.cursor()
#make sure your sql values is right ,in python connect to mysql, we must make sure type is same.
sql = "insert into Test(lstar,nftaddress,banned) values(%d,'%s',%s)" % \
(lstar,nftaddress,banned)
print(sql)
rows = cursor.execute(sql)
conn.commit()
get_account_address_by_token(tokenAddress)
# -- test
# get_mysql_inser_different_boo_int_char_test()
# get_NFT_token_list_by_owner_test()
# get_NFT_uri_test()
# get_NFT_totalInfo_by_Uri_test()
# mysql_insert_test()