Im having issues incrementing the time in the start_time variable. Im trying to have it so each loop-through it adds 5 seconds. I thought if I converted it to a timestamp and added 5000 to it, then converted back to iso that would work but it doesnt I get errors:
File "snaps.py", line 36, in <module>
stamp += 5000
TypeError: unsupported operand type(s) for +=: 'datetime.datetime' and 'int'
import requests
import json
import pandas as pd
import os
import dateutil.parser
import time
instrument = ('btc-usd')
exchange = ('cbse')
start_time = '2021-01-26T00:00:00Z'
stamp = dateutil.parser.parse(start_time)
d_range = 'Jan-14-16'
page_size = '1'
url = f'https://us.market-api.kaiko.io/v2/data/order_book_snapshots.v1/exchanges/{exchange}/spot/{instrument}/snapshots/depth'
params = { 'page_size': page_size, 'start_time': start_time }
KEY = 'xxx'
headers = {
"X-Api-Key": KEY,
"Accept": "application/json",
"Accept-Encoding": "gzip"
}
csv_file = f"Snapshot-{start_time}.csv"
snapshot = 0
while(snapshot <= 100):
res = requests.get(url, params=params, headers=headers)
j_data = res.json()
parse_data = j_data['data']
# create dataframe
df = pd.DataFrame.from_dict(pd.json_normalize(parse_data), orient='columns')
#df.insert(1, 'time', pd.to_datetime(df.timestamp.astype(int),unit='ms'))
snapshot += 1
stamp += 5000
start_time= datetime.datetime.fromtimestamp(stamp).isoformat()
#that means file already exists need to append
if(csv_file in os.listdir()):
csv_string = df.to_csv(index=False, encoding='utf-8', header=False)
with open(csv_file, 'a') as f:
f.write(csv_string)
#that means writing file for the first time
else:
csv_string = df.to_csv(index=False, encoding='utf-8')
with open(csv_file, 'w') as f:
f.write(csv_string)
question from:
https://stackoverflow.com/questions/65907133/issues-incrementing-start-time-parameter 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…