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