use github api for contributors (d1gress)

GitHub API impl for contributors
This commit is contained in:
dewy 2020-04-13 13:28:14 +01:00 committed by GitHub
commit 9ff732ed33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 144 additions and 38 deletions

View File

@ -1,12 +1,18 @@
package me.zeroeightsix.kami.command.commands; package me.zeroeightsix.kami.command.commands;
import me.zeroeightsix.kami.command.Command; import me.zeroeightsix.kami.command.Command;
import me.zeroeightsix.kami.util.WebUtils;
import java.util.Arrays;
import java.util.List;
import static me.zeroeightsix.kami.util.MessageSendHelper.sendChatMessage; import static me.zeroeightsix.kami.util.MessageSendHelper.sendChatMessage;
/** /**
* Created by S-B99 on 01/12/2019. * Created by S-B99 on 01/12/2019.
* Revamped by d1gress/Qther on 13 April 2020
*/ */
public class CreditsCommand extends Command { public class CreditsCommand extends Command {
public CreditsCommand() { public CreditsCommand() {
@ -16,43 +22,62 @@ public class CreditsCommand extends Command {
@Override @Override
public void call(String[] args) { public void call(String[] args) {
sendChatMessage("\n" + List<Integer> exceptions = Arrays.asList(
"Name (Github if not same as name)\n" + 17222512, // s-b99 / dominika
"&l&9Author:\n" + 27009727, // zeroeightysix
"086 (zeroeightysix)\n" + 48992448, // blockparole
"&l&9Contributors:\n" + 19880089, // EmotionalLove
"Bella (S-B99)\n" + 55198830, 24369412, // d1gress and Vonr
"hub (blockparole)\n" + 51212427, // Cuhnt
"Sasha (EmotionalLove)\n" + 11698651, // jacksonellworth03
"Qther (d1gress / Vonr)\n" + 44139104, // TheBritishMidget
"HHSGPA\n" + 59456376, // Hamburger2k
"20kdc\n" + 41800112, // PretendingToCode
"IronException\n" + 52386117, // Bluskript
"cats (Cuhnt)\n" + 26636167, // AnotherEntity
"Katatje\n" + 22961592, // ArisaSnowbell
"Deauthorized\n" + 13212688, // jamie27
"snowmii\n" + 50775247, // DarkiBoi
"kdb424\n" + 12820770, // Babbaj
"Jack (jacksonellsworth03)\n" + 11377481, // Crystallinqq
"cookiedragon234\n" + 3837873, // leijurv
"0x2E (PretendingToCode)\n" + 49104462, // Elementars
"babbaj\n" + 56689414, // WaizyNet
"ZeroMemes\n" + 58238984, // Itistheend
"TheBritishMidget (TBM)\n" +
"Hamburger (Hamburger2k)\n" + // Bots
"Darki\n" + 27856297 // dependabot
"Crystallinqq\n" + );
"Elementars\n" + String message =
"fsck\n" + "\nName (Github if not same as name)" +
"Jamie (jamie27)\n" + "\n&l&9Author:" +
"Waizy\n" + "\n086 (zeroeightysix)" +
"It is the end\n" + "\n&l&9Contributors:" +
"fluffcq\n" + "\nBella (S-B99)" +
"leijurv\n" + "\nhub (blockparole)" +
"polymer\n" + "\nSasha (EmotionalLove)" +
"Battery Settings (Bluskript)\n" + "\nQther (d1gress / Vonr)" +
"An-En (AnotherEntity)\n" + "\ncats (Cuhnt)" +
"Arisa (Arisa-Snowbell)\n" + "\nJack (jacksonellsworth03)" +
"UrM0ther"); "\nTheBritishMidget (TBM)" +
"\nHamburger (Hamburger2k)" +
"\n0x2E (PretendingToCode)" +
"\nBattery Settings (Bluskript)" +
"\nAn-En (AnotherEntity)" +
"\nArisa (Arisa-Snowbell)" +
"\nJamie (jamie27)" +
"\nDarki (DarkiBoi)" +
"\nWaizy (WaizyNet)" +
"\nIt is the end (Itistheend)" +
"\nbabbaj" +
"\nCrystallinqq" +
"\nleijurv" +
"\nElementars";
for (WebUtils.GithubUser u : WebUtils.getContributors(exceptions)) {
message = message.concat("\n" + u.login);
}
sendChatMessage(message);
} }
} }

View File

@ -1,13 +1,21 @@
package me.zeroeightsix.kami.util; package me.zeroeightsix.kami.util;
import com.google.gson.Gson;
import me.zeroeightsix.kami.KamiMod; import me.zeroeightsix.kami.KamiMod;
import javax.net.ssl.HttpsURLConnection;
import java.awt.*; import java.awt.*;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI; import java.net.URI;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
/** /**
* Created by Dewy on 09/04/2020 * Created by Dewy on 09/04/2020
* Updated by d1gress/Qther on 13 April 2020
*/ */
public class WebUtils { public class WebUtils {
@ -18,4 +26,77 @@ public class WebUtils {
KamiMod.log.error("Couldn't open link: " + url.toString()); KamiMod.log.error("Couldn't open link: " + url.toString());
} }
} }
public static java.util.List<GithubUser> getContributors() {
// log attempt
KamiMod.log.info("Attempting to get contributors from github api...");
//initialize list
java.util.List<GithubUser> contributorsAsList = new LinkedList<>(Collections.emptyList());
try {
// connect to https://api.github.com/repos/kami-blue/client/contributors
HttpsURLConnection connection = (HttpsURLConnection) new URL(KamiMod.CAPES_JSON).openConnection();
connection.connect();
// then parse it
GithubUser[] contributors = new Gson().fromJson(new InputStreamReader(connection.getInputStream()), GithubUser[].class);
// disconnect from api
connection.disconnect();
// add contributors to the list
contributorsAsList.addAll(Arrays.asList(contributors));
} catch (Throwable t) {
// throw error
KamiMod.log.error("Attempt to get contributors from github api failed.\nError:\n\n" + t.toString());
}
return contributorsAsList;
}
public static java.util.List<GithubUser> getContributors(java.util.List<Integer> exceptions) {
// log attempt
KamiMod.log.info("Attempting to get contributors from github api...");
//initialize list
java.util.List<GithubUser> contributorsAsList = new LinkedList<>(Collections.emptyList());
try {
// connect to https://api.github.com/repos/kami-blue/client/contributors
HttpsURLConnection connection = (HttpsURLConnection) new URL("https://api.github.com/repos/kami-blue/client/contributors").openConnection();
connection.connect();
// then parse it
GithubUser[] contributors = new Gson().fromJson(new InputStreamReader(connection.getInputStream()), GithubUser[].class);
// disconnect from api
connection.disconnect();
// add contributors to the list
for (GithubUser githubUser : contributors) {
contributorsAsList.add(githubUser);
for (int exception : exceptions) {
if (githubUser.id == exception) {
contributorsAsList.remove(githubUser);
}
}
}
} catch (Throwable t) {
// throw error
KamiMod.log.error("Attempt to get contributors from github api failed.\nError:\n\n" + t.toString());
MessageSendHelper.sendErrorMessage("Attempt to get contributors from github api failed.\nError:\n\n" + t.toString());
}
return contributorsAsList;
}
public class GithubUser {
public String login;
public int id;
public String contributions;
}
} }