Skip to content

Instantly share code, notes, and snippets.

@louis030195
Created July 4, 2024 12:18
Show Gist options
  • Save louis030195/e00763766310a0f5a59a24f3ee0622a5 to your computer and use it in GitHub Desktop.
Save louis030195/e00763766310a0f5a59a24f3ee0622a5 to your computer and use it in GitHub Desktop.
A script that sync your github stargazers to loops.so
"""
virtualenv env
source env/bin/activate
pip install --upgrade pip setuptools
pip install python-dotenv
pip install requests
python3 scripts/main.py
"""
import requests
import os
import dotenv
import json
dotenv.load_dotenv()
# GitHub API endpoint for stargazers
GITHUB_API = "https://api.github.com/repos/louis030195/screen-pipe/stargazers"
# Loops API endpoint for creating contacts
LOOPS_API = "https://app.loops.so/api/v1/contacts/create"
# Replace with your GitHub token (if needed) and Loops API key
GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN")
LOOPS_API_KEY = os.environ.get("LOOPS_API_KEY")
def get_stargazers():
stargazers = []
page = 1
while True:
headers = {"Authorization": f"token {GITHUB_TOKEN}"} if GITHUB_TOKEN else {}
response = requests.get(f"{GITHUB_API}?page={page}&per_page=100", headers=headers)
if response.status_code != 200:
print(f"Error fetching stargazers: {response.status_code}")
break
data = response.json()
if not data:
break
print(f"Fetched {len(data)} stargazers on page {page}")
stargazers.extend(data)
page += 1
if len(data) < 100: # Last page
break
print(f"Total stargazers fetched: {len(stargazers)}")
return stargazers
def add_to_loops(user):
headers = {
"Authorization": f"Bearer {LOOPS_API_KEY}",
"Content-Type": "application/json"
}
data = {
"email": user["email"],
"firstName": user["name"].split()[0] if user["name"] else "",
"lastName": " ".join(user["name"].split()[1:]) if user["name"] else "",
"source": "GitHub Stargazer",
"userGroup": "screen-pipe-stargazers"
}
print(f"Adding {user['email']} to Loops")
response = requests.post(LOOPS_API, headers=headers, json=data)
if response.status_code == 200:
print(f"Added {user['login']} to Loops")
else:
print(f"Error adding {user['login']} to Loops: {response.status_code}")
import concurrent.futures
def process_stargazer(stargazer):
headers = {"Authorization": f"token {GITHUB_TOKEN}"} if GITHUB_TOKEN else {}
user_response = requests.get(stargazer["url"], headers=headers)
if user_response.status_code == 200:
user_data = user_response.json()
if user_data.get("email"):
add_to_loops(user_data)
else:
print(f"Error fetching user data for {stargazer['login']}: {user_response.status_code}: {user_response.text}")
def main():
stargazers = get_stargazers()
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
executor.map(process_stargazer, stargazers)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment