-
Notifications
You must be signed in to change notification settings - Fork 1
/
scrape.py
49 lines (41 loc) · 1.49 KB
/
scrape.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
import json
import time
import os
import urllib.request
import requests
import shutil
# Time to sleep to avoid rate limit error
rate_limit_time = 3
# Local folder Structures
folder = "/icons/"
coin = []
name = []
url = "https://www.cryptocompare.com/api/data/coinlist/"
# Get initial coin list via API
data = json.loads(urllib.request.urlopen(urllib.request.Request(url)).read().decode('utf-8'))
# Loop through data to create list of coin names/coin image request url
for key in data['Data']:
if 'ImageUrl' in data['Data'][key]:
coin.append(url+ data['Data'][key]['ImageUrl'])
name.append( data['Data'][key]['Name'])
# Check if coin image exists already
def check(icon_path):
if os.path.isfile(icon_path):
return True
else:
return False
total = str(len(coin))
# Loop through coin list, make request if they exist with rate limit in between
for index in range(len(coin)):
icon_path = os.getcwd()+folder+ name[index] + ".png"
if (check(icon_path)):
print("["+ str(index+1) + "/"+ total +"]: File for " + name[index] +" exists already")
else:
print(coin[index])
print("Sleeping " + str(rate_limit_time) +" seconds to avoid rate limit..")
time.sleep(rate_limit_time)
response = requests.get(coin[index], stream=True)
with open(icon_path,'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
print("["+ str(index+1) + "/"+ total +"]: Saved: " + icon_path)
del response