#!/bin/env python
from __future__ import print_function
import json
import urllib
#import MySQLdb

url = "http://api.coindesk.com/v1/bpi/historical/close.json?start=2012-01-01&end=2013-10-31"

db_host = 'localhost'
db_user = 'stocks2'
db_pass = 'HNSCi0VOgbs6'
db_base = 'stocks2'

res = urllib.urlopen(url)
from pprint import pprint

data = json.loads(res.read())

#mysqldb = MySQLdb.connect(host=db_host,
#                          user=db_user,
#                          passwd=db_pass,
#                          db=db_base,
#                          charset='utf8',
#                          unix_socket='/tmp/mysql.sock')
#mysqlcursor = mysqldb.cursor()

for date, value in data['bpi'].items():
    sql = """INSERT INTO stocks(id, src, s_date, s_time, value, region, s_unixtime, scaled) VALUES ({0}, {1}, '{2}', '{3}', {4}, {5}, {6}, {7});""".format(
                60000, 40, date, '17:00:00', value, 0, 0, 1,)
    print(sql)
#    mysqlcursor.execute(sql)

#mysqldb.commit()
#mysqldb.close()
