Last active
March 16, 2025 10:06
-
-
Save monperrus/b1ffe956fed22745b1ba59e7a5447c20 to your computer and use it in GitHub Desktop.
a python script to call the github search api looking for code
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
| #!/usr/bin/python | |
| import requests | |
| import json | |
| import os | |
| import time | |
| def search_github_code(query, language=None, sort=None, order=None, per_page=30, page=1): | |
| """ | |
| Search for code on GitHub using the GitHub Search API. | |
| Parameters: | |
| - query (str): The search query | |
| - language (str, optional): Filter by programming language | |
| - sort (str, optional): Sort results by 'indexed' (default) or 'best-match' | |
| - order (str, optional): Order results 'desc' (default) or 'asc' | |
| - per_page (int, optional): Number of results per page (max 100) | |
| - page (int, optional): Page number for pagination | |
| Returns: | |
| - dict: JSON response from GitHub API | |
| """ | |
| # Base URL for GitHub Search API | |
| base_url = "https://api.github.com/search/code" | |
| # Construct the query | |
| search_query = query | |
| if language: | |
| search_query += f" language:{language}" | |
| # Parameters for the request | |
| params = { | |
| "q": search_query, | |
| "per_page": per_page, | |
| "page": page | |
| } | |
| if sort: | |
| params["sort"] = sort | |
| if order: | |
| params["order"] = order | |
| # Headers for the request | |
| headers = { | |
| "Accept": "application/vnd.github.v3+json" | |
| } | |
| # Add GitHub token if available | |
| github_token = os.environ.get("GITHUB_API_TOKEN") | |
| if github_token: | |
| headers["Authorization"] = f"token {github_token}" | |
| else: | |
| print("Warning: No GITHUB_TOKEN found in environment variables.") | |
| print("Unauthenticated requests are rate limited to 10 requests per minute.") | |
| # Make the request | |
| response = requests.get(base_url, params=params, headers=headers) | |
| # Check for rate limiting | |
| if response.status_code == 403 and "rate limit exceeded" in response.text.lower(): | |
| reset_time = int(response.headers.get("X-RateLimit-Reset", 0)) | |
| wait_time = max(0, reset_time - int(time.time())) | |
| print(f"Rate limit exceeded. Reset in {wait_time} seconds.") | |
| if wait_time > 0 and wait_time < 300: # Only wait if less than 5 minutes | |
| print(f"Waiting {wait_time} seconds...") | |
| time.sleep(wait_time) | |
| return search_github_code(query, language, sort, order, per_page, page) | |
| # Check if the request was successful | |
| response.raise_for_status() | |
| return response.json() | |
| def display_results(results): | |
| """Display search results in a readable format.""" | |
| total_count = results.get("total_count", 0) | |
| items = results.get("items", []) | |
| print(f"Found {total_count} code results") | |
| print("-" * 50) | |
| for i, item in enumerate(items, 1): | |
| repo = item.get("repository", {}) | |
| print(f"{i}. {item.get('name')}") | |
| print(f" Repository: {repo.get('full_name')}") | |
| print(f" URL: {item.get('html_url')}") | |
| print(f" Path: {item.get('path')}") | |
| print("-" * 50) | |
| if __name__ == "__main__": | |
| import argparse | |
| parser = argparse.ArgumentParser(description="Search for code on GitHub") | |
| parser.add_argument("query", help="Search query") | |
| parser.add_argument("--language", "-l", help="Filter by programming language") | |
| parser.add_argument("--sort", "-s", choices=["indexed", "best-match"], | |
| help="Sort results by 'indexed' (default) or 'best-match'") | |
| parser.add_argument("--order", "-o", choices=["desc", "asc"], | |
| help="Order results 'desc' (default) or 'asc'") | |
| parser.add_argument("--per-page", "-p", type=int, default=30, | |
| help="Number of results per page (max 100)") | |
| parser.add_argument("--page", type=int, default=1, | |
| help="Page number for pagination") | |
| args = parser.parse_args() | |
| try: | |
| results = search_github_code( | |
| args.query, | |
| args.language, | |
| args.sort, | |
| args.order, | |
| args.per_page, | |
| args.page | |
| ) | |
| display_results(results) | |
| except requests.exceptions.HTTPError as e: | |
| print(f"Error: {e}") | |
| except Exception as e: | |
| print(f"An unexpected error occurred: {e}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment