hacktoberfest.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. """
  2. This script finds repos participating in hacktoberfest:
  3. - Hosted on GitHub or gitlab
  4. - Includes the "hacktoberfest" topic
  5. To run, install from pip:
  6. - pygithub
  7. - python-gitlab
  8. Add environment variables:
  9. - GH_TOKEN (see https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token#creating-a-token)
  10. - with public_repo permission
  11. - GL_TOKEN (see https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html#create-a-personal-access-token)
  12. - with read_api scope
  13. """
  14. import os
  15. import re
  16. from github import Github, GithubException
  17. from gitlab import Gitlab, GitlabHttpError, GitlabGetError
  18. from scripts.utils import games
  19. GH_REGEX = re.compile(r"https://github.com/([^/]+)/([^/]+)")
  20. GL_REGEX = re.compile(r"https://gitlab.com/([^/]+/[^/]+)")
  21. def main():
  22. gh = Github(os.environ["GH_TOKEN"])
  23. gl = Gitlab("https://gitlab.com", private_token=os.environ["GL_TOKEN"])
  24. hacktober_games = {}
  25. for game in games():
  26. repo_url = game.get("repo", "")
  27. if not repo_url:
  28. continue
  29. match = re.match(GH_REGEX, repo_url)
  30. if match:
  31. owner, repo = match.groups()
  32. try:
  33. gh_repo = gh.get_repo(f"{owner}/{repo}")
  34. except GithubException as e:
  35. print(f"Error getting GitHub repo info for {owner}/{repo}: {e}")
  36. continue
  37. topics = gh_repo.get_topics()
  38. if "hacktoberfest" in topics:
  39. game["platform"] = "github"
  40. game["stars"] = gh_repo.stargazers_count
  41. hacktober_games[game["name"]] = game
  42. else:
  43. match = re.match(GL_REGEX, repo_url)
  44. if match:
  45. project_namespace = match.groups()[0]
  46. try:
  47. project = gl.projects.get(project_namespace)
  48. except (GitlabHttpError, GitlabGetError) as e:
  49. print(f"Error getting Gitlab repo info for {project_namespace}: {e}")
  50. continue
  51. if "hacktoberfest" in project.topics:
  52. game["platform"] = "gitlab"
  53. game["stars"] = project.star_count
  54. hacktober_games[game["name"]] = game
  55. for name, game in hacktober_games.items():
  56. stars_badge = f"![stars](https://img.shields.io/badge/{game['platform']}%20stars-{game['stars']}-blue)"
  57. langs = ", ".join(f"`{lang}`" for lang in game.get('langs', []))
  58. frameworks = ", ".join(f"`{fw}`" for fw in game.get('frameworks', []))
  59. print(f"- [**{name}** {stars_badge}]({game['repo']}): ({game.get('development', '')} {game.get('status', '')} {langs} {frameworks})")
  60. if __name__ == "__main__":
  61. main()