import os import time import pandas as pd FETCH_URL = "https://poloniex.com/public?command=returnChartData¤cyPair=%s&start=%d&end=%d&period=300" #PAIR_LIST = ["BTC_ETH"] DATA_DIR = "data" COLUMNS = ["date","high","low","open","close","volume","quoteVolume","weightedAverage"] def get_data(pair): datafile = os.path.join(DATA_DIR, pair+".csv") timefile = os.path.join(DATA_DIR, pair) if os.path.exists(datafile): newfile = False start_time = int(open(timefile).readline()) + 1 else: newfile = True start_time = 1388534400 # 2014.01.01 end_time = 9999999999#start_time + 86400*30 url = FETCH_URL % (pair, start_time, end_time) print("Get %s from %d to %d" % (pair, start_time, end_time)) df = pd.read_json(url, convert_dates=False) #import pdb;pdb.set_trace() if df["date"].iloc[-1] == 0: print("No data.") return end_time = df["date"].iloc[-1] ft = open(timefile,"w") ft.write("%d\n" % end_time) ft.close() outf = open(datafile, "a") if newfile: df.to_csv(outf, index=False, cols=COLUMNS) else: df.to_csv(outf, index=False, cols=COLUMNS, header=False) outf.close() print("Finish.") time.sleep(30) def main(): if not os.path.exists(DATA_DIR): os.mkdir(DATA_DIR) df = pd.read_json("https://poloniex.com/public?command=return24hVolume") pairs = [pair for pair in df.columns if pair.startswith('BTC')] print(pairs) for pair in pairs: get_data(pair) time.sleep(2) if __name__ == '__main__': main()