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.

github.py 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 logging
  17. from abc import ABC
  18. import pandas as pd
  19. import requests
  20. from agent.component.base import ComponentBase, ComponentParamBase
  21. class GitHubParam(ComponentParamBase):
  22. """
  23. Define the GitHub component parameters.
  24. """
  25. def __init__(self):
  26. super().__init__()
  27. self.top_n = 10
  28. def check(self):
  29. self.check_positive_integer(self.top_n, "Top N")
  30. class GitHub(ComponentBase, ABC):
  31. component_name = "GitHub"
  32. def _run(self, history, **kwargs):
  33. ans = self.get_input()
  34. ans = " - ".join(ans["content"]) if "content" in ans else ""
  35. if not ans:
  36. return GitHub.be_output("")
  37. try:
  38. url = 'https://api.github.com/search/repositories?q=' + ans + '&sort=stars&order=desc&per_page=' + str(
  39. self._param.top_n)
  40. headers = {"Content-Type": "application/vnd.github+json", "X-GitHub-Api-Version": '2022-11-28'}
  41. response = requests.get(url=url, headers=headers).json()
  42. github_res = [{"content": '<a href="' + i["html_url"] + '">' + i["name"] + '</a>' + str(
  43. i["description"]) + '\n stars:' + str(i['watchers'])} for i in response['items']]
  44. except Exception as e:
  45. return GitHub.be_output("**ERROR**: " + str(e))
  46. if not github_res:
  47. return GitHub.be_output("")
  48. df = pd.DataFrame(github_res)
  49. logging.debug(f"df: {df}")
  50. return df