ths_api.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import hug
  2. from bottle import route, run,NORUN
  3. import iFinDPy as THS
  4. import json
  5. import time
  6. hug.API(__name__).http.output_format = hug.output_format.json
  7. username = "账号"
  8. password = "密码"
  9. @hug.get('/hz_server')
  10. def hello():
  11. return 1
  12. # return 'wind true'
  13. # 同花顺登录函数
  14. def thslogindemo():
  15. # 输入用户的帐号和密码
  16. thsLogin = THS.THS_iFinDLogin(username, password)
  17. print(thsLogin)
  18. if (thsLogin == 0 or thsLogin == -201):
  19. print('ths 登录成功')
  20. else:
  21. print('ths 登录失败')
  22. @hug.get('/edbInfo/ths')
  23. def GetEdbDataByThs(EdbCode, StartDate, EndDate):
  24. print("GetEdbDataByThs")
  25. thsLogin = THS.THS_iFinDLogin(username, password)
  26. thsEDBDataQuery = THS.THS_EDBQuery(EdbCode, StartDate, EndDate, True)
  27. print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ":start GetEdbDataByThs")
  28. print("thsEDBDataQuery")
  29. print(thsEDBDataQuery)
  30. thsEDBDataQuery = thsEDBDataQuery.decode("unicode_escape")
  31. result = json.loads(thsEDBDataQuery)
  32. print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ":end GetEdbDataByThs")
  33. return result
  34. @hug.get('/edbInfo/ths/future_good')
  35. def GetFutureGoodEdbDataByThs(EdbCode, StartDate, EndDate):
  36. print("THS_HistoryQuotes start")
  37. thsLogin = THS.THS_iFinDLogin(username, password)
  38. filed = 'lastclose,open,high,low,close,avgprice,change,changeper,volume,amount,hsl,lastsettlement,settlement,zdsettlement,zdfsettlement,ccl,ccbd,zf,zjlx,zjcd'
  39. # filed = 'open,high,low,close,volume,amount,settlement,ccl,zjlx'
  40. params = 'Interval:D,CPS:1,baseDate:1900-01-01,Currency:YSHB,fill:Previous'
  41. # params = 'YSHB;Tradedays'
  42. # print(params)
  43. thsEDBDataQuery = THS.THS_HistoryQuotes(EdbCode, filed, params, StartDate, EndDate, True)
  44. # thsEDBDataQuery = THS.THS_HQ(EdbCode, filed, params, StartDate, EndDate)
  45. # thsEDBDataQuery = THS.THS_EDBQuery(EdbCode, StartDate, EndDate, True)
  46. print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ":start GetFutureGoodEdbDataByThs")
  47. print("THS_HistoryQuotes end")
  48. print(thsEDBDataQuery)
  49. thsEDBDataQuery = thsEDBDataQuery.decode("unicode_escape")
  50. result = json.loads(thsEDBDataQuery)
  51. print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ":end GetFutureGoodEdbDataByThs")
  52. return result
  53. def ths_data_to_dict(ths_instance):
  54. if ths_instance is not None and ths_instance.data is not None:
  55. return {
  56. 'errorcode': ths_instance.errorcode,
  57. 'errmsg': ths_instance.errmsg,
  58. 'data': ths_instance.data.to_dict(orient='records')
  59. }
  60. else:
  61. # 处理 ths_instance 为 None 的情况
  62. return {
  63. 'errorcode': 'N/A',
  64. 'errmsg': 'N/A',
  65. 'data': []
  66. }
  67. def has_comma(input_string):
  68. return isinstance(input_string, str) and ',' in input_string
  69. def process_single_edb_code(StockCode, SingleEdbCode, StartDate, EndDate):
  70. thsEDBDataQuery = THS.THS_DS(StockCode, SingleEdbCode, '', '', StartDate, EndDate)
  71. print(thsEDBDataQuery)
  72. result = json.dumps(ths_data_to_dict(thsEDBDataQuery))
  73. return result
  74. @hug.get('/edbInfo/ths/ds')
  75. def GetEdbDataByThsDs(StockCode, EdbCode, StartDate, EndDate):
  76. print("THS_DataSequence start")
  77. print(EdbCode)
  78. result_list = []
  79. if isinstance(EdbCode, list):
  80. for SingleEdbCode in EdbCode:
  81. result_list.append(process_single_edb_code(StockCode, SingleEdbCode, StartDate, EndDate))
  82. print("THS_DS end")
  83. print(result_list)
  84. return result_list
  85. else:
  86. # 如果不包含逗号,直接处理
  87. result = process_single_edb_code(StockCode, EdbCode, StartDate, EndDate)
  88. print(result)
  89. return result
  90. if __name__ == "__main__":
  91. # ths登录函数
  92. thslogindemo()
  93. app = __hug__.http.server()
  94. run(app=app, reloader=True, port=7000)