Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dmccuk/45ba75a346199dff818b9ed956197c08 to your computer and use it in GitHub Desktop.
Save dmccuk/45ba75a346199dff818b9ed956197c08 to your computer and use it in GitHub Desktop.

Generated via ChatGPT:

Hopefully this is what you need...

Python version:

To use this script, you'll need to replace your_company_name with the name of the company you're searching for, and your_github_api_token with a valid GitHub API token that has the necessary permissions to access user data. You can run the script from the command line or in a Python development environment, and it will print a list of usernames associated with the specified company on GitHub.

To use this script with the company_name argument, you can run the command like this:

python github_users.py -c teslamotors

This will search for usernames associated with the "teslamotors" company on GitHub. If you don't specify the company_name argument, the script will raise an error.

import argparse
import requests
import json

# Parse command-line arguments
parser = argparse.ArgumentParser(description='Search for GitHub usernames associated with a company')
parser.add_argument('-c', '--company', dest='company_name', required=True, help='the name of the company to search for')
args = parser.parse_args()

# Set the GitHub API token
api_token = "your_github_api_token"

# Define the API endpoint for searching GitHub users
api_endpoint = "https://api.github.com/search/users"

# Define the search parameters
search_params = {
    "q": f"type:user company:{args.company_name}",
    "per_page": 100,
    "page": 1
}

# Set the headers for the API request
headers = {
    "Authorization": f"token {api_token}",
    "Accept": "application/vnd.github.v3+json"
}

# Send the initial API request to get the first page of search results
response = requests.get(api_endpoint, headers=headers, params=search_params)

# Parse the JSON response and extract the usernames
usernames = [user["login"] for user in response.json()["items"]]

# Check if there are more pages of search results
while "next" in response.links:
    # Get the URL for the next page of search results
    next_page_url = response.links["next"]["url"]

    # Send a new API request to get the next page of search results
    response = requests.get(next_page_url, headers=headers)

    # Parse the JSON response and extract the usernames from this page
    usernames += [user["login"] for user in response.json()["items"]]

# Print the list of usernames associated with the company
print(usernames)

Bash Version

To use this script, you'll need to replace your_company_name with the name of the company you're searching for, and your_github_api_token with a valid GitHub API token that has the necessary permissions to access user data. You can run the script from the command line, and it will print a list of usernames associated with the specified company on GitHub. The script requires the jq command-line JSON processor to be installed.

To use this script with the -company option, you can run the command like this:

./github_users.sh -c teslamotors

This will search for usernames associated with the "teslamotors" company on GitHub. If you don't specify the -company option, the script will use the default company name set in the script.

#!/bin/bash

# Set the default company name and GitHub API token
company_name="your_company_name"
api_token="your_github_api_token"

# Parse command-line options
while [[ $# -gt 0 ]]; do
    key="$1"
    case $key in
        -c|--company)
            company_name="$2"
            shift
            shift
            ;;
        *)
            echo "Unknown option: $1"
            exit 1
            ;;
    esac
done

# Define the API endpoint for searching GitHub users
api_endpoint="https://api.github.com/search/users"

# Define the search parameters
search_params="q=type:user+company:$company_name&per_page=100&page=1"

# Set the headers for the API request
headers="-H 'Authorization: token $api_token' -H 'Accept: application/vnd.github.v3+json'"

# Send the initial API request to get the first page of search results
response=$(curl -sSL "$api_endpoint?$search_params" $headers)

# Parse the JSON response and extract the usernames
usernames=($(echo $response | jq -r '.items[].login'))

# Check if there are more pages of search results
while [ "$(echo $response | jq -r '.links[].rel' | grep next)" ]; do
    # Get the URL for the next page of search results
    next_page_url=$(echo $response | jq -r '.links[] | select(.rel == "next").url')

    # Send a new API request to get the next page of search results
    response=$(curl -sSL "$next_page_url" $headers)

    # Parse the JSON response and extract the usernames from this page
    usernames+=($(echo $response | jq -r '.items[].login'))
done

# Print the list of usernames associated with the company
printf '%s\n' "${usernames[@]}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment