-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
276 lines (222 loc) · 10.8 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import os
import sys
import time
from dotenv import load_dotenv
import requests
from colorama import init, Fore, Style
if sys.platform == "win32":
os.system('cls')
else:
os.system('clear')
init(autoreset=True) # Initialize colorama to automatically reset color
load_dotenv() # Load environment variables from .env file (just the github access token for now)
def fetch_github_repositories(search_term, language=None, max_results=250):
access_token = os.getenv("GITHUB_ACCESS_TOKEN")
if not access_token:
access_token = input("Enter your GitHub access token: ")
os.environ["GITHUB_ACCESS_TOKEN"] = access_token
base_url = "https://api.github.com/search/repositories"
headers = {"Authorization": f"Bearer {access_token}"}
if language:
language = language.lower()
params = {"q": search_term, "language": language, "per_page": 250} if language else {"q": search_term, "per_page": 250}
repositories = []
page = 1
total_results = 0 # Track the total number of results found
while True:
params["page"] = page
response = requests.get(base_url, params=params, headers=headers)
response_json = response.json()
if response.status_code == 200:
current_repositories = response_json["items"]
if not current_repositories:
break
repositories.extend(current_repositories)
total_results = response_json.get('total_count', 0) # Update the total number of results
page += 1
if len(repositories) >= max_results or len(current_repositories) < 100:
break
else:
print(f"{Fore.RED}Failed to fetch repositories. Status code: {response.status_code}")
break
# Sort repositories based on the 'updated_at' field (most recently updated to oldest updated)
repositories.sort(key=lambda x: x.get('updated_at'), reverse=True)
return repositories[:max_results], total_results
def display_repositories(repositories):
for repo in repositories:
print(f"{Fore.GREEN}Repository: {repo['name']}")
print(f"{Fore.YELLOW}Description: {repo['description']}")
print(f"{Fore.CYAN}URL: {repo['html_url']}")
print(f"{Fore.MAGENTA}Language: {repo['language']}")
# Check if the 'updated_at' field is available
if repo.get('updated_at'):
updated_at = repo['updated_at'].replace('T', ' ').replace('Z', ' UTC')
print(f"{Fore.RESET}Last Updated: {updated_at}")
else:
print(f"{Fore.RESET}Last Updated: Not available")
# Check if the repository is archived by the owner
if repo.get('archived', False):
print(f"{Fore.RED}Repository is archived by the owner [!]")
print("=" * 60)
def fetch_github_users(search_term, max_results=250):
access_token = os.getenv("GITHUB_ACCESS_TOKEN")
if not access_token:
access_token = input("Enter your GitHub access token: ")
os.environ["GITHUB_ACCESS_TOKEN"] = access_token
base_url = "https://api.github.com/search/users"
headers = {"Authorization": f"Bearer {access_token}"}
params = {"q": search_term, "per_page": 250}
users = []
page = 1
total_results = 0 # Track the total number of results found
while True:
params["page"] = page
response = requests.get(base_url, params=params, headers=headers)
response_json = response.json()
if response.status_code == 200:
current_users = response_json["items"]
if not current_users:
break
users.extend(current_users)
total_results = response_json.get('total_count', 0) # Update the total number of results
page += 1
if len(users) >= max_results or len(current_users) < 100:
break
else:
print(f"{Fore.RED}Failed to fetch users. Status code: {response.status_code}")
break
return users[:max_results], total_results
def display_github_users(users):
for user in users:
print(f"{Fore.GREEN}User: {user['login']}")
print(f"{Fore.CYAN}Profile: {user['html_url']}")
print("=" * 60)
print("")
while True:
print(f"""{Fore.BLUE}
_____ _ _ _ _ _
/ ____(_) | | | | | | |
| | __ _| |_ | |__| |_ _ _ __ | |_
| | |_ | | __| | __ | | | | '_ \| __|
| |__| | | |_ | | | | |_| | | | | |_
\_____|_|\__| |_| |_|\__,_|_| |_|\__|
A Simple GitHub Search 🔎 Tool
{Fore.RESET}""")
search_term = input(f"{Fore.CYAN}Enter the search term: {Fore.RESET}")
if not search_term:
print(f"{Fore.YELLOW}Search term cannot be empty. Please try again.")
time.sleep(3)
if sys.platform == "win32":
os.system('cls')
else:
os.system('clear')
print(f"""{Fore.BLUE}
_____ _ _ _ _ _
/ ____(_) | | | | | | |
| | __ _| |_ | |__| |_ _ _ __ | |_
| | |_ | | __| | __ | | | | '_ \| __|
| |__| | | |_ | | | | |_| | | | | |_
\_____|_|\__| |_| |_|\__,_|_| |_|\__|
A Simple GitHub Search 🔎 Tool
{Fore.RESET}""")
continue
search_mode = input(f"{Fore.CYAN}Choose a search mode (1: Repositories, 2: Users, 3: Both): {Fore.RESET}")
if search_mode == '1':
language = input(f"{Fore.CYAN}Enter the programming language (optional): {Fore.RESET}")
max_results_input = input(f"{Fore.CYAN}Enter the maximum number of repositories to show [Press ENTER for default 250]: {Fore.RESET}")
print("")
try:
max_results = int(max_results_input) if max_results_input else 250
repositories, total_results = fetch_github_repositories(search_term, language, max_results)
if repositories:
found_count = min(len(repositories), total_results)
print(f"{Fore.LIGHTGREEN_EX}Found {total_results} repositories, displaying the first {found_count} repositories:")
print("")
display_repositories(repositories)
else:
print(f"{Fore.YELLOW}No repositories found.")
except ValueError:
print(f"{Fore.RED}Invalid input for maximum results. Please enter a number.")
time.sleep(3)
if sys.platform == "win32":
os.system('cls')
else:
os.system('clear')
elif search_mode == '2':
max_results_input = input(f"{Fore.CYAN}Enter the maximum number of users to show [Press ENTER for default 250]: {Fore.RESET}")
print("")
try:
max_results = int(max_results_input) if max_results_input else 250
users, total_results = fetch_github_users(search_term, max_results)
if users:
found_count = min(len(users), total_results)
if found_count == 1:
print(f"{Fore.LIGHTGREEN_EX}Found {total_results} user with the search term {Fore.BLUE}{search_term}{Fore.LIGHTGREEN_EX}, displaying the first/only user:")
else:
print(f"{Fore.LIGHTGREEN_EX}Found {total_results} users with the search term {Fore.BLUE}{search_term}{Fore.LIGHTGREEN_EX}, displaying the first {found_count} users:")
print("")
display_github_users(users)
else:
print(f"{Fore.YELLOW}No users found.")
except ValueError:
print(f"{Fore.RED}Invalid input for maximum results. Please enter a number.")
time.sleep(3)
if sys.platform == "win32":
os.system('cls')
else:
os.system('clear')
elif search_mode == '3':
language = input(f"{Fore.CYAN}Enter the programming language [optional]: {Fore.RESET}")
max_results_input = input(f"{Fore.CYAN}Enter the maximum number of repositories and users to show [Press ENTER for default 250]: {Fore.RESET}")
print("")
try:
max_results = int(max_results_input) if max_results_input else 250
repositories, repo_total = fetch_github_repositories(search_term, language, max_results)
users, user_total = fetch_github_users(search_term, max_results)
print(f"{Fore.LIGHTGREEN_EX}Found {repo_total} repositories and {user_total} users ")
print("")
if repositories:
found_count = min(len(repositories), repo_total)
print(f"{Fore.LIGHTGREEN_EX}Displaying the first {found_count} repositories:")
print("")
display_repositories(repositories)
else:
print(f"{Fore.YELLOW}No repositories found.")
print("=" * 70)
if users:
found_count = min(len(users), max_results)
if found_count == 1:
print(f"{Fore.LIGHTGREEN_EX}Found {total_results} user with the search term {Fore.BLUE}{search_term}{Fore.LIGHTGREEN_EX}, displaying the first/only user:")
else:
print(f"{Fore.LIGHTGREEN_EX}Found {total_results} users with the search term {Fore.BLUE}{search_term}{Fore.LIGHTGREEN_EX}, displaying the first {found_count} users:")
print("")
display_github_users(users)
else:
print(f"{Fore.YELLOW}No users found.")
print("")
except ValueError:
print(f"{Fore.RED}Invalid input for maximum results. Please enter a number.")
print('')
time.sleep(3)
if sys.platform == "win32":
os.system('cls')
else:
os.system('clear')
else:
print(f"{Fore.RED}Invalid input. Please enter 1, 2, or 3.")
time.sleep(3)
if sys.platform == "win32":
os.system('cls')
else:
os.system('clear')
# Ask if the user wants to perform another search or exit the program
exit_choice = input(f"{Fore.CYAN}Search query finished. Press Enter to exit or ['R'] to Reset Program: {Fore.RESET}")
exit_choice = exit_choice.upper()
if exit_choice != 'R':
break
else:
if sys.platform == "win32":
os.system('cls')
else:
os.system('clear')
print(f"{Fore.YELLOW}Thank you for using GitHunt. Goodbye!{Fore.RESET}")