Skip to content

Commit cdafa15

Browse files
added search-user and renamed query to search-repository
1 parent ef7cbe8 commit cdafa15

3 files changed

Lines changed: 123 additions & 18 deletions

File tree

src/main/java/com/francescodisalesdev/gitcli/GitCli.java

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.francescodisalesdev.gitcli;
22

33
import com.francescodisalesdev.gitcli.utility.ErrorMessages;
4+
import com.francescodisalesdev.gitcli.utility.SystemMessages;
45
import org.json.simple.parser.ParseException;
56
import org.springframework.boot.autoconfigure.info.ProjectInfoProperties;
67
import org.springframework.shell.standard.ShellComponent;
@@ -9,15 +10,17 @@
910
import org.springframework.shell.standard.ShellOption;
1011

1112
import java.io.IOException;
13+
import java.util.Iterator;
1214
import java.util.List;
15+
import java.util.Set;
1316

1417

1518
@ShellComponent
1619
class GitCli
1720
{
1821

1922
@ShellMethod("search for a git repository")
20-
public void query(String repository,@ShellOption(defaultValue = "1") int page)
23+
public void searchRepository(String repository,@ShellOption(defaultValue = "1") int page)
2124
{
2225
try
2326
{
@@ -28,7 +31,21 @@ public void query(String repository,@ShellOption(defaultValue = "1") int page)
2831
if(result==null)
2932
System.out.println("page number must be bigger than 0");
3033
else
31-
gitService.filterPage(repository,result);
34+
{
35+
Iterator responseIterator = result.iterator();
36+
37+
while(responseIterator.hasNext())
38+
{
39+
System.out.println(responseIterator.next());
40+
}
41+
42+
int pagesRepository = gitService.getPages(repository);
43+
44+
if(pagesRepository>1)
45+
System.out.println(SystemMessages.TOTAL_PAGES.toString()+pagesRepository);
46+
47+
}
48+
3249

3350
}
3451
catch(IOException | ParseException e)
@@ -97,6 +114,7 @@ public void info(String repository)
97114
try
98115
{
99116
gitService.getInfoRepository(repository);
117+
100118
}
101119
catch (IOException e)
102120
{
@@ -139,5 +157,36 @@ public void checkFile(String path)
139157

140158
}
141159

160+
@ShellMethod("search for a specific user")
161+
public void searchUser(String username,@ShellOption(defaultValue = "1") int page)
162+
{
163+
GitService gitService = new GitService();
164+
165+
try
166+
{
167+
Set<String> usersResult = gitService.searchUserService(username,page);
168+
169+
if(usersResult == null)
170+
System.out.println(ErrorMessages.USER_NOT_FOUND);
171+
else
172+
{
173+
Iterator iterator = usersResult.iterator();
174+
175+
while(iterator.hasNext())
176+
System.out.println(iterator.next());
177+
}
178+
179+
int pages = gitService.getUserPages(username);
180+
if(pages > 1)
181+
System.out.println("total pages "+pages);
182+
183+
}
184+
catch (IOException | ParseException e)
185+
{
186+
System.out.println(ErrorMessages.SOMETHING_BAD);
187+
System.out.println(e.getMessage());
188+
}
189+
190+
}
142191

143192
}

src/main/java/com/francescodisalesdev/gitcli/service/GitService.java

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,17 @@ public List<String> getResult(String param,int page) throws IOException, ParseEx
6767
return repo;
6868
}
6969

70-
public void getPages(String param) throws IOException
70+
public int getPages(String param) throws IOException
7171
{
7272
String URL = "https://github.com/search?q="+param;
7373
Document document = Jsoup.connect(URL).get();
7474
Elements elementsLinks = document.select("em");
7575

76-
System.out.println(elementsLinks.attr("data-total-pages").toString());
76+
int pagesValue = Integer.parseInt(elementsLinks.attr("data-total-pages").toString());
77+
return pagesValue;
7778

7879
}
7980

80-
public void filterPage(String param,List<String> response) throws IOException
81-
{
82-
83-
Iterator responseIterator = response.iterator();
84-
85-
while(responseIterator.hasNext())
86-
{
87-
System.out.println(responseIterator.next());
88-
}
89-
90-
System.out.println(SystemMessages.TOTAL_PAGES.toString());
91-
this.getPages(param);
92-
93-
}
9481

9582
public void cloneRepository(String repository,String localPath) throws IOException, InterruptedException
9683
{
@@ -200,4 +187,66 @@ public void checkFileService(String path) throws IOException
200187
}
201188
}
202189

190+
public Set<String> searchUserService(String username,int page) throws IOException,ParseException
191+
{
192+
193+
Set<String> users = new HashSet<String>();
194+
195+
String URL;
196+
197+
if(username.isEmpty())
198+
return null;
199+
200+
if(page == 0 || page < 1)
201+
return null;
202+
203+
if(page == 1)
204+
URL = "https://github.com/search?q="+username+"&type=users";
205+
else
206+
URL = "https://github.com/search?p="+page+"&q="+username+"&type=Users";
207+
208+
Document document = Jsoup.connect(URL).get();
209+
Elements elements = document.select("a");
210+
211+
for(Element element : elements)
212+
{
213+
String repositoryJSON = element.attr("data-hydro-click").toString();
214+
JSONParser jsonParser = new JSONParser();
215+
216+
if(!repositoryJSON.isBlank())
217+
{
218+
JSONObject jsonObject = (JSONObject) jsonParser.parse(repositoryJSON);
219+
220+
String payload = jsonObject.get("payload").toString();
221+
JSONObject result = (JSONObject) jsonParser.parse(payload);
222+
223+
if(result.containsKey("result"))
224+
{
225+
String resultString = result.get("result").toString();
226+
JSONObject three = (JSONObject) jsonParser.parse(resultString);
227+
228+
String urlString = three.get("url").toString();
229+
users.add(urlString);
230+
}
231+
232+
}
233+
234+
}
235+
236+
return users;
237+
}
238+
239+
public int getUserPages(String username) throws IOException
240+
{
241+
String URL = URL = "https://github.com/search?q="+username+"&type=users";
242+
243+
Document document = Jsoup.connect(URL).get();
244+
Elements elementsLinks = document.select("em");
245+
246+
int value = Integer.parseInt(elementsLinks.attr("data-total-pages").toString());
247+
return value;
248+
249+
}
250+
251+
203252
}

src/main/java/com/francescodisalesdev/gitcli/utility/ErrorMessages.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,12 @@ public String toString() {
2424
}
2525

2626

27+
},
28+
USER_NOT_FOUND
29+
{
30+
@Override
31+
public String toString() {
32+
return "User not found";
33+
}
2734
}
2835
}

0 commit comments

Comments
 (0)