|
| 1 | +import requests |
| 2 | + |
| 3 | +def get_repository_contents(repo_owner, repo_name): |
| 4 | + url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/contents" |
| 5 | + response = requests.get(url) |
| 6 | + |
| 7 | + if response.status_code == 200: |
| 8 | + return response.json() |
| 9 | + else: |
| 10 | + print(f"Failed to fetch repository contents. Status code: {response.status_code}") |
| 11 | + return None |
| 12 | + |
| 13 | +def extract_parent_folder(script_path): |
| 14 | + return script_path.rsplit("/", 1)[0] |
| 15 | + |
| 16 | +def filter_items_by_keyword(contents, keyword, repo_owner, repo_name): |
| 17 | + filtered_items = [] |
| 18 | + repo_url = f"https://github.com/{repo_owner}/{repo_name}" |
| 19 | + |
| 20 | + for item in contents: |
| 21 | + item_name = item["name"].lower() |
| 22 | + item_type = item["type"] |
| 23 | + |
| 24 | + if keyword.lower() in item_name: |
| 25 | + if item_type == "file": |
| 26 | + filtered_items.append((item_name, item["html_url"])) |
| 27 | + elif item_type == "dir": |
| 28 | + filtered_items.append((item_name, item["html_url"])) |
| 29 | + |
| 30 | + return filtered_items |
| 31 | + |
| 32 | +if __name__ == "__main__": |
| 33 | + # Replace with your GitHub repository details |
| 34 | + owner = "avinashkranjan" |
| 35 | + repo = "Amazing-Python-Scripts" |
| 36 | + |
| 37 | + # Get repository contents |
| 38 | + contents = get_repository_contents(owner, repo) |
| 39 | + |
| 40 | + if contents: |
| 41 | + search_keyword = input("\nEnter the keyword to search for in the scripts: ") |
| 42 | + found_items = filter_items_by_keyword(contents, search_keyword, owner, repo) |
| 43 | + |
| 44 | + if found_items: |
| 45 | + print("\nFound items matching the keyword:\n") |
| 46 | + for idx, (item_name, item_url) in enumerate(found_items, start=1): |
| 47 | + print(f"{idx}) {item_name} ({item_url})") |
| 48 | + print("\n") |
| 49 | + else: |
| 50 | + print("No items found matching the keyword.") |
0 commit comments