forked from DOSPunksDAO/dump-opensea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump-opensea.py
526 lines (441 loc) · 19.6 KB
/
dump-opensea.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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# Import necessary libraries
import requests, argparse, json, sys, time, traceback
import math
from colorama import Fore, Back, Style, init as coloramaInit
from alive_progress import alive_bar as progressBar
from pathlib import Path
# Set constants
MORALIS_READ_TIMEOUT = 2
MORALIS_PAUSE_BETWEEN_REQUESTS = 0.7
# Initialize main vars
offset = 0
num = 0
totalOwned = 0
totalNfts = 0
owners = {}
filteredOwners = {}
userWasPrompted = False
exception_traces = False
# Header
def showHeader():
print(
f"""{Fore.LIGHTBLUE_EX}
██████████ █████ █████ ██████ ██████ ███████████
░░███░░░░███ ░░███ ░░███ ░░██████ ██████ ░░███░░░░░███
░███ ░░███ ░███ ░███ ░███░█████░███ ░███ ░███
░███ ░███ ░███ ░███ ░███░░███ ░███ ░██████████
░███ ░███ ░███ ░███ ░███ ░░░ ░███ ░███░░░░░░
░███ ███ ░███ ░███ ░███ ░███ ░███
██████████ ░░████████ █████ █████ █████
░░░░░░░░░░ ░░░░░░░░ ░░░░░ ░░░░░ ░░░░░
███████ █████████
███░░░░░███ ███░░░░░███
███ ░░███ ████████ ██████ ████████ ░███ ░░░ ██████ ██████
░███ ░███░░███░░███ ███░░███░░███░░███ ░░█████████ ███░░███ ░░░░░███
░███ ░███ ░███ ░███░███████ ░███ ░███ ░░░░░░░░███░███████ ███████
░░███ ███ ░███ ░███░███░░░ ░███ ░███ ███ ░███░███░░░ ███░░███
░░░███████░ ░███████ ░░██████ ████ █████░░█████████ ░░██████ ░░████████
░░░░░░░ ░███░░░ ░░░░░░ ░░░░ ░░░░░ ░░░░░░░░░ ░░░░░░ ░░░░░░░░
░███
█████
░░░░░ - BROUGHT TO YOU BY DOS PUNKS DAO
{Fore.WHITE}Created by: {Fore.LIGHTRED_EX}GBE#0001{Fore.WHITE} for DOS PUNKS DAO
{Fore.WHITE}Reimagined by: {Fore.LIGHTRED_EX}DENVERS.ETH{Fore.WHITE} - Error context, support for JSON imports, parser rearchitecture, refactoring,
... support for OpenSea API keys.
"""
)
# Styling helper
def styleArgVal(yellowText):
return Fore.YELLOW + str(yellowText) + Fore.WHITE
# Input prompt with styling
def inputStyled(argumentName):
print(f"{Fore.YELLOW}[] {argumentName}: {Style.RESET_ALL}", end="")
return input().strip()
def _keys_to_string(_dict) -> str:
"""Return a comma-separated list of keys for a given object.
Should work with anything that can be used as a dict key (e.g. string, number, etc.)"""
return ",".join([str(key) for key in _dict.keys()])
# TODO: improve docstring
# TODO: move to own script/library
def chooseFromDict(
_dict,
message_prompt,
value_prompt_attr,
message_invalid=f"{Fore.RED}\nSorry, that's not a valid choice. Please try again.\n{Style.RESET_ALL}",
display_options=True,
convert_ints=True,
retry=False,
):
"""Prompt user to select an option from a given dictionaries.
Returns the value of the corrosponding key choice.
- value_prompt_attr: an attribute each value in the dictionary has which is
- Used as context for the value when displaying choices
"""
# Collect options based on dict keys
options = {i: a for (i, a) in enumerate(_dict)}
# Generate prompt message
prompt = f"{message_prompt} [{_keys_to_string(options)}]"
while True:
# Display options to user
if display_options:
for (i, a) in options.items():
option_context = getattr(_dict[i], value_prompt_attr)
print(f" {i}: {option_context}")
print()
# Prompt for choice
sourceChoice = inputStyled(prompt)
# Convert integers if applicable
if convert_ints:
if sourceChoice.isdigit():
sourceChoice = int(sourceChoice)
# If choice is valid, return corrosponding dict value
if sourceChoice in options.keys():
return options[sourceChoice]
else:
print(message_invalid)
if not retry:
raise ValueError("Invalid choice")
# Init with flags
def flagInit():
global userWasPrompted
# Parse Flags
parser = argparse.ArgumentParser()
requiredParameters = parser.add_argument_group("required")
tokenIdSource = parser.add_argument_group(
"sources"
).add_mutually_exclusive_group()
advancedParameters = parser.add_argument_group("advanced")
openseaParameters = parser.add_argument_group("opensea")
actions = [
# Required arguments
requiredParameters.add_argument(
"-c", "--contract", help="Contract Address", default="==PROMPT=="
),
requiredParameters.add_argument(
"-k", "--apikey", help="API Key (Moralis)", default="==PROMPT=="
),
# Required mutex arguments
tokenIdSource.add_argument("-s", "--slug", help="Slug Name (OpenSea)"),
tokenIdSource.add_argument(
"-t", "--token-id-json", help="Token ID JSON"
),
# Optional arguments
parser.add_argument(
"-f",
"--filter",
help="Filter Owners (Minimum Tokens Held)",
type=int,
),
advancedParameters.add_argument(
"-e",
"--exception-traces",
help="Show error stacktraces",
action="store_true",
),
# OpenSea specific
openseaParameters.add_argument(
"-o", "--opensea-api-key", help="API Key (OpenSea)"
),
]
try:
args = parser.parse_args()
# Prompts begin
# Create dictionary of dest-indexed actions
destActions = {
action.dest: action
for action in actions
if hasattr(action, "dest")
}
# Ensure source for token IDs provided
if not (args.slug or args.token_id_json):
# Prompt user to select a source
sourceAction = chooseFromDict(
tokenIdSource._group_actions,
"Which token_id source would you like to use?",
"help",
)
# Set source-specific argument to be prompted
setattr(args, sourceAction.dest, "==PROMPT-STRICT==")
statusMsg("SELECTED TOKEN ID SOURCE.")
# Note that user was prompted
userWasPrompted = True
# Ensure OpenSea args are set (if necessary)
if args.slug:
if not args.opensea_api_key:
setattr(
args,
destActions["opensea_api_key"].dest,
"==PROMPT-STRICT==",
)
# Prompt user for values for all arguments set to "==PROMPT=="
for a in actions:
strict = getattr(args, a.dest) is "==PROMPT-STRICT=="
if getattr(args, a.dest) in ("==PROMPT==", "==PROMPT-STRICT=="):
values = inputStyled(a.help)
setattr(args, a.dest, values)
userWasPrompted = True
# If prompt-strict, raise an error if the provided value is falsy
if strict:
if bool(getattr(args, a.dest)) is False:
raise Exception(f"{a.dest} mustn't be falsy")
except KeyboardInterrupt:
raise Exception("Quitting...")
except Exception as err:
raise Exception(f"An error occuring during parsing: {err}")
# Get flags
(
contract,
apiKey,
slug,
tokenIdJson,
tokenFilter,
excepTraces,
openseaApiKey,
) = vars(args).values()
# Enable exception traces
if excepTraces is True:
global exception_traces
exception_traces = True
# Check either slug or tokenIdJson are set
if not (slug or tokenIdJson):
raise argparse.ArgumentError(
None, "Must provide either Slug name or Token ID JSON"
)
# Check tokenIdJson is a valid filepath
if tokenIdJson is not None:
tidjPath = Path(tokenIdJson)
if tidjPath.is_file() is False:
raise FileNotFoundError(
f"Failed to find token ID JSON file specified: {tidjPath}"
)
if userWasPrompted is True:
statusMsg("ALL REQUIRED PARAMETERS COLLECTED.")
# TODO: add any missing flags
print(
f"[] Slug: {styleArgVal(slug)}\n"
+ f"[] Token ID JSON: {styleArgVal(tokenIdJson)}\n"
+ f"[] Contract Address: {styleArgVal(contract)}\n"
+ f"[] API Key (Moralis): {styleArgVal(apiKey)}\n"
+ f"[] API Key (OpenSea): {styleArgVal(openseaApiKey)}\n"
+ f"[] Filter: {styleArgVal(tokenFilter)}",
)
# TODO: add any missing flags
return slug, tokenIdJson, contract, apiKey, tokenFilter, openseaApiKey
# Fatal Error --> Exit
def fatalError(excep):
global exception_traces
if exception_traces:
print(traceback.format_exc())
print(
f"\n{Fore.BLACK}{Back.YELLOW} ⚠︎ FATAL ERROR ⚠︎ {Style.RESET_ALL}"
+ f"{Fore.YELLOW}{Back.BLACK} {excep} {Style.RESET_ALL}"
)
print(f"\n{Fore.RED}An error has occurred. Please try again.\n")
sys.exit()
class API:
# TODO: add typehints to funcsig
def get_asset_metadata(contract_address, token_id, moralis_api_key):
"""Retrieve asset metadata from Moralis API."""
return requests.get(
f"https://deep-index.moralis.io/api/v2/nft/{contract_address}/{token_id}/owners?chain=eth&format=decimal",
headers={"X-API-Key": moralis_api_key},
timeout=MORALIS_READ_TIMEOUT,
)
# TODO: add typehints to funcsig
def get_opensea_collection_assets(
collection_slug, offset, opensea_api_key=None
):
"""Retrieve assets from an OpenSea collection using OpenSea API.
Response contains an array of token IDs that can be used with other APIs.
"""
headers = {}
if opensea_api_key is not None:
headers["X-API-Key"] = opensea_api_key
return requests.get(
f"https://api.opensea.io/api/v1/assets?order_direction=desc&offset={offset}&limit=50&collection={collection_slug}",
headers=headers,
)
# TODO: add typehints to funcsig
def get_opensea_collection_stats(collection_slug, opensea_api_key):
"""Get stats on an OpenSea collection with the collection's slug."""
headers = {}
if opensea_api_key is not None:
headers["X-API-Key"] = opensea_api_key
response = requests.get(
f"https://api.opensea.io/api/v1/collection/{collection_slug}",
headers=headers,
)
if not response.ok:
raise RuntimeError(
f"Failed to get OpenSea collection stats: {response.status_code} ({response.reason})"
)
return response.json()["collection"]["stats"]
def _getOwners(tokenIds, contract, apiKey, bar):
for tokenId in tokenIds:
global totalNfts, totalOwned, owners, num
totalNfts += 1
while True:
try:
time.sleep(MORALIS_PAUSE_BETWEEN_REQUESTS)
response = API.get_asset_metadata(contract, tokenId, apiKey)
if response.ok is not True:
_msgBase = f"Failed to get asset metadata from Moralis: {response.status_code} ({response.reason})"
if response.status_code == 401:
raise RuntimeError(
f"{_msgBase}: {Fore.YELLOW}Double-check your Moralis API key is valid{Style.RESET_ALL}"
)
if response.status_code == 429:
raise RuntimeError(
f"{_msgBase}: {Fore.YELLOW}Getting rate-limited{Style.RESET_ALL}"
)
raise RuntimeError(_msgBase)
web3response = response.json()
if not "total" in web3response:
raise RuntimeError("total is missing!")
break
# TODO: Add debug flag that re-enables error printing in this section
except RuntimeError as err:
# print(err)
# print("might be getting throttled, trying again in 1.5 seconds...")
time.sleep(1.5)
except KeyboardInterrupt:
# print("Cancelled by user...")
raise KeyboardInterrupt
except requests.exceptions.ReadTimeout:
continue
# print(
# f"{Fore.YELLOW}Timed-out waiting for server response...{Style.RESET_ALL}"
# )
except Exception as err:
print(response.status_code, response.reason, type(err))
print("Unexpected error, trying again in 1 second...")
print(f"Error: ${err}")
time.sleep(1)
totalOwned += web3response["total"]
for r in web3response["result"]:
if r["owner_of"] in owners:
owners[r["owner_of"]] += 1
else:
owners[r["owner_of"]] = 1
num += 1
bar()
# Retrieving owners from API's
def getOwners(contract, tokenIds, tokenFilter, apiKey):
# showCheckingHolders()
with progressBar(len(tokenIds), bar="filling") as bar:
_getOwners(tokenIds, contract, apiKey, bar)
try:
if tokenFilter not in (None, 0):
print(
f"\n\n{Fore.WHITE}[+] Filtered by holders with: {Fore.GREEN}{tokenFilter} or + Tokens{Fore.WHITE}"
)
for _o in owners:
if int(owners[_o]) >= int(tokenFilter):
filteredOwners[_o] = owners[_o]
owners = filteredOwners
except Exception as err:
fatalError(err)
# Export to JSON
def exportJSON(slug):
global totalNfts, totalOwned, owners
# TODO: Add timestamp to snapshot file
outputFile = open(f"./snapshots/{slug}-owners.json", "w")
json.dump(owners, outputFile)
time.sleep(1.5)
print(
f"""\n{Fore.YELLOW}
+------------------------------------------------------------+
| SNAPSHOT HAS BEEN TAKEN |
+------------------------------------------------------------+\n
{Fore.GREEN} [+] Total NFTS: {Fore.WHITE}{totalNfts}{Fore.YELLOW}
{Fore.GREEN} [+] Total Owned: {Fore.WHITE}{totalOwned}{Fore.YELLOW}
{Fore.GREEN} [+] Total Owners: {Fore.WHITE}{len(owners)}{Fore.YELLOW}
{Fore.GREEN} [+] Exported File: {Fore.WHITE}./snapshots/{slug}-owners.json{Fore.YELLOW}\n\n"""
)
class CollectTokenIds:
def from_opensea(slug, contract, opensea_api_key):
# Get size of collection
collection_stats = API.get_opensea_collection_stats(
slug, opensea_api_key
)
# NOTE: Supersedes totalMinted argument
collection_size = collection_stats["total_supply"]
# Figure out how many requests we need to make
assets_per_request = 50
requests_left = math.ceil(collection_size / assets_per_request)
collected = 0
token_ids = []
with progressBar(requests_left, bar="filling") as bar:
for r in range(requests_left):
# Calculate request's offset
offset = r * assets_per_request
# Collect OpenSea collection assets
response = API.get_opensea_collection_assets(
slug, offset, opensea_api_key
)
if response.ok is not True:
raise RuntimeError(
f"Failed to collect assets from OpenSea API: {response.status_code} ({response.reason})"
)
# Parse OpenSea response
try:
# NOTE: .assets[n].owner.address is always 0x000...0 (guess: related to frozen/non-frozen metadata?)
responseJson = response.json()
except Exception as err:
raise RuntimeError(
f"Failed to parse OpenSea response: {err}"
)
# Extract token_ids from response, append to total collection
token_ids.extend(
[asset["token_id"] for asset in responseJson["assets"]]
)
# Increment collected counter
collected += assets_per_request
if collected > collection_size:
collected = collection_size
# Announce progress
# print(f"Request #{r} - {collected} assets collected")
bar()
return token_ids
def from_json(tokenIdJson):
with open(tokenIdJson) as f:
return json.load(f)["tokenIds"]
def statusMsg(msg):
"""Prints status message with common formatting."""
print(f"\n{Fore.YELLOW}{Back.BLACK} {msg} {Style.RESET_ALL}\n")
# Main handle
def main():
coloramaInit()
showHeader()
try:
# Gather arguments
(
slug,
tokenIdJson,
contract,
apiKey,
tokenFilter,
openseaApiKey,
) = flagInit()
statusMsg("COLLECTING TOKEN IDS.")
# Gather token_ids
if slug:
tokenIds = CollectTokenIds.from_opensea(
slug, contract, openseaApiKey
)
elif tokenIdJson:
tokenIds = CollectTokenIds.from_json(tokenIdJson)
else:
raise Exception(
"No valid token_ids sources to selected! - Please open a GitHub issue if you see this"
)
statusMsg("COLLECTING OWNERS.")
# Get owners
getOwners(contract, tokenIds, tokenFilter, apiKey)
statusMsg("EXPORTING RESULTS.")
# Export results
exportJSON(slug)
except Exception as err:
fatalError(err)
# Start
main()