In [1]:
import datetime as dt
from funcy import walk_values, join, flatten, first, rest, rpartial

import pandas as pd
import numpy as np

import plotly.plotly as py
import plotly.graph_objs as go
import cufflinks as cf

In [ ]:


In [2]:
# helpers
from toolz import keyfilter

def keep(d, whitelist):
    return keyfilter(lambda k: k in whitelist, d)

def omit(d, blacklist):
    return keyfilter(lambda k: k not in blacklist, d)

In [ ]:


In [3]:
import requests

def historic_data(urls=None):
    return list(map(lambda x: requests.get(x).json(), urls))

def merge_historic_data(historic_data):
    """ Simplify and flatten all historic data into a single list of events."""
    data = []
    for hist in historic_data:
        data.append([simplify_fragment(x) for x in hist['history'].values()])
    return list(flatten(data))

def simplify_fragment(obj):
    """ Simplify and flatten individual fragment."""
    # clean up the mess
    def replace_values(val):
        if type(val) == dict:
            return walk_values(replace_values, val)
        if val == "?" or val == 'None':
            return 0
        return val
    obj = walk_values(replace_values, obj)
    
    return {
        'symbol': obj['symbol'],
        'category': obj['category'],
        'supply': obj['availableSupply'],
        'position': int(obj['position']),
        'cap_usd': round(float(obj['marketCap']['usd'])),
        'cap_btc': round(float(obj['marketCap']['btc'])),
        'volume_btc': round(float(obj['volume24']['btc'])),
        'price_usd': float(obj['price']['usd']),
        'price_btc': float(obj['price']['btc']),
        'timestamp': dt.datetime.fromtimestamp(obj['timestamp'])

    }


chart_filter = rpartial(
    keep,
    ['symbol', 'timestamp', 'price_usd', 'price_btc', 'cap_usd', 'volume_btc', 'supply'],
)

def simplify_hist_data(historic_data):
    return [keep(x, ['symbol', 'timestamp', 'price_usd']) for x in historic_data]

In [ ]:


In [4]:
steem_urls = [
    'http://coinmarketcap.northpole.ro/api/v6/history/STEEM_2016.json',
    'http://coinmarketcap.northpole.ro/api/v6/history/STEEM_2017.json',
]
from toolz import thread_last

steem_chart = thread_last(
    historic_data(steem_urls),
    merge_historic_data,
    (map, chart_filter),
    list,
)

In [ ]:


In [5]:
df = pd.DataFrame(steem_chart)

In [6]:
df.sort_values('timestamp').tail()


Out[6]:
cap_usd price_btc price_usd supply symbol timestamp volume_btc
416 530551374 0.000768 2.26676 234057145 STEEM 2017-06-12 00:00:02 2742
417 492335702 0.000796 2.10289 234123374 STEEM 2017-06-13 00:00:02 3129
418 546869965 0.000860 2.33504 234201540 STEEM 2017-06-14 00:00:02 3155
419 467210043 0.000817 1.99433 234269175 STEEM 2017-06-15 00:00:02 2025
420 464083335 0.000826 1.98022 234359483 STEEM 2017-06-16 00:00:02 2504

In [7]:
# cf.go_offline()

In [8]:
df.sort_values('timestamp', inplace=True)
df.set_index('timestamp', inplace=True)

In [9]:
df[['cap_usd', 'supply']].iplot(title='USD Market Cap and STEEM Supply',
                 colors=['blue', 'orange'],
                 theme='white',
                 fill=True,
                 filename='steem-supply-cap')


/home/user/anaconda3/lib/python3.6/site-packages/cufflinks/plotlytools.py:156: FutureWarning:

pandas.tslib is deprecated and will be removed in a future version.
You can access Timestamp as pandas.Timestamp

Out[9]:

In [ ]:


In [ ]: