Skip to content

Instantly share code, notes, and snippets.

@j2kun
Created February 17, 2024 17:59
Show Gist options
  • Save j2kun/6a730559394e795e529eb4324636f83c to your computer and use it in GitHub Desktop.
Save j2kun/6a730559394e795e529eb4324636f83c to your computer and use it in GitHub Desktop.
search GitHub code filtering on star count of repository
import argparse
from github import Github, Auth
parser = argparse.ArgumentParser(description="Search for code on GitHub")
parser.add_argument('query')
parser.add_argument('-o', '--output_file')
parser.add_argument('-m', '--max_iters')
args = parser.parse_args()
auth = Auth.Token("YOUR_TOKEN")
g = Github(auth=auth)
results = g.search_code(args.query)
max_iters = args.max_iters or 1000
print(results.totalCount)
processed = dict()
# iterate over the subset of results that have at least 50 stars
iters = 0
for result in results:
    if result.repository.stargazers_count > 50:
        if result.repository.full_name in processed:
            continue
        line = ",".join(
                [
                    result.repository.full_name,
                    str(result.repository.stargazers_count),
                    result.html_url,
                ]
            )
        print(line)
        processed[result.repository.full_name] = line
    iters += 1
    if iters > max_iters:
        print(f"Reached {max_iters=}")
        break
sorted = sorted(processed.values(), key=lambda x: int(x.split(",")[1]), reverse=True)
if not args.output_file:
    args.output_file = f"results_{args.query}.csv"
with open(args.output_file, "w") as f:
    for line in sorted:
        f.write(line + "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment