|
| 1 | +import com.google.gson.Gson; |
| 2 | +import com.google.gson.JsonArray; |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.URI; |
| 5 | +import java.net.URISyntaxException; |
| 6 | +import java.net.http.HttpClient; |
| 7 | +import java.net.http.HttpRequest; |
| 8 | +import java.net.http.HttpResponse; |
| 9 | +import java.util.*; |
| 10 | + |
| 11 | +public class Main { |
| 12 | + |
| 13 | + /* |
| 14 | + * CLI to fetch the recent activity of a GitHub user and display it in the terminal. |
| 15 | + * https://github.com/ASJordi/gh-user-activity-cli |
| 16 | + */ |
| 17 | + |
| 18 | + public static void main(String[] args) { |
| 19 | + Main m = new Main("asjordi"); |
| 20 | + |
| 21 | + int gist = m.getNumGists(); |
| 22 | + int stars = m.getNumStars(); |
| 23 | + int followers = m.getNumFollowers(); |
| 24 | + int following = m.getNumFollowing(); |
| 25 | + int repos = m.getInfoRepos(); |
| 26 | + |
| 27 | + System.out.println("Stats for user: " + m.getUsuario()); |
| 28 | + System.out.println("Repos: " + repos); |
| 29 | + System.out.println("Gists: " + gist); |
| 30 | + System.out.println("Stars: " + stars); |
| 31 | + System.out.println("Followers: " + followers); |
| 32 | + System.out.println("Following: " + following); |
| 33 | + } |
| 34 | + |
| 35 | + private final String usuario; |
| 36 | + private Gson gson = new Gson(); |
| 37 | + private HttpClient httpClient = HttpClient.newHttpClient(); |
| 38 | + private HttpRequest request = null; |
| 39 | + |
| 40 | + public Main(String usuario) { |
| 41 | + this.usuario = usuario; |
| 42 | + } |
| 43 | + |
| 44 | + public int getInfoRepos() { |
| 45 | + String url = "https://api.github.com/users/" + this.usuario + "/repos?per_page=100"; |
| 46 | + Map<String, Integer> map = new HashMap<>(); |
| 47 | + |
| 48 | + HttpResponse<String> response = sendRequest(url); |
| 49 | + int statusCode = response.statusCode(); |
| 50 | + |
| 51 | + if (statusCode != 200) return -1; |
| 52 | + String responseBody = response.body(); |
| 53 | + JsonArray repos = gson.fromJson(responseBody, JsonArray.class); |
| 54 | + |
| 55 | + return repos.size(); |
| 56 | + } |
| 57 | + |
| 58 | + public int getNumFollowing() { |
| 59 | + String url = "https://api.github.com/users/" + this.usuario + "/following?per_page=100"; |
| 60 | + |
| 61 | + HttpResponse<String> response = sendRequest(url); |
| 62 | + int statusCode = response.statusCode(); |
| 63 | + |
| 64 | + if (statusCode != 200) return -1; |
| 65 | + String responseBody = response.body(); |
| 66 | + JsonArray following = gson.fromJson(responseBody, JsonArray.class); |
| 67 | + |
| 68 | + return following.size(); |
| 69 | + } |
| 70 | + |
| 71 | + public int getNumFollowers() { |
| 72 | + String url = "https://api.github.com/users/" + this.usuario + "/followers?per_page=100"; |
| 73 | + |
| 74 | + HttpResponse<String> response = sendRequest(url); |
| 75 | + int statusCode = response.statusCode(); |
| 76 | + |
| 77 | + if (statusCode != 200) return -1; |
| 78 | + String responseBody = response.body(); |
| 79 | + JsonArray followers = gson.fromJson(responseBody, JsonArray.class); |
| 80 | + |
| 81 | + return followers.size(); |
| 82 | + } |
| 83 | + |
| 84 | + public int getNumGists() { |
| 85 | + String url = "https://api.github.com/users/" + this.usuario + "/gists?per_page=100"; |
| 86 | + |
| 87 | + HttpResponse<String> response = sendRequest(url); |
| 88 | + int statusCode = response.statusCode(); |
| 89 | + |
| 90 | + if (statusCode != 200) return -1; |
| 91 | + String responseBody = response.body(); |
| 92 | + JsonArray gists = gson.fromJson(responseBody, JsonArray.class); |
| 93 | + |
| 94 | + return gists.size(); |
| 95 | + } |
| 96 | + |
| 97 | + public int getNumStars() { |
| 98 | + String url = "https://api.github.com/users/" + this.usuario + "/starred?per_page=100"; |
| 99 | + |
| 100 | + HttpResponse<String> response = sendRequest(url); |
| 101 | + int statusCode = response.statusCode(); |
| 102 | + |
| 103 | + if (statusCode != 200) return -1; |
| 104 | + String responseBody = response.body(); |
| 105 | + JsonArray stars = gson.fromJson(responseBody, JsonArray.class); |
| 106 | + |
| 107 | + return stars.size(); |
| 108 | + } |
| 109 | + |
| 110 | + private HttpResponse<String> sendRequest(String url) { |
| 111 | + HttpResponse<String> response = null; |
| 112 | + try { |
| 113 | + this.request = HttpRequest.newBuilder() |
| 114 | + .uri(new URI(url)) |
| 115 | + .GET() |
| 116 | + .build(); |
| 117 | + |
| 118 | + response = this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); |
| 119 | + } catch (URISyntaxException | IOException | InterruptedException e) { |
| 120 | + throw new RuntimeException(e); |
| 121 | + } |
| 122 | + |
| 123 | + return response; |
| 124 | + } |
| 125 | + |
| 126 | + public String getUsuario() { |
| 127 | + return this.usuario; |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments