選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import json
  17. from abc import ABC
  18. import pandas as pd
  19. import requests
  20. from agent.component.base import ComponentBase, ComponentParamBase
  21. class Jin10Param(ComponentParamBase):
  22. """
  23. Define the Jin10 component parameters.
  24. """
  25. def __init__(self):
  26. super().__init__()
  27. self.type = "flash"
  28. self.secret_key = "xxx"
  29. self.flash_type = '1'
  30. self.calendar_type = 'cj'
  31. self.calendar_datatype = 'data'
  32. self.symbols_type = 'GOODS'
  33. self.symbols_datatype = 'symbols'
  34. self.contain = ""
  35. self.filter = ""
  36. def check(self):
  37. self.check_valid_value(self.type, "Type", ['flash', 'calendar', 'symbols', 'news'])
  38. self.check_valid_value(self.flash_type, "Flash Type", ['1', '2', '3', '4', '5'])
  39. self.check_valid_value(self.calendar_type, "Calendar Type", ['cj', 'qh', 'hk', 'us'])
  40. self.check_valid_value(self.calendar_datatype, "Calendar DataType", ['data', 'event', 'holiday'])
  41. self.check_valid_value(self.symbols_type, "Symbols Type", ['GOODS', 'FOREX', 'FUTURE', 'CRYPTO'])
  42. self.check_valid_value(self.symbols_datatype, 'Symbols DataType', ['symbols', 'quotes'])
  43. class Jin10(ComponentBase, ABC):
  44. component_name = "Jin10"
  45. def _run(self, history, **kwargs):
  46. ans = self.get_input()
  47. ans = " - ".join(ans["content"]) if "content" in ans else ""
  48. if not ans:
  49. return Jin10.be_output("")
  50. jin10_res = []
  51. headers = {'secret-key': self._param.secret_key}
  52. try:
  53. if self._param.type == "flash":
  54. params = {
  55. 'category': self._param.flash_type,
  56. 'contain': self._param.contain,
  57. 'filter': self._param.filter
  58. }
  59. response = requests.get(
  60. url='https://open-data-api.jin10.com/data-api/flash?category=' + self._param.flash_type,
  61. headers=headers, data=json.dumps(params))
  62. response = response.json()
  63. for i in response['data']:
  64. jin10_res.append({"content": i['data']['content']})
  65. if self._param.type == "calendar":
  66. params = {
  67. 'category': self._param.calendar_type
  68. }
  69. response = requests.get(
  70. url='https://open-data-api.jin10.com/data-api/calendar/' + self._param.calendar_datatype + '?category=' + self._param.calendar_type,
  71. headers=headers, data=json.dumps(params))
  72. response = response.json()
  73. jin10_res.append({"content": pd.DataFrame(response['data']).to_markdown()})
  74. if self._param.type == "symbols":
  75. params = {
  76. 'type': self._param.symbols_type
  77. }
  78. if self._param.symbols_datatype == "quotes":
  79. params['codes'] = 'BTCUSD'
  80. response = requests.get(
  81. url='https://open-data-api.jin10.com/data-api/' + self._param.symbols_datatype + '?type=' + self._param.symbols_type,
  82. headers=headers, data=json.dumps(params))
  83. response = response.json()
  84. if self._param.symbols_datatype == "symbols":
  85. for i in response['data']:
  86. i['Commodity Code'] = i['c']
  87. i['Stock Exchange'] = i['e']
  88. i['Commodity Name'] = i['n']
  89. i['Commodity Type'] = i['t']
  90. del i['c'], i['e'], i['n'], i['t']
  91. if self._param.symbols_datatype == "quotes":
  92. for i in response['data']:
  93. i['Selling Price'] = i['a']
  94. i['Buying Price'] = i['b']
  95. i['Commodity Code'] = i['c']
  96. i['Stock Exchange'] = i['e']
  97. i['Highest Price'] = i['h']
  98. i['Yesterday’s Closing Price'] = i['hc']
  99. i['Lowest Price'] = i['l']
  100. i['Opening Price'] = i['o']
  101. i['Latest Price'] = i['p']
  102. i['Market Quote Time'] = i['t']
  103. del i['a'], i['b'], i['c'], i['e'], i['h'], i['hc'], i['l'], i['o'], i['p'], i['t']
  104. jin10_res.append({"content": pd.DataFrame(response['data']).to_markdown()})
  105. if self._param.type == "news":
  106. params = {
  107. 'contain': self._param.contain,
  108. 'filter': self._param.filter
  109. }
  110. response = requests.get(
  111. url='https://open-data-api.jin10.com/data-api/news',
  112. headers=headers, data=json.dumps(params))
  113. response = response.json()
  114. jin10_res.append({"content": pd.DataFrame(response['data']).to_markdown()})
  115. except Exception as e:
  116. return Jin10.be_output("**ERROR**: " + str(e))
  117. if not jin10_res:
  118. return Jin10.be_output("")
  119. return pd.DataFrame(jin10_res)