import pandas as pd
import urllib
import json
import matplotlib as mpl
mpl.use('agg')
import seaborn as sns; sns.set()
%matplotlib inline

def read_quandl(url):
    d = json.loads(urllib.urlopen(url).read())
    df = pd.DataFrame(d['data'], columns=d['column_names']).set_index('Date').sort()
    return df

"""
Explanation: <img src="http://datapark.io/img/logo.png" alt="datpark" width="35%" align="right" border="0"><br>
Analyzing Bitcoin Data with PQP and Quandl.com
End of explanation
"""


url = "https://www.quandl.com/api/v1/datasets/BCHAIN/TOTBC.json"

number = read_quandl(url)

"""
Explanation: Number of Bitcoins
End of explanation
"""


number.plot(figsize=(10, 5))

"""
Explanation: Number of Bitcoins in existence.
End of explanation
"""


url = "https://www.quandl.com/api/v1/datasets/BAVERAGE/USD.json"

quotes = read_quandl(url)

"""
Explanation: Bitcoin Rate
End of explanation
"""


quotes[['24h Average', 'Total Volume']][quotes.index > '2012-12-31'].plot(
        subplots=True, figsize=(10, 5), color='b')

"""
Explanation: Historical Bitcoin rate in USD.
End of explanation
"""


quotes[['24h Average', 'Total Volume']][quotes.index > '2014-12-31'].plot(
        subplots=True, figsize=(10, 5), color='b')

"""
Explanation: Bitcoin rate to the USD in 2015.
End of explanation
"""


