You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

akshare.py 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. from abc import ABC
  17. import pandas as pd
  18. import akshare as ak
  19. from agent.component.base import ComponentBase, ComponentParamBase
  20. class AkShareParam(ComponentParamBase):
  21. """
  22. Define the AkShare component parameters.
  23. """
  24. def __init__(self):
  25. super().__init__()
  26. self.top_n = 10
  27. def check(self):
  28. self.check_positive_integer(self.top_n, "Top N")
  29. class AkShare(ComponentBase, ABC):
  30. component_name = "AkShare"
  31. def _run(self, history, **kwargs):
  32. ans = self.get_input()
  33. ans = ",".join(ans["content"]) if "content" in ans else ""
  34. if not ans:
  35. return AkShare.be_output("")
  36. try:
  37. ak_res = []
  38. stock_news_em_df = ak.stock_news_em(symbol=ans)
  39. stock_news_em_df = stock_news_em_df.head(self._param.top_n)
  40. ak_res = [{"content": '<a href="' + i["新闻链接"] + '">' + i["新闻标题"] + '</a>\n 新闻内容: ' + i[
  41. "新闻内容"] + " \n发布时间:" + i["发布时间"] + " \n文章来源: " + i["文章来源"]} for index, i in stock_news_em_df.iterrows()]
  42. except Exception as e:
  43. return AkShare.be_output("**ERROR**: " + str(e))
  44. if not ak_res:
  45. return AkShare.be_output("")
  46. return pd.DataFrame(ak_res)