Skip to content

Instantly share code, notes, and snippets.

@ehzawad
Last active September 21, 2023 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ehzawad/1b58be89e8047a4df8786c12bde62bc2 to your computer and use it in GitHub Desktop.
Save ehzawad/1b58be89e8047a4df8786c12bde62bc2 to your computer and use it in GitHub Desktop.
python md
You can use the GitHub API to search for repositories, code, commits, issues, users, topics etc. Here is an example of how to search for repositories using Python's `requests` library.
```python
import requests
import json
def search_github(keyword):
url = 'https://api.github.com/search/repositories'
params = {'q': keyword}
response = requests.get(url, params=params)
print(response.status_code)
print(response.json())
search_github('python')
```
This will print out the status code of the response and the JSON response itself, which contains information about the repositories that match the search keyword.
Please note that GitHub API has a rate limit. For unauthenticated requests, the rate limit allows for up to 10 requests per minute. If you need to make more requests, you will need to authenticate your request.
Here is how you can authenticate your request using a personal access token:
```python
import requests
import json
def search_github(keyword, token):
url = 'https://api.github.com/search/repositories'
params = {'q': keyword}
headers = {'Authorization': f'token {token}'}
response = requests.get(url, params=params, headers=headers)
print(response.status_code)
print(response.json())
search_github('python', 'your_personal_access_token')
```
Replace `'your_personal_access_token'` with your actual personal access token. You can generate a personal access token in your GitHub account settings.
Remember to keep your personal access token secret. Do not share it with anyone and do not include it in your code if you plan to share your code publicly.
@ehzawad
Copy link
Author

ehzawad commented Sep 21, 2023

Here's how you can modify the function to pretty print the JSON response and extract only the repository name and link:

import requests
import json

def search_github(keyword, token):
    url = 'https://api.github.com/search/repositories'
    params = {'q': keyword}
    headers = {'Authorization': f'token {token}'}
    response = requests.get(url, params=params, headers=headers)
    
    if response.status_code == 200:
        response_json = response.json()
        print(json.dumps(response_json, indent=4))  # pretty print the JSON

        # Extract repo name and link
        for item in response_json['items']:
            print('Name: ', item['name'])
            print('Link: ', item['html_url'])
            print('-------------------')
    else:
        print('Request failed with status code', response.status_code)

search_github('python', 'your_personal_access_token')

This will print out the name and the link (html_url) of each repository in the search results. Replace 'your_personal_access_token' with your actual personal access token.

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