import requests
import urllib3
from concurrent.futures import ThreadPoolExecutor, as_completed
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def fetch_gists(username, token):
headers = {"Authorization": f"token {token}"}
url = f"https://api.github.com/users/{username}/gists"
response = requests.get(url, headers=headers, verify=False)
if response.status_code == 200:
return response.json()
else:
print("Error:", response.status_code)
return None
def search_keyword_in_file(file_url, keyword):
file_content = requests.get(file_url, verify=False).text
return keyword.lower() in file_content.lower()
def search_keyword_in_gist(gist, keyword):
gist_id = gist["id"]
gist_files = gist["files"]
keyword_found_in = []
for file in gist_files.values():
file_url = file["raw_url"]
if search_keyword_in_file(file_url, keyword):
keyword_found_in.append({"gist_id": gist_id, "file_name": file["filename"]})
return keyword_found_in
def search_keyword_in_gists(username, token, keyword):
gists = fetch_gists(username, token)
if not gists:
print(f"No gists found for user {username}")
return
keyword_found_in = []
with ThreadPoolExecutor() as executor:
futures = [executor.submit(search_keyword_in_gist, gist, keyword) for gist in gists]
for future in as_completed(futures):
gist_results = future.result()
if gist_results:
folder_name = gist_results[0]["file_name"]
for result in gist_results:
result["folder_name"] = folder_name
keyword_found_in.extend(gist_results)
return keyword_found_in
username = "github_username"
token = "github_pat_token"
keyword = "gateway"
results = search_keyword_in_gists(username, token, keyword)
if results:
print(f"Keyword '{keyword}' found in the following gists:")
for result in results:
print(f"Folder name: {result['folder_name']}, Gist ID: {result['gist_id']}, File name: {result['file_name']}")
else:
print(f"No gists found containing the keyword '{keyword}'.")