-
Notifications
You must be signed in to change notification settings - Fork 2
/
1_query_wallet_points.py
108 lines (78 loc) · 3.06 KB
/
1_query_wallet_points.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
# %%
import requests
import time
import os
import csv
import json
from dotenv import load_dotenv
with open('./configs/configs.json', 'r') as file:
configs = json.load(file)
limit = configs['Address_Limit'] + 1
cooldown = configs['Cooldown']
timestamp = int(time.time())
print(f"::set-output name=timestamp::{timestamp}")
def sort_csv_by_numeric_column(input_file, output_file, sort_column):
with open(input_file, 'r', newline='') as infile:
reader = csv.DictReader(infile)
data = list(reader)
for row in data:
row[sort_column] = float(row[sort_column])
data.sort(key=lambda x: x[sort_column], reverse=True)
with open(output_file, 'w', newline='') as outfile:
writer = csv.DictWriter(outfile, fieldnames=reader.fieldnames)
writer.writeheader()
writer.writerows(data)
def sort_csv(input_file, output_file, sort_key):
with open(input_file, 'r', newline='') as infile:
reader = csv.DictReader(infile)
data = list(reader)
data.sort(key=lambda x: x[sort_key], reverse=True)
with open(output_file, 'w', newline='') as outfile:
writer = csv.DictWriter(outfile, fieldnames=reader.fieldnames)
writer.writeheader()
writer.writerows(data)
def csv_to_json(csv_file_path, json_file_path):
data = []
with open(csv_file_path, newline='', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
data.append(row)
with open(json_file_path, 'w', encoding='utf-8') as jsonfile:
json.dump(data, jsonfile, ensure_ascii=False)
def query_wallet_points(address):
url = f"https://kx58j6x5me.execute-api.us-east-1.amazonaws.com/scroll/wallet-points?walletAddress={address}"
response = requests.get(url)
return response.json()
def main():
result_file_path = f'./historical_data/results-{timestamp}.csv'
with open(result_file_path, 'a') as file:
file.write('address,point,timestamp\n')
with open('./assets/Addresses.csv', 'r') as file:
addresses = file.readlines()
index = 1
for address in addresses[1:limit + 1]:
address = address.strip()
if index%100 == 0:
print(f'Milestone reached at {int(time.time())}! Index: {index}')
if address:
try:
result = query_wallet_points(address)
result = result[0]
except Exception as e:
print(f"Error querying {address}: {e}")
time.sleep(cooldown) # Sleep to avoid hitting rate limits
with open(result_file_path, 'a') as file:
points = result.get('points')
ts = result.get('timestamp')
line = f'{address},{points},{ts}\n'
file.write(line)
index += 1
sort_csv_by_numeric_column(result_file_path, result_file_path, 'point')
csv_to_json(result_file_path, './assets/Results.json')
print(f"Copied {result_file_path} to ./assets/Results.json")
if __name__ == "__main__":
try:
main()
except Exception as e:
print(f"Error occurred: {e}")
# %%