import pymysql
from apscheduler.schedulers.background import BackgroundScheduler
import requests
import datetime
import time
logging.basicConfig(filename = 'tstw.log',format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
db = pymysql.connect(host = "localhost",user = "root",password = "root",database = "dogeusdt",charset = 'utf8' )
global cursor
cursor = db.cursor()
global sql
global last_datetime
last_datetime = None
sql1 = "CREATE TABLE IF NOT EXISTS `1min` ( `datetime` date,`open` float,`high` float,`low` float,`close` float)"
cursor.execute(sql1)
sql = 'insert into 1min(datetime,open,high,low,close) values(%(datetime)s,%(open)s,%(high)s,%(low)s,%(close)s);'
def get_kbar_and_save():
for i in range(3):
try:
r1 = requests.get("https://api.huobi.pro/market/history/kline?period=1min&size=1&symbol=btcusdt").json()
r1_data = r1['data'][0]
if (r1['status']=='ok') and (last_datetime - datetime.datetime.fromtimestamp(r1_d['id'])== datetime.timedelta(seconds=60)):
r1_data['datetime'] = datetime.datetime.fromtimestamp(r1_d['id'])
try:
cursor.execute(sql,r1_data )
db.commit()
last_datetime = r1_data ['datetime']
return logging.info('已写入数据库')
except:
logging.warning('数据库写入错误')
r1_data['datetime'] = datetime.datetime.now()
r1_data['open'] = 0
r1_data['high'] = 0
r1_data['low'] = 0
r1_data['close'] = 0
else:
time.sleep(10)
except:
time.sleep(10)
sche_00 = BackgroundScheduler()
sche_00.add_job(get_kbar_and_save, 'cron', hour='*',minute='*', second='0', id='task0')
sche_00.start()```
觉得自己太菜了想放弃的时候该怎么心理建设。。。
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.