github_searcher/github_searcher.py
2024-08-26 13:56:26 -05:00

55 lines
No EOL
1.6 KiB
Python

import os
import argparse
from github import Github, Auth
def auth():
access_token = os.getenv("GITHUB_ACCESS_TOKEN")
auth = Auth.Token(access_token)
g = Github(auth=auth)
return g
class GithubSearcher():
def __init__(self, query):
self.g = auth()
self.query = query
self.result = None
def search_repo(self):
self.result = self.g.search_repositories(self.query)
def search_users(self):
self.result = self.g.search_users(self.query)
def search_in_name(self):
self.result = self.g.search_repositories('in:name ' + self.query)
def get_result(self):
return self.result
def main():
parser = argparse.ArgumentParser(description="Search GitHub repositories and users.")
parser.add_argument("--query", type=str, help="The search query.")
parser.add_argument("--search_type", type=str, choices=["repo", "users", "name"], help="The type of search to perform: 'repo', 'users', or 'name'.")
args = parser.parse_args()
searcher = GithubSearcher(args.query)
if args.search_type == "repo":
searcher.search_repo()
elif args.search_type == "users":
searcher.search_users()
elif args.search_type == "name":
searcher.search_in_name()
result = searcher.get_result()
for item in result:
# If users, print all user repo URLs
if args.search_type == "users":
user_repos = item.get_repos()
for repo in user_repos:
print(repo.html_url)
else:
print(item.html_url)
if __name__ == "__main__":
main()