def get_cmc_targets(self, max_days): cmc = Pymarketcap() recent = cmc.recently() targets = [] for r in recent: if r['added'] == 'Today': days_old = 0 else: days_old = int(r['added'].replace(' days ago', '')) if days_old < max_days: targets.append({ 'symbol': r['symbol'], 'days_old': days_old, 'volume_24h': r['volume_24h'], 'market_cap': r['market_cap'], }) return targets
def coinMarketCapData(): from pymarketcap import Pymarketcap coinmarketcap = Pymarketcap() symbols = coinmarketcap.symbols # ###### Set initial Date/Time.. Will be setting throughout # In[45]: #Pulls date and time information moment = time.strftime("%Y-%b-%d__%H_%M_%S", time.localtime()) today = pd.Timestamp("today").strftime("%m/%d/%Y") hh = pd.Timestamp("today").strftime("%H") mm = pd.Timestamp("today").strftime("%M") # ###### Coinmarketcap statistics - up to date 1/14/2018 # In[47]: try: stats = coinmarketcap.stats() stats = pd.DataFrame(stats, index=[0]) stats['Date'] = today stats['Hour'] = hh stats['Minute'] = mm stats['Now'] = pd.Timestamp("today") stats.to_csv('Coinmarketcap/boom/stats/stats_' + moment + '.csv', sep=',') print("Look at all those stats 0.o") except: print("*NO STATS GATHERED*") # ###### Coin Data - Updated 1/14/2018 # In[49]: try: from pymarketcap import Pymarketcap coinmarketcap = Pymarketcap() ticker = coinmarketcap.ticker(limit=1500, convert="BTC") ticker = pd.DataFrame(ticker) ticker['Date'] = today ticker['Hour'] = hh ticker['Minute'] = mm ticker['Now'] = pd.Timestamp("today") ticker.to_csv('Coinmarketcap/boom/coins/coins_' + moment + '.csv', sep=',') print("Chaaaa-CHING! The coin data is in") except: print("*NO COINS GATHERED* *YOU ARE LOSING OUT ON BAGS*") # ###### Coin exchange info for each token - Updated 1/14/2018 # In[ ]: for coin2 in symbols: try: from pymarketcap import Pymarketcap coinmarketcap = Pymarketcap() markets = coinmarketcap.markets(coin2) markets = pd.DataFrame(markets['markets']) markets['Date'] = today markets['Hour'] = hh markets['Minute'] = mm markets['Now'] = pd.Timestamp("today") markets.to_csv('Coinmarketcap/boom/markets/markets_' + coin2 + '_' + moment + '.csv', sep=',') except: print("No market data was captured for ", coin2) pass print("I hear the exchange trades from here.. exchange data collected :)") # ###### Gainers and Losers (1h, 24h, 7d) - Updated 1/14/2018 # ###### ******Currently no 7d Gainers being captured****** # ###### New coins - Updated 1/14/2018 # In[102]: #Set date and time today = pd.Timestamp("today").strftime("%m/%d/%Y") hh = pd.Timestamp("today").strftime("%H") mm = pd.Timestamp("today").strftime("%M") moment = time.strftime("%Y-%b-%d__%H_%M_%S", time.localtime()) data = coinmarketcap.recently() data = pd.DataFrame(data) data.head() data['Date'] = today data['Hour'] = hh data['Minute'] = mm data['days_ago'] = data['added'].str.extract('(\d.)', expand=True) data['Now'] = pd.Timestamp("today") data['days_ago'] = data['days_ago'].apply(pd.to_numeric) data['Date_Added'] = data['Now'] - pd.to_timedelta(data['days_ago'], unit='d') data['Date_Added'] = data['Date_Added'].dt.date #Reimport due to naming issues from pymarketcap import Pymarketcap coinmarketcap = Pymarketcap() #Get Market date for new coins for coin2 in data["symbol"]: try: markets = coinmarketcap.markets(coin2) markets = pd.DataFrame(markets['markets']) #add 'Today' 'Hour' and 'minutes' column markets['Date'] = today markets['Hour'] = hh markets['Minute'] = mm markets['Now'] = pd.Timestamp("today") #Save CSV markets.to_csv('Coinmarketcap/boom/markets/new/markerts_' + coin2 + '_' + moment + '.csv', sep=',') print("Market info for new coin", coin2, " captured!") except: print("************ERROR COIN", coin2, "DATA NOT CAPUTRED*************") pass ###Upload datafile data.to_csv('Coinmarketcap/boom/new/new_' + moment + '.csv', sep=',') print("Gotta catch all the new coins!")
class TestScraperCoinmarketcap(unittest.TestCase): """ Tests for Coinmarketcap Api commands. These will fail in the absence of an internet connection or if Coinmarketcap API goes down. """ def setUp(self): self.coinmarketcap = Pymarketcap() def test_endpoints(self): from requests import get endpoints = [ 'currencies/%s/' % config.COIN_NAME, 'gainers-losers/', 'currencies/%s/historical-data/'\ % config.COIN_NAME, 'new', 'exchanges/%s/' % config.EXCHANGE, 'exchanges/volume/24-hour/all/' ] base_url = self.coinmarketcap.web_url for e in endpoints: _status_code = get(base_url + e).status_code self.assertEqual(_status_code, 200) def test_markets(self): actual = self.coinmarketcap.markets(config.COIN) value_types = { 'price_usd': Decimal, '24h_volume_usd': int, 'percent_volume': Decimal, 'pair': str, 'exchange': str } self.assertIs(type(actual), list) self.assertIs(len(actual) > 0, True) for source in actual: self.assertIs(type(source), dict) for key, value in source.items(): self.assertIs(type(value), value_types[key]) def test_ranks(self): temps = ['1h', '24h', '7d'] queries = ['gainers', 'losers'] value_types = { 'percent_change': Decimal, '24h_volume_usd': int, 'symbol': str, 'price_usd': Decimal, 'name': str } actual = self.coinmarketcap.ranks() self.assertIs(type(actual), dict) for q, temp in actual.items(): self.assertIn(q, queries) self.assertIs(type(temp), dict) for t, data in temp.items(): self.assertIn(t, temps) self.assertIs(type(data), list) self.assertIs(len(data) > 0, True) for d in data: self.assertIs(type(d), dict) for key, value in d.items(): self.assertIs(type(value), value_types[key]) # Test invalid argument with self.assertRaises(AttributeError): self.coinmarketcap.ranks('8d') def test_historical(self): from datetime import datetime value_types = { 'close': Decimal, 'low': Decimal, 'usd_volume': int, 'open': Decimal, 'usd_market_cap': int, 'high': Decimal, 'date': datetime } actual = self.coinmarketcap.historical(config.COIN, datetime(2017, 9, 30), datetime(2017, 10, 10)) self.assertIs(type(actual), list) for tick in actual: self.assertIs(type(tick), dict) for key, value in tick.items(): self.assertIs(type(value), value_types[key]) def test_recently(self): actual = self.coinmarketcap.recently() value_types = { 'price_usd': Decimal, 'mineable': bool, 'symbol': str, 'usd_market_cap': [str, int], 'circulating_supply': [str, int], 'volume_24h_usd': [str, int], 'days_ago': [str, int], 'name': str } self.assertIs(type(actual), list) for c in actual: self.assertIs(type(c), dict) for key, value in c.items(): if type(value_types[key]) is list: self.assertIn(type(value), value_types[key]) else: self.assertIs(type(value), value_types[key]) def test_exchange(self): actual = self.coinmarketcap.exchange(config.EXCHANGE) value_types = { 'market': str, 'price_usd': Decimal, 'rank': int, 'volume_24h_usd': int, 'name': str, 'perc_volume': Decimal } self.assertIs(type(actual), list) for market in actual: self.assertIs(type(market), dict) for key, value in market.items(): self.assertIs(type(value), value_types[key]) def test_exchanges(self): actual = self.coinmarketcap.exchanges() value_types = { 'market': str, 'price_usd': Decimal, 'rank': int, 'volume_24h_usd': int, 'name': str, 'perc_volume': Decimal, 'perc_change': Decimal } self.assertIs(type(actual), list) for exch in actual: self.assertIs(type(exch), dict) for key, value in exch.items(): if key in ('rank', 'volume_usd'): self.assertIs(type(value), int) elif key == 'name': self.assertIs(type(value), str) elif key == 'markets': self.assertIs(type(value), list) for m in value: self.assertIs(type(m), dict) for _key, _value in m.items(): self.assertIs(type(_value), value_types[_key]) def test_exchange_names(self): actual = self.coinmarketcap.exchange_names self.assertIs(type(actual), list)
class TestScraperCoinmarketcap(unittest.TestCase): """ Tests for Coinmarketcap Api commands. These will fail in the absence of an internet connection or if coinmarketcap.com goes down. """ def __init__(self, *args, **kwargs): super(TestScraperCoinmarketcap, self).__init__(*args, **kwargs) self.coinmarketcap = Pymarketcap() self.config = ConfigTest() def tearDown(self): # Prevent to many requests error in tests time.sleep(.25) def test_markets(self): actual = self.coinmarketcap.markets(self.config.COIN) value_types = { 'price_usd': Decimal, '24h_volume_usd': int, 'percent_volume': Decimal, 'pair': str, 'exchange': str, 'updated': bool } self.assertIs(type(actual), list) self.assertIs(len(actual) > 0, True) for source in actual: self.assertIs(type(source), dict) for key, value in source.items(): self.assertIs(type(value), value_types[key]) def test_ranks(self): temps = ['1h', '24h', '7d'] queries = ['gainers', 'losers'] value_types = { 'percent_change': Decimal, '24h_volume_usd': int, 'symbol': str, 'price_usd': Decimal, 'name': str } actual = self.coinmarketcap.ranks() self.assertIs(type(actual), dict) for q, temp in actual.items(): self.assertIn(q, queries) self.assertIs(type(temp), dict) for t, data in temp.items(): self.assertIn(t, temps) self.assertIs(type(data), list) self.assertIs(len(data) > 0, True) for d in data: self.assertIs(type(d), dict) for key, value in d.items(): self.assertIs(type(value), value_types[key]) # Test invalid argument with self.assertRaises(AttributeError): self.coinmarketcap.ranks('8d') def test_historical(self): from datetime import datetime value_types = { 'close': Decimal, 'low': Decimal, 'usd_volume': int, 'open': Decimal, 'usd_market_cap': int, 'high': Decimal, 'date': datetime } actual = self.coinmarketcap.historical(self.config.COIN, datetime(2017, 9, 30), datetime(2017, 10, 10)) self.assertIs(type(actual), list) for tick in actual: self.assertIs(type(tick), dict) for key, value in tick.items(): self.assertIs(type(value), value_types[key]) def test_recently(self): actual = self.coinmarketcap.recently() value_types = { 'price_usd': [Decimal, str], 'mineable': bool, 'symbol': str, 'usd_market_cap': [str, int], 'circulating_supply': [str, int], 'volume_24h_usd': [str, int], 'days_ago': [str, int], 'name': str, } self.assertIs(type(actual), list) for c in actual: self.assertIs(type(c), dict) for key, value in c.items(): if type(value_types[key]) is list: self.assertIn(type(value), value_types[key]) else: self.assertIs(type(value), value_types[key]) def test_exchange(self): actual = self.coinmarketcap.exchange(self.config.EXCHANGE) value_types = { 'market': str, 'price_usd': Decimal, 'rank': int, 'volume_24h_usd': int, 'name': str, 'perc_volume': Decimal } self.assertIs(type(actual), list) for market in actual: self.assertIs(type(market), dict) for key, value in market.items(): self.assertIs(type(value), value_types[key]) def test_exchanges(self): actual = self.coinmarketcap.exchanges() value_types = { 'market': str, 'price_usd': Decimal, 'rank': int, 'volume_24h_usd': int, 'name': str, 'perc_volume': Decimal, 'perc_change': Decimal } self.assertIs(type(actual), list) for exch in actual: self.assertIs(type(exch), dict) for key, value in exch.items(): if key in ('rank', 'volume_usd'): self.assertIs(type(value), int) elif key == 'name': self.assertIs(type(value), str) elif key == 'markets': self.assertIs(type(value), list) for m in value: self.assertIs(type(m), dict) for _key, _value in m.items(): self.assertIs(type(_value), value_types[_key]) def test_exchange_names(self): actual = self.coinmarketcap.exchange_names self.assertIs(type(actual), list) def _assert_graphs_data_structure(self, data): self.assertIs(type(data), dict) for key, value in data.items(): self.assertIs(type(value), list) for timestamp in value: self.assertIs(type(timestamp), list) for _value in timestamp: self.assertIn(type(_value), (float, int)) def test_graphs_currency(self): actual = self.coinmarketcap.graphs.currency(self.config.COIN) self._assert_graphs_data_structure(actual) def test_graphs_global_cap(self): actual = self.coinmarketcap.graphs.global_cap(bitcoin=True) self._assert_graphs_data_structure(actual) def test_graphs_dominance(self): actual = self.coinmarketcap.graphs.dominance() self._assert_graphs_data_structure(actual) def test_download_logo(self): filename = self.coinmarketcap.download_logo(self.config.COIN) self.assertNotEqual(filename, None) if filename: os.remove(filename)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from pymarketcap import Pymarketcap pym = Pymarketcap() currencies = pym.recently() def assert_any_field_in_response(field, readable_field): found = False for curr in currencies: value = curr[field] if isinstance(value, (int, float)): found = True elif isinstance(value, str): if value != "": found = True elif value is None: continue else: msg = "Case not anticipated. 'type(value) == %r'" raise NotImplementedError(msg % type(value)) try: assert found == True except AssertionError as err: print("Any %s found searching all currencies." % readable_field \ + "Check 'processer:recently()' function.") raise err