-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch2addr.py
More file actions
437 lines (328 loc) · 12.2 KB
/
search2addr.py
File metadata and controls
437 lines (328 loc) · 12.2 KB
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
from bs4 import BeautifulSoup as BS
import urllib.request as urllib2
import datetime
import re
import csv
import configparser
import mysql.connector
from mysql.connector import Error
import queue
import os
import sys
from positioncalc import getbalance
import concurrent.futures
import queue
import threading
# To add FirstTrans/LastTrans
# turn page
# re-insert duplicate again
# To add multi-thread for crawling networked adddresses
THRESHOLD=3000
def calcprice(filename,pricefile):
pricedata=list(csv.reader(open(pricefile)))
try:
f = open(filename, 'r')
data = f.read()
rows = data.split('\n')
totalCost=0
totalQuantity=0
for row in rows:
try:
if "input" in row:
sign=1
elif "output" in row:
sign=-1
else:
sign=0
quantity=''
text= re.search("(?<= )[\d.]+(?= )",row).group()
quantity=str(text).lstrip()
date=re.search("\d{4}-\d{2}-\d{2}",row).group()
#print ("result)"+text+","+date)
for pricepoint in pricedata:
index=-1
if datetime.datetime.strptime(date,'%Y-%m-%d')>= datetime.datetime.strptime(pricepoint[1],'%d/%m/%Y'):
index=pricedata.index(pricepoint)
if index>-1 and datetime.datetime.strptime(date,'%Y-%m-%d') < datetime.datetime.strptime(pricedata[index+1][1],'%d/%m/%Y'):
cost=sign*float(pricedata[index][0])*float(quantity)
totalCost=totalCost+cost
totalQuantity=totalQuantity+sign*float(quantity)
#print ("cost "+str(cost))
except Exception as msg_1013:
print(msg_1013)
except Exception as e:
print(e)
print ("Avg cost is %s, of %s Tokens"%(totalCost/totalQuantity,totalQuantity))
#(?<= {6})\d*\.?\d* regex
#( )\1+
#(?<=( )\1+)\d*\.?\d* regex
# + +means as many preceding letter as possible
# d6453c7a2a7f83d9b8d51851de25a62081bec153
#curl -X POST --data '{"method":"xdag_get_block_info", "params":["dfKdPEdqac23INOdR/juDDY1LKFRePFk"], "id":1}' localhost:16005
def search(suffix,wallet):
url=suffix+wallet
global result
global dt
global db
global THRESHOLD
#print ("search "+url)
storeList=[]
balanceList=[]
try:
html = urllib2.urlopen(url)
soup = BS(html,'html.parser')
#print (tag.next_element)
#print (tag.nextsibling)
#print (tag.nextsibling.nextsibling)
flag=0
for eliminate in soup.find_all('h4'):
if eliminate.text=='Block as address':
flag=flag+1
if flag==0:
return('',storeList) #Block as transaction need to be removed
#print(soup.find('body').findChildren())
try:
for tag in soup.find_all('a', href=True):
if tag['href'].startswith("/block/") == True and wallet not in tag['href'] and float(str(tag.parent.findNext('td').contents[0]))>THRESHOLD:
#print("%s,%d"%(tag['href'][7:],float(str(tag.parent.findNext('td').contents[0]))))
storeList.append(tag['href'][7:]) #remove string head '/block/'
except Exception as err:
print("error"+str(err))
for tag in soup.find_all("div"):
#tds = tag.find_all("td") early Aug18 # you get list
#print('text:', tds[0].get_text()) # get element [0] from list
#print('value:', tds[1].get_text())
#print ("...."+tag.text+"....")
if tag.text=="Balance":#tag.text.startswith("Balance"):
bal=tag.find_next('span').text.replace(',','')
balanceList.append(bal)
#write_db(bal,wallet,dt,db,'xdag')
#f = open("c:\\result.csv", 'r+')
#f.write(url+","+bal,dt+'\n')
#print (url+","+tag.find_next('span').text,dt+'\n')
if len(balanceList)==0:
balance=''
else:
balance=balanceList[0]
print (wallet+" bal:" +str(balanceList))
return (balance, storeList)#.replace("<td>",u"余额:").replace("<a href=>\"/block","Addr:"))#nextsibling.text)
except:
return ('', storeList)
#soup.find("th", text="Balance").find_next_sibling("td").text
#b.body.findAll(text=re.compile('Trump wins .+? uncertain future'))
#result=soup.body.findAll(text=re.compile('Balance</th>.+?</td>'))
#print (result.text)
'''elem =soup.findAll('td', text = re.compile(ur'Fixed text:(.*)', re.DOTALL), attrs = {'class': 'pos'})#('a', {'title': 'title here'})
# <th scope="row">Balance</th><td>0.950165999
elem[0].text'''
def db_connect():
config = configparser.ConfigParser()
config.read(os.path.join(sys.path[0],'dbconfig.ini'))
try:
dbconn = mysql.connector.connect(host = config['mysqlDB']['host'],
user = config['mysqlDB']['user'],
passwd = config['mysqlDB']['password'],
db = config['mysqlDB']['database'])
if dbconn.is_connected():
print('Connected to MySQL database')
return dbconn
except Exception as err:
print ("error"+str(err))
def write_db(conn,asset_type,address,balance,version,dt):
query0 = "UPDATE balance_history set version=version+1 where asset_type=%s and address=%s "
args0 =(asset_type,address)
query = "INSERT INTO balance_history(asset_type,address,balance,version,create_time) " \
"VALUES(%s,%s,%s,%s,%s)"
args = (asset_type, address,balance,version,dt)
try:
cursor = conn.cursor(buffered=True)
cursor.execute(query0, args0)
cursor.execute(query, args)
if cursor.lastrowid:
print('last insert id', cursor.lastrowid)
else:
print('last insert id not found')
conn.commit()
cursor.close
except Error as error:
print(error)
#id,create_time,asset_type,address,balance,version,detail
def get_info_db(sql,addr,version=0,date='1990-01-01'):
# version=0 means latest
if date>'1990-01-01':
sql=sql+"select balance, create_time from balance_history where address='"+add+"'"
sql=sql+" and date>"
sql=sql+" order by CreateTime desc"
try:
dbconn = mysql.connector.connect(host='localhost',database='python_mysql',user='root',password='secret')
if conn.is_connected():
print('Connected to MySQL database')
cursor = dbconn.cursor(buffered=True)
cursor.execute(sql)
results = cursor.fetchall
for row in results:
print (row[0])
if count(queryresult)>=version:
queryresult[version]
except Exception as err:
print (err)
pass
return balance
def readcsv():
# how to threading and loop until no new addr? while true: dedup a list, move detected to another list
global THRESHOLD
global db
f = open(os.path.join(sys.path[0],"addrlist.csv"), 'r+')
data = f.read()
rows = data.split('\n')
newrows=[]
dt=datetime.datetime.now()#.strftime('%Y-%m-%d')
result=[]
q=queue.Queue()
written=[]
for row in rows:
readlist=[] #G6jTFKRkFlKj67zIdOZJ4jMjuhCe6oOg BLOCK as address, fails
(prt1, readlist) = search("https://explorer.xdag.io/block/",row)
v0=getbalance(db,'xdag',row,0)
if len(prt1)>0 and row not in written and (v0 is None or float(prt1)!=v0[0]): #either not in db, or value changed
write_db(db,'xdag',row,float(prt1),0,dt)
written.append(row)
for NewItem in readlist:
if NewItem not in newrows:
newrows.append(NewItem)
print("newlist")
print(newrows)
for row in newrows:
try:
readlist=[] #G6jTFKRkFlKj67zIdOZJ4jMjuhCe6oOg BLOCK as address, fails
(prt1, readlist) = search("https://explorer.xdag.io/block/",row)
if len(prt1)>0:
print("Gen2 bal:"+prt1+" "+row)
if float(prt1)>THRESHOLD and row not in written:
write_db(db,'xdag',row,float(prt1),0,dt)
written.append(row)
print("DERIVED"+prt1+" "+row)
for NewItem in list(set(readlist)):
if NewItem not in rows and NewItem not in newrows:
q.put(NewItem)
rows.append(NewItem)
f.write(NewItem+'\n')
while not q.empty():
try:
pop=q.get(False)
print("queue search "+pop)
(prt1, readlist) = search("https://explorer.xdag.io/block/",pop)
if len(prt1)>0 and float(prt1)>THRESHOLD and pop not in written:
write_db(db,'xdag',pop,float(prt1),0,dt)
written.append(pop)
print("Gen4 bal:"+prt1+" "+pop)
for gen4 in readlist:
if gen4 not in rows and gen4 not in newrows:
q.put(gen4)
#print("queue added "+gen4)
except Exception as Empty:
print("queue err"+str(Empty))
continue
q.task_done()
#for job in iter(q.get(), None):
except Exception as err:
print ("newrows err"+str(err))
#To make sure that you're data is written to disk, use file.flush() followed by os.fsync(file.fileno()).
#(prt1, readlist) = search("https://explorer.xdag.io/block/"+row)
def read1k():
global THRESHOLD
global db
f = open(os.path.join(sys.path[0],"addrlist.csv"), 'r+')
data = f.read()
rows = data.split('\n')
newrows=[]
dt=datetime.datetime.now()#.strftime('%Y-%m-%d')
result=[]
q=queue.Queue()
written=[]
for row in rows:
readlist=[] #G6jTFKRkFlKj67zIdOZJ4jMjuhCe6oOg BLOCK as address, fails
(prt1, readlist) = search("https://explorer.xdag.io/block/",row)
v0=getbalance(db,'xdag',row,0)
if len(prt1)>0 and row not in written and (v0 is None or float(prt1)!=v0[0]): #either not in db, or value changed
write_db(db,'xdag',row,float(prt1),0,dt)
written.append(row)
def load_url(url):
global written
db=db_connect()
print(url)
row=url
(prt1, readlist) = search("https://explorer.xdag.io/block/",row)
v0=getbalance(db,'xdag',row,0)
if len(prt1)>0 and row not in written and (v0 is None or float(prt1)!=v0[0]): #either not in db, or value changed
write_db(db,'xdag',row,float(prt1),0,dt)
written.append(row)
db.close()
#return handle_response(json,url,dbconn)
def ThreadPoolExecutor():
return concurrent.futures.ThreadPoolExecutor(max_workers=MAX_WORKERS)
INTERVAL = 0.25 * 60
# The number of worker threads
MAX_WORKERS = 12
# You should set up request headers
# if you want to better evade anti-spider programs
HEADERS = {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'en-US,en;q=0.9',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
#'Host': None,
'If-Modified-Since': '0',
#'Referer': None,
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36',
}
if __name__ == "__main__": ## If we are not importing this:
# calcprice('dfk balance.txt','pricefile.csv')
#search("https://explorer.xdag.io/block/","YvcUHwI9iw2kGpXFwI9qAeUy+ni6D2+g")
#read1k()
f = open(os.path.join(sys.path[0],"addrlist.csv"), 'r+')
data = f.read()
URLS = data.split('\n')
with ThreadPoolExecutor() as executor:
if not URLS:
raise RuntimeError('Please fill in the array `URLS` to start probing!')
tasks = queue.Queue()
for url in URLS:
tasks.put_nowait(url)
def wind_up(url):
#print('wind_up(url={})'.format(url))
tasks.put(url)
i=0
while True:
url = tasks.get()
# Work
executor.submit(load_url, url)
i=i+1
#print(i)
threading.Timer(interval=INTERVAL, function=wind_up, args=(url,)).start()
'''
dt=datetime.datetime.now()
rows = ['R2eJ1N88Zsu3u74qzo+IILkrTg9RkprK']
newrows=[]
dt=datetime.datetime.today().strftime('%Y-%m-%d')
result=[]
for row in rows:
readlist=[] #G6jTFKRkFlKj67zIdOZJ4jMjuhCe6oOg BLOCK as address, fails
(prt1, readlist) = search("https://explorer.xdag.io/block/",row)
#print(row+","+prt1)
for NewItem in readlist:
if NewItem not in newrows:
newrows.append(NewItem)
#print (rows)
for row in newrows:
try:
readlist=[] #G6jTFKRkFlKj67zIdOZJ4jMjuhCe6oOg BLOCK as address, fails
(prt1, readlist) = search("https://explorer.xdag.io/block/",row)
for NewItem in readlist:
if NewItem not in rows and NewItem not in newrows:
rows.append(NewItem)
print ("final:"+NewItem)
f.write(NewItem+'\n')
except:
pass '''