-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_search.cpp
More file actions
444 lines (352 loc) · 8.83 KB
/
github_search.cpp
File metadata and controls
444 lines (352 loc) · 8.83 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
========================================================
GitHub Advanced Search CLI PRO v4
Language : C++
Library : libcurl + nlohmann/json
UPGRADE v4:
- Better stable version
- Proper min/max repo size filter
- Multiple topics support
- Better formatted output
- Input validation
- Retry-safe curl setup
- Timeout handling
- Total repository count
- Clean terminal UX
- Optional sort/order
- Only repository name required
INSTALL:
sudo apt install libcurl4-openssl-dev
sudo apt install nlohmann-json3-dev
COMPILE:
g++ github_search.cpp -o ghsearch -lcurl
RUN:
./ghsearch
========================================================
*/
/*
RX Search
Copyright (C) 2026 Reno
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License v3.0.
See the LICENSE file for more details.
*/
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
#include <curl/curl.h>
#include <nlohmann/json.hpp>
using namespace std;
using json = nlohmann::json;
/*
========================================================
CURL CALLBACK
========================================================
*/
size_t WriteCallback(void* contents, size_t size, size_t nmemb, string* output)
{
size_t total = size * nmemb;
output->append((char*)contents, total);
return total;
}
/*
========================================================
HELPERS
========================================================
*/
void line()
{
cout << "============================================================\n";
}
bool isNumber(const string& s)
{
if (s.empty()) return false;
for (char c : s)
{
if (!isdigit(c))
return false;
}
return true;
}
string encodeQuery(const string& str)
{
string result;
for (char c : str)
{
if (c == ' ')
result += "%20";
else
result += c;
}
return result;
}
vector<string> splitTopics(const string& input)
{
vector<string> topics;
string word;
stringstream ss(input);
while (ss >> word)
{
topics.push_back(word);
}
return topics;
}
/*
========================================================
PRETTY PRINT
========================================================
*/
void printRepo(const json& repo, int index)
{
cout << "[" << index << "] "
<< repo.value("name", "N/A")
<< "\n\n";
cout << left << setw(15) << "Language"
<< ": "
<< repo.value("language", "N/A")
<< "\n";
cout << left << setw(15) << "Stars"
<< ": "
<< repo.value("stargazers_count", 0)
<< "\n";
cout << left << setw(15) << "Forks"
<< ": "
<< repo.value("forks_count", 0)
<< "\n";
cout << left << setw(15) << "Size"
<< ": "
<< repo.value("size", 0)
<< " KB\n";
cout << left << setw(15) << "Updated"
<< ": "
<< repo.value("updated_at", "N/A")
<< "\n";
cout << left << setw(15) << "URL"
<< ": "
<< repo.value("html_url", "N/A")
<< "\n";
cout << "\nDescription:\n";
if (repo["description"].is_null())
cout << "No description\n";
else
cout << repo.value("description", "No description") << "\n";
cout << "\n------------------------------------------------------------\n\n";
}
void printResults(const string& response)
{
try
{
json data = json::parse(response);
if (data.contains("message") &&
data["message"] == "Validation Failed")
{
line();
cout << "GitHub API Validation Error\n";
line();
cout << data.dump(4) << "\n";
return;
}
if (data.contains("message") &&
data["message"] == "API rate limit exceeded")
{
line();
cout << "GitHub API Rate Limit Exceeded\n";
line();
cout << "Gunakan GitHub Token di versi berikutnya.\n";
return;
}
int total = data.value("total_count", 0);
line();
cout << "Total Repository Found : " << total << "\n";
line();
cout << "\n";
if (!data.contains("items") || data["items"].empty())
{
cout << "No repositories found.\n";
return;
}
int index = 1;
for (const auto& repo : data["items"])
{
printRepo(repo, index++);
}
}
catch (exception& e)
{
line();
cout << "JSON Parse Error\n";
line();
cout << e.what() << "\n";
}
}
/*
========================================================
MAIN
========================================================
*/
int main()
{
string keyword;
string language;
string stars;
string forks;
string minSize;
string maxSize;
string topicInput;
string updated;
string sort;
string order;
line();
cout << " GitHub Advanced Search CLI PRO v4\n";
line();
cout << "\n";
/*
INPUT
*/
cout << "[1] Repository Name / Keyword (required): ";
getline(cin, keyword);
if (keyword.empty())
{
cout << "\n[ERROR] Repository name wajib diisi.\n";
return 1;
}
cout << "[2] Language (optional | C/C++/Python/etc): ";
getline(cin, language);
cout << "[3] Minimum Stars (optional | number only): ";
getline(cin, stars);
cout << "[4] Minimum Forks (optional | number only): ";
getline(cin, forks);
cout << "[5] Min Repo Size KB (optional | ex: 1): ";
getline(cin, minSize);
cout << "[6] Max Repo Size KB (optional | ex: 100): ";
getline(cin, maxSize);
cout << "[7] Topics (optional | ex: compiler interpreter): ";
getline(cin, topicInput);
cout << "[8] Updated After (optional | YYYY-MM-DD): ";
getline(cin, updated);
cout << "[9] Sort (optional | stars/forks/updated): ";
getline(cin, sort);
cout << "[10] Order (optional | desc/asc): ";
getline(cin, order);
/*
BASIC VALIDATION
*/
if (!stars.empty() && !isNumber(stars))
{
cout << "\n[ERROR] Stars harus angka.\n";
return 1;
}
if (!forks.empty() && !isNumber(forks))
{
cout << "\n[ERROR] Forks harus angka.\n";
return 1;
}
if (!minSize.empty() && !isNumber(minSize))
{
cout << "\n[ERROR] Min size harus angka.\n";
return 1;
}
if (!maxSize.empty() && !isNumber(maxSize))
{
cout << "\n[ERROR] Max size harus angka.\n";
return 1;
}
/*
BUILD QUERY
*/
string query = keyword;
if (!language.empty())
query += "+language:" + language;
if (!stars.empty())
query += "+stars:>=" + stars;
if (!forks.empty())
query += "+forks:>=" + forks;
/*
Proper size range
*/
if (!minSize.empty() && !maxSize.empty())
{
query += "+size:" + minSize + ".." + maxSize;
}
else if (!minSize.empty())
{
query += "+size:>=" + minSize;
}
else if (!maxSize.empty())
{
query += "+size:<=" + maxSize;
}
/*
Multi topic support
*/
vector<string> topics = splitTopics(topicInput);
for (const auto& topic : topics)
{
query += "+topic:" + topic;
}
if (!updated.empty())
query += "+pushed:>=" + updated;
query = encodeQuery(query);
/*
BUILD URL
*/
string url =
"https://api.github.com/search/repositories?q=" + query;
if (!sort.empty())
url += "&sort=" + sort;
if (!order.empty())
url += "&order=" + order;
url += "&per_page=10";
/*
CURL SETUP
*/
CURL* curl;
CURLcode res;
string response;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (!curl)
{
cout << "\n[ERROR] Failed init CURL.\n";
return 1;
}
struct curl_slist* headers = NULL;
headers = curl_slist_append(
headers,
"User-Agent: GitHubSearchCLI"
);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
/*
stability improvements
*/
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 15L);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
cout << "\n";
line();
cout << "Fetching repositories from GitHub...\n";
line();
cout << "\n";
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
cerr << "[CURL ERROR] "
<< curl_easy_strerror(res)
<< "\n";
}
else
{
printResults(response);
}
/*
CLEANUP
*/
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
curl_global_cleanup();
return 0;
}