-
Notifications
You must be signed in to change notification settings - Fork 7
/
bayc.py
61 lines (41 loc) · 1.62 KB
/
bayc.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
import requests, csv
from collections import defaultdict
from statistics import mean
PAGE_SIZE = 50
TOTAL_APES = 10000
url = "https://api.opensea.io/api/v1/assets"
querystring = {"asset_contract_address": "0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d", "order_direction": "desc", "offset": "0", "limit": str(PAGE_SIZE)}
apes = []
traits = ['Earring', 'Background', 'Fur', 'Mouth', 'Clothes', 'Hat', 'Eyes']
fieldnames = ['token_id', 'price', 'numTraits', 'maxRarity', 'avgRarity'] + traits + ['permalink']
while len(apes) < TOTAL_APES:
response = requests.request("GET", url, params=querystring).json()
for ape in response['assets']:
thisApe = {'token_id': ape['token_id']}
if ape['sell_orders']:
thisApe['price'] = float(ape['sell_orders'][0]['current_price']) / 1000000000000000000.0
else:
thisApe['price'] = None
rarity = 1
traitPercents = []
for trait in ape['traits']:
thisApe[trait['trait_type']] = trait['value']
traitPercents.append(float(trait['trait_count']) / float(10000))
avgRarity = mean(traitPercents)
traitPercents.remove(max(traitPercents))
rarity = max(traitPercents)
thisApe['numTraits'] = len(ape['traits'])
thisApe['maxRarity'] = rarity
thisApe['avgRarity'] = avgRarity
thisApe['permalink'] = ape['permalink']
for trait in traits:
if trait not in thisApe:
thisApe[trait] = None
apes.append(thisApe)
querystring['offset'] = str(int(querystring['offset']) + int(querystring['limit']))
print len(apes)
with open('apes.csv', 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for ape in apes:
writer.writerow(ape)