Skip to content

Instantly share code, notes, and snippets.

@arjunlol
Created September 26, 2023 06:26
Show Gist options
  • Save arjunlol/d2f2ff99a65754fbe0cfd99f8adb764b to your computer and use it in GitHub Desktop.
Save arjunlol/d2f2ff99a65754fbe0cfd99f8adb764b to your computer and use it in GitHub Desktop.
import requests
import csv
import os
from dotenv import load_dotenv
load_dotenv()
token = os.getenv("GITHUB_TOKEN")
headers = {"Authorization": f"Bearer {token}"}
owner = "paper-trail-gem"
repo = "paper_trail"
page = 1
has_next_page = True
csv_filename = f"{owner}_{repo}_stargazers.csv"
with open(csv_filename, mode="w", newline="") as csv_file:
fieldnames = ["Name", "Organization", "Github Link", "Email"]
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
while has_next_page:
response = requests.get(
f"https://api.github.com/repos/{owner}/{repo}/stargazers?page={page}", headers=headers
)
if response.status_code != 200:
raise
stargazers = response.json()
for stargazer in stargazers:
username = stargazer["login"]
user_response = requests.get(f"https://api.github.com/users/{username}", headers=headers)
user_data = user_response.json()
if user_data["company"] is not None:
name = user_data.get("name", "")
organization = user_data["company"]
email = user_data.get("email")
link = f"https://github.com/{username}"
row = {"Name": name, "Organization": organization, "Github Link": link, "Email": email}
writer.writerow(row)
print(f"Checkpoint - scraped page {page}")
link_header = response.headers.get("Link", "")
if "rel=\"next\"" not in link_header:
has_next_page = False
else:
page += 1
print(f"Stargazers' information has been scraped and saved to {csv_filename}.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment