Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.google.gson.Gson;
- import com.google.gson.JsonElement;
- import com.google.gson.JsonObject;
- import org.apache.http.Header;
- import org.apache.http.HttpHost;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpUriRequest;
- import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
- import org.apache.http.impl.client.HttpClientBuilder;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
- import org.apache.http.ssl.SSLContextBuilder;
- import org.apache.poi.hssf.usermodel.HSSFRow;
- import org.apache.poi.hssf.usermodel.HSSFSheet;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.poifs.filesystem.POIFSFileSystem;
- import java.io.BufferedReader;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- import java.util.function.Function;
- import java.util.stream.Collectors;
- /**
- * Created by Lotem on Mar-18.
- */
- public class Main {
- private static final String FILE_PATH = "C:\\Users\\Lotem\\Desktop\\giveaway.xls"; // make sure it's .xls and not .xlsx
- private static final int FORUMS_NAME_COL = 1;
- private static final int IGN_COL = 2;
- private static final int RAFFLE_CHOICE_COL = 4;
- private static final String WYNN_API_URL = "https://api.wynncraft.com/forums/getForumId/";
- private static final String WYNN_FORUM_MEMBER_PAGE_URL = "https://forums.wynncraft.com/members/";
- private static final String FORUMS_REDIRECT_REGEX = "https://forums.wynncraft.com/members/%s.%d/";
- private static final String COOKIES = ""; // ADD YOUR FORUMS COOKIES HERE!!!
- private static class Participant {
- private int rowNum;
- private String ign;
- private String forumsName;
- private boolean inBigRaffle;
- }
- private static HttpClient client;
- public static HttpClient createClient() {
- try {
- SSLContextBuilder builder = new SSLContextBuilder();
- builder.useProtocol("TLSv1.2");
- builder.loadTrustMaterial(null, (chain, authType) -> true);
- SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
- HttpClientBuilder hcBuilder = HttpClients.custom();
- HttpHost httpProxy = new HttpHost("localhost", 8083);
- DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(httpProxy);
- hcBuilder.setRoutePlanner(routePlanner);
- return hcBuilder.disableCookieManagement().setSSLSocketFactory(sslsf).build();
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- private static String getResponseContent(HttpUriRequest request) throws IOException {
- HttpResponse response = client.execute(request);
- BufferedReader rd = new BufferedReader(
- new InputStreamReader(response.getEntity().getContent()));
- StringBuilder requestContent = new StringBuilder();
- String line;
- while ((line = rd.readLine()) != null) {
- requestContent.append(line);
- }
- return requestContent.toString();
- }
- private static String getForumsRedirectById(int id) throws IOException {
- // check on the forums
- HttpGet forumsRequest = new HttpGet(WYNN_FORUM_MEMBER_PAGE_URL + id);
- forumsRequest.removeHeaders("Cookie");
- forumsRequest.addHeader("Host", "forums.wynncraft.com");
- forumsRequest.addHeader("Connection", "close");
- forumsRequest.addHeader("Upgrade-Insecure-Requests", "1");
- forumsRequest.addHeader("User-Agent", "Mozilla/5.0 (Linux; Android 6.0.1; ONE E1005 Build/MMB29M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.109 Mobile Safari/537.36");
- forumsRequest.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
- forumsRequest.addHeader("Accept-Encoding", "gzip, deflate");
- forumsRequest.addHeader("Accept-Language", "en-US,en;q=0.9,he;q=0.8");
- forumsRequest.setHeader("Cookie", COOKIES);
- forumsRequest.setParams(forumsRequest.getParams().setParameter("http.protocol.handle-redirects", false));
- HttpResponse response = client.execute(forumsRequest);
- Header location = response.getFirstHeader("Location");
- return location == null ? null : location.getValue();
- }
- private static boolean isValidPlayer(Participant p) throws IOException {
- String responseStr = null;
- responseStr = getResponseContent(new HttpGet(WYNN_API_URL + p.ign));
- JsonObject json = new Gson().fromJson(responseStr, JsonElement.class).getAsJsonObject();
- if (json.keySet().contains("error")) {
- System.out.println(String.format("[---] %d. %s (Forums: %s) is not a real player.", p.rowNum, p.ign, p.forumsName));
- return false;
- }
- JsonElement username = json.get("username");
- if (username == null) {
- System.out.println(String.format("[---] %d. %s (Forums: %s) have no forum account linked.", p.rowNum, p.ign, p.forumsName));
- return false;
- }
- String pUsername = p.forumsName.toLowerCase().replace(' ', '-');
- String usernameSrt = username.getAsString().toLowerCase();
- int userId = json.get("id").getAsInt();
- if (!usernameSrt.equals(pUsername)) {
- String location = null;
- location = getForumsRedirectById(userId);
- if (location == null || !location.matches(String.format(FORUMS_REDIRECT_REGEX, pUsername, userId))) {
- System.out.println(String.format(
- "[---] %d. %s (Forums: %s) is not linked. The username is \"%s\".", p.rowNum, p.ign, p.forumsName, usernameSrt));
- return false;
- } else {
- System.out.println("DEBUG NOTE!");
- }
- }
- System.out.println(String.format("[+] %d. %s (Forums: %s) is a valid player.", p.rowNum, p.ign, p.forumsName));
- return true;
- }
- private static <T, R> boolean showPropertyDupes(Collection<T> all, Function<T, R> getProp, String type) {
- Set<R> dupes = getDuplicate(all.stream().map(getProp).collect(Collectors.toList()));
- if (dupes.size() > 0) {
- System.out.println("[-] There are " + type + " duplicates:");
- for (R dup : dupes) {
- System.out.println("\t" + dup);
- }
- System.out.println();
- return true;
- }
- return false;
- }
- private static <T> Set<T> getDuplicate(Collection<T> all) {
- Set<T> allSet = new HashSet<>();
- Set<T> dupes = new HashSet<>();
- all.forEach(item -> {
- if (!allSet.add(item)) {
- dupes.add(item);
- }
- });
- return dupes;
- }
- public static void main(String[] args) throws Exception {
- client = createClient();
- POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(FILE_PATH));
- HSSFWorkbook wb = new HSSFWorkbook(fs);
- HSSFSheet sheet = wb.getSheetAt(0);
- int rows = sheet.getPhysicalNumberOfRows();
- List<Participant> participants = new ArrayList<>();
- // starting from row 1. row 0 contains the column titles
- for (int r = 1; r < rows; r++) {
- HSSFRow row = sheet.getRow(r);
- Participant p = new Participant();
- p.rowNum = r;
- p.forumsName = row.getCell(FORUMS_NAME_COL).getStringCellValue().trim();
- p.ign = row.getCell(IGN_COL).getStringCellValue().trim();
- p.inBigRaffle = row.getCell(RAFFLE_CHOICE_COL).getStringCellValue().contains("128");
- participants.add(p);
- }
- boolean containsDupes = showPropertyDupes(participants, o -> o.ign, "IGN");
- containsDupes |= showPropertyDupes(participants, o -> o.forumsName, "forum name");
- boolean allValid = true;
- for (Participant p : participants) {
- if (!isValidPlayer(p)) {
- allValid = false;
- }
- }
- if (containsDupes || !allValid) {
- System.out.println("[-] The excel file should be fixed manually before the winners are chosen.");
- return;
- }
- System.out.println("[+] Ready to go!");
- System.out.println("[*] Selecting winners...\n");
- List<Participant> smalls = participants.stream().filter(p -> !p.inBigRaffle).collect(Collectors.toList());
- Participant smallWinner = smalls.get(new Random().nextInt(smalls.size()));
- System.out.println(String.format("Out of %d participants, the winner of the small raffle is... %s!!!",
- smalls.size(), smallWinner.ign));
- List<Participant> bigs = participants.stream().filter(p -> p.inBigRaffle).collect(Collectors.toList());
- Participant bigWinner = bigs.get(new Random().nextInt(bigs.size()));
- System.out.println(String.format("Out of %d participants, the winner of the small raffle is... %s!!!",
- bigs.size(), bigWinner.ign));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment