-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetdata.py
More file actions
100 lines (76 loc) · 2.63 KB
/
getdata.py
File metadata and controls
100 lines (76 loc) · 2.63 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
import os
from dotenv import load_dotenv
from urllib.parse import urljoin
import requests
import time
import json
# Load environment variables from the .env file
load_dotenv()
def get_access_token(username, password):
auth_url = "https://api.rated.network/v0/auth/token"
headers = {
"accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded",
}
data = {
"grant_type": "",
"username": username,
"password": password,
"scope": "",
"client_id": "",
"client_secret": "",
}
response = requests.post(auth_url, headers=headers, data=data)
if response.status_code == 200:
access_token = response.json().get("accessToken")
return access_token
else:
print(f"Error getting access token: {response.status_code}")
return None
def fetch_data(api_url, access_token):
headers = {
"accept": "application/json",
"X-Rated-Network": "mainnet",
"Authorization": f"Bearer {access_token}",
}
response = requests.get(api_url, headers=headers)
if response.status_code == 200:
data = response.json()
process_data(data)
# Check if there's more data
next_url = data.get("next")
# Use urljoin to construct the full URL
api_url = urljoin(api_url, next_url) if next_url else None
return api_url
else:
print(f"Error: {response.status_code}")
return None
def process_data(data):
# for item in data.get('data', []):
# print(f"Item: {item}")
# Pretty print the data to the console
print("Response:")
print(json.dumps(data, indent=2))
with open("output_data.json", "a") as json_file:
json.dump(data, json_file)
json_file.write("\n")
# Get the username and password from environment variables
username = os.getenv("USERNAME")
password = os.getenv("PASSWORD")
# Check if both username and password are available
if username is not None and password is not None:
# Get the access token
# access_token = get_access_token(username, password)
access_token = os.getenv("API_KEY")
if access_token:
# Set the initial API URL
api_url = "https://api.rated.network/v0/eth/operators?window=30d&poolType=all&idType=poolShare&size=100"
while api_url:
# Fetch data from the current URL
api_url = fetch_data(api_url, access_token)
# Wait for 0.8 seconds between requests to comply with the rate limit
time.sleep(5)
else:
print(
"Error: Missing username or password. Make sure to set them in the .env file."
)