Created
February 17, 2024 17:59
-
-
Save j2kun/6a730559394e795e529eb4324636f83c to your computer and use it in GitHub Desktop.
search GitHub code filtering on star count of repository
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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