-
Notifications
You must be signed in to change notification settings - Fork 4
/
get_images.py
78 lines (58 loc) · 2.16 KB
/
get_images.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
import json, requests, shutil, os, sys, getopt
def do_request(id, take, skip):
url = "https://api.fxhash.xyz/graphql/"
query = f"""{{
generativeToken(id: {id}){{
objkts(take: {take}, skip: {skip}) {{
metadata
}}
}}
}}"""
return requests.post(url, json={'query': query})
def main(argv):
GT_id = ''
folder = ''
try:
opts, args = getopt.getopt(argv, "i:f:",["id=","folder="])
except:
print("Error in arguments: get_images.py --id <GT_id> --folder <folder>")
for opt, arg in opts:
if opt in ['-i', '--id']:
GT_id = arg
elif opt in ['-f', '--folder']:
folder = arg
if not os.path.exists(folder):
os.mkdir(folder)
valid_request = True
token_count = 0
take_count = 20
while valid_request:
# Do multiple requests to get all tokens, like a pagination.
r = do_request(GT_id, take_count, token_count)
token_count += take_count
if r.status_code == 200:
binary = r.content
output = json.loads(binary)
output = output['data']['generativeToken']['objkts']
for objkt in output:
objkt_name = objkt['metadata']['name']
image_url = f"https://gateway.fxhash.xyz/ipfs/{objkt['metadata']['displayUri'][7:]}"
filename = f'{folder}/{objkt_name}.png'
if os.path.exists(filename):
print(f'{objkt_name} already downloaded')
continue
r_img = requests.get(image_url, stream = True)
if r_img.status_code == 200:
r_img.raw.decode_content = True
with open(filename,'wb') as f:
shutil.copyfileobj(r_img.raw, f)
print(f'{objkt_name} downloaded')
else:
print(f'Error downloading {objkt_name}')
valid_request = False
if len(output) < take_count:
break
else:
print(f'Error {str(r.status_code)}')
valid_request = False
main(sys.argv[1:])