forked from gogogoutham/coinmarketcap-scraper
-
Notifications
You must be signed in to change notification settings - Fork 42
/
scrape.py
84 lines (72 loc) · 2.89 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
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
""" Core scraper for coinmarketcap.com. """
import argparse
import db
import logging
import coinmarketcap
import sys
import time
import traceback
# Parse min market cap argument
parser = argparse.ArgumentParser(description='Scrape data from coinmarketcap into local database.')
parser.add_argument('min_market_cap', metavar='min_cap', type=int, nargs='?', default=0,
help='minimum market cap [usd] for currency to be scraped (default: scrape all)')
args = parser.parse_args()
# Configuration
timestamp_0 = 1367174841000
timestamp_1 = int(round(time.time() * 1000))
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(levelname)s: %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p')
# Database
database = db.Database()
def scrapeCoinList():
"""Scrape coin list."""
html = coinmarketcap.requestList('coins', 'all')
data = coinmarketcap.parseList(html, 'currencies')
return data
def scrapeTokenList():
"""Scrape token list."""
html = coinmarketcap.requestList('tokens', 'all')
data = coinmarketcap.parseList(html, 'assets')
return data
def scrapeMarketCap(slug, name, type):
"""Scrape market cap for the specified coin or token slug."""
jsonDump = coinmarketcap.requestMarketCap(slug)
result = coinmarketcap.parseMarketCap(jsonDump, slug)
database.batch_entry(result, name, type)
return result[-1]['market_cap_by_available_supply'] < args.min_market_cap
logging.info("Attempting to scrape token list...")
tokens = scrapeTokenList()
logging.info("Finished scraping token list. Starting on tokens...")
for token in tokens:
logging.info("> Starting scrape of token {0}...".format(token['slug']))
try:
if scrapeMarketCap(token['slug'], token['name'], 'token'):
logging.info("Minimum market cap reached. Stopped scraping tokens.")
break
except Exception as e:
print('-'*60)
print("Could not scrape token {0}.".format(token['slug']))
print(traceback.format_exc())
print('-'*60)
logging.info(">> Could not scrape {0}. Skipping.".format(token['slug']))
continue
logging.info("Attempting to scrape coin list...")
coins = scrapeCoinList()
logging.info("Finished scraping coin list. Starting on coins...")
for coin in coins:
logging.info("> Starting scrape of coin {0}...".format(coin['slug']))
try:
if scrapeMarketCap(coin['slug'], coin['name'], 'coin'):
logging.info("Minimum market cap reached. Stopped scraping coins.")
break
except Exception as e:
print('-'*60)
print("Could not scrape coin {0}.".format(coin['slug']))
print(traceback.format_exc())
print('-'*60)
logging.info(">> Could not scrape {0}. Skipping.".format(coin['slug']))
continue
logging.info("Finished scraping tokens and coins. All done.")
logging.info("Made {0} requests in total.".format(coinmarketcap.countRequested))