-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshot_members.py
92 lines (76 loc) · 2.3 KB
/
snapshot_members.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
#! Python3
"""
File name: snapshot_members.py
Author: Jonathan Snow
Date created: 12/14/2022
Python Version: 3.9.x
File Details:
Purpose: A series of functions to pull the followers of a Snapshot Space
"""
# Imports
from time import sleep, time
from modules import data, eth, db, config
from termcolor import colored
import sys
import requests
DEBUG = True
# Globals
URL = "https://hub.snapshot.org/graphql"
LIMIT = 1000
##################################################
def main():
"""
Function to pull the followers for a provided
Snapshot Space ID.
"""
print("\nFetching Snapshot members.")
# Build query and make it generic
space = "cvx.eth"
# Loop in the event that a space has more than 1,000 members
follower_data = []
offset = 0
while True:
print("Query # " + str(int((offset/1000) + 1)))
query = """
query Followers {
follows(
first: %s,
skip: %s,
where: {
space_in: ["%s"]
}
) {
follower
space {
id
}
created
}
}
"""%(LIMIT, offset, space)
# Request data from Snapshot GraphQL endpoint
response = requests.post(URL, json={'query': query})
response_data = response.json()["data"]["follows"]
# Add data to existing list
follower_data.extend(response_data)
# Check if there is more data to process
if len(response_data) != 0:
offset += LIMIT
else:
print(" Processing complete.")
break
# Pull follower addresses from GQL data
followers = []
for each in follower_data:
followers.append(each["follower"])
output = list(set(followers))
print("Processed " + str(len(output)) + " followers.")
if DEBUG: sys.exit(0)
# Dump Follower data to CSV
output_path = "files/output"
output_name = "snapshot_followers_" + space + "_" + str(int(time())) + ".csv"
data.save(output, output_path, output_name, ["address"])
##################################################
# Runtime Entry Point
if __name__ == "__main__":
main()