Guest User

Lotem's Giveaway Validator

a guest
Mar 17th, 2018
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.25 KB | None | 0 0
  1. import com.google.gson.Gson;
  2. import com.google.gson.JsonElement;
  3. import com.google.gson.JsonObject;
  4. import org.apache.http.Header;
  5. import org.apache.http.HttpHost;
  6. import org.apache.http.HttpResponse;
  7. import org.apache.http.client.HttpClient;
  8. import org.apache.http.client.methods.HttpGet;
  9. import org.apache.http.client.methods.HttpUriRequest;
  10. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  11. import org.apache.http.impl.client.HttpClientBuilder;
  12. import org.apache.http.impl.client.HttpClients;
  13. import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
  14. import org.apache.http.ssl.SSLContextBuilder;
  15. import org.apache.poi.hssf.usermodel.HSSFRow;
  16. import org.apache.poi.hssf.usermodel.HSSFSheet;
  17. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  18. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  19.  
  20. import java.io.BufferedReader;
  21. import java.io.FileInputStream;
  22. import java.io.IOException;
  23. import java.io.InputStreamReader;
  24. import java.util.*;
  25. import java.util.function.Function;
  26. import java.util.stream.Collectors;
  27.  
  28.  
  29. /**
  30.  * Created by Lotem on Mar-18.
  31.  */
  32. public class Main {
  33.  
  34.     private static final String FILE_PATH = "C:\\Users\\Lotem\\Desktop\\giveaway.xls"; // make sure it's .xls and not .xlsx
  35.     private static final int FORUMS_NAME_COL = 1;
  36.     private static final int IGN_COL = 2;
  37.     private static final int RAFFLE_CHOICE_COL = 4;
  38.  
  39.     private static final String WYNN_API_URL = "https://api.wynncraft.com/forums/getForumId/";
  40.     private static final String WYNN_FORUM_MEMBER_PAGE_URL = "https://forums.wynncraft.com/members/";
  41.     private static final String FORUMS_REDIRECT_REGEX = "https://forums.wynncraft.com/members/%s.%d/";
  42.  
  43.     private static final String COOKIES =  ""; // ADD YOUR FORUMS COOKIES HERE!!!
  44.  
  45.     private static class Participant {
  46.         private int rowNum;
  47.         private String ign;
  48.         private String forumsName;
  49.         private boolean inBigRaffle;
  50.     }
  51.  
  52.     private static HttpClient client;
  53.  
  54.     public static HttpClient createClient() {
  55.         try {
  56.             SSLContextBuilder builder = new SSLContextBuilder();
  57.             builder.useProtocol("TLSv1.2");
  58.             builder.loadTrustMaterial(null, (chain, authType) -> true);
  59.             SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
  60.  
  61.             HttpClientBuilder hcBuilder = HttpClients.custom();
  62.             HttpHost httpProxy = new HttpHost("localhost", 8083);
  63.             DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(httpProxy);
  64.             hcBuilder.setRoutePlanner(routePlanner);
  65.  
  66.             return hcBuilder.disableCookieManagement().setSSLSocketFactory(sslsf).build();
  67.         } catch (Exception e) {
  68.             throw new RuntimeException(e);
  69.         }
  70.     }
  71.  
  72.     private static String getResponseContent(HttpUriRequest request) throws IOException {
  73.         HttpResponse response = client.execute(request);
  74.  
  75.         BufferedReader rd = new BufferedReader(
  76.                 new InputStreamReader(response.getEntity().getContent()));
  77.  
  78.         StringBuilder requestContent = new StringBuilder();
  79.         String line;
  80.         while ((line = rd.readLine()) != null) {
  81.             requestContent.append(line);
  82.         }
  83.  
  84.         return requestContent.toString();
  85.     }
  86.  
  87.     private static String getForumsRedirectById(int id) throws IOException {
  88.         // check on the forums
  89.         HttpGet forumsRequest = new HttpGet(WYNN_FORUM_MEMBER_PAGE_URL + id);
  90.         forumsRequest.removeHeaders("Cookie");
  91.         forumsRequest.addHeader("Host", "forums.wynncraft.com");
  92.         forumsRequest.addHeader("Connection", "close");
  93.         forumsRequest.addHeader("Upgrade-Insecure-Requests", "1");
  94.         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");
  95.         forumsRequest.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
  96.         forumsRequest.addHeader("Accept-Encoding", "gzip, deflate");
  97.         forumsRequest.addHeader("Accept-Language", "en-US,en;q=0.9,he;q=0.8");
  98.         forumsRequest.setHeader("Cookie", COOKIES);
  99.  
  100.         forumsRequest.setParams(forumsRequest.getParams().setParameter("http.protocol.handle-redirects", false));
  101.  
  102.         HttpResponse response = client.execute(forumsRequest);
  103.         Header location = response.getFirstHeader("Location");
  104.  
  105.         return location == null ? null : location.getValue();
  106.     }
  107.  
  108.     private static boolean isValidPlayer(Participant p) throws IOException {
  109.         String responseStr = null;
  110.         responseStr = getResponseContent(new HttpGet(WYNN_API_URL + p.ign));
  111.         JsonObject json = new Gson().fromJson(responseStr, JsonElement.class).getAsJsonObject();
  112.  
  113.         if (json.keySet().contains("error")) {
  114.             System.out.println(String.format("[---] %d. %s (Forums: %s) is not a real player.", p.rowNum, p.ign, p.forumsName));
  115.             return false;
  116.         }
  117.  
  118.         JsonElement username = json.get("username");
  119.  
  120.         if (username == null) {
  121.             System.out.println(String.format("[---] %d. %s (Forums: %s) have no forum account linked.", p.rowNum, p.ign, p.forumsName));
  122.             return false;
  123.         }
  124.  
  125.         String pUsername = p.forumsName.toLowerCase().replace(' ', '-');
  126.         String usernameSrt = username.getAsString().toLowerCase();
  127.         int userId = json.get("id").getAsInt();
  128.  
  129.         if (!usernameSrt.equals(pUsername)) {
  130.             String location = null;
  131.             location = getForumsRedirectById(userId);
  132.  
  133.             if (location == null || !location.matches(String.format(FORUMS_REDIRECT_REGEX, pUsername, userId))) {
  134.                 System.out.println(String.format(
  135.                         "[---] %d. %s (Forums: %s) is not linked. The username is \"%s\".", p.rowNum, p.ign, p.forumsName, usernameSrt));
  136.                 return false;
  137.             } else {
  138.                 System.out.println("DEBUG NOTE!");
  139.             }
  140.         }
  141.  
  142.         System.out.println(String.format("[+] %d. %s (Forums: %s) is a valid player.", p.rowNum, p.ign, p.forumsName));
  143.         return true;
  144.     }
  145.  
  146.     private static <T, R> boolean showPropertyDupes(Collection<T> all, Function<T, R> getProp, String type) {
  147.         Set<R> dupes = getDuplicate(all.stream().map(getProp).collect(Collectors.toList()));
  148.  
  149.         if (dupes.size() > 0) {
  150.             System.out.println("[-] There are " + type + " duplicates:");
  151.             for (R dup : dupes) {
  152.                 System.out.println("\t" + dup);
  153.             }
  154.             System.out.println();
  155.  
  156.             return true;
  157.         }
  158.  
  159.         return false;
  160.     }
  161.  
  162.     private static <T> Set<T> getDuplicate(Collection<T> all) {
  163.         Set<T> allSet = new HashSet<>();
  164.         Set<T> dupes = new HashSet<>();
  165.  
  166.         all.forEach(item -> {
  167.             if (!allSet.add(item)) {
  168.                 dupes.add(item);
  169.             }
  170.         });
  171.  
  172.         return dupes;
  173.     }
  174.  
  175.     public static void main(String[] args) throws Exception {
  176.         client = createClient();
  177.  
  178.         POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(FILE_PATH));
  179.         HSSFWorkbook wb = new HSSFWorkbook(fs);
  180.         HSSFSheet sheet = wb.getSheetAt(0);
  181.  
  182.         int rows = sheet.getPhysicalNumberOfRows();
  183.  
  184.         List<Participant> participants = new ArrayList<>();
  185.  
  186.         // starting from row 1. row 0 contains the column titles
  187.         for (int r = 1; r < rows; r++) {
  188.             HSSFRow row = sheet.getRow(r);
  189.  
  190.             Participant p = new Participant();
  191.             p.rowNum = r;
  192.             p.forumsName = row.getCell(FORUMS_NAME_COL).getStringCellValue().trim();
  193.             p.ign = row.getCell(IGN_COL).getStringCellValue().trim();
  194.             p.inBigRaffle = row.getCell(RAFFLE_CHOICE_COL).getStringCellValue().contains("128");
  195.             participants.add(p);
  196.         }
  197.  
  198.         boolean containsDupes = showPropertyDupes(participants, o -> o.ign, "IGN");
  199.         containsDupes |= showPropertyDupes(participants, o -> o.forumsName, "forum name");
  200.  
  201.         boolean allValid = true;
  202.         for (Participant p : participants) {
  203.             if (!isValidPlayer(p)) {
  204.                 allValid = false;
  205.             }
  206.         }
  207.  
  208.         if (containsDupes || !allValid) {
  209.             System.out.println("[-] The excel file should be fixed manually before the winners are chosen.");
  210.             return;
  211.         }
  212.  
  213.         System.out.println("[+] Ready to go!");
  214.         System.out.println("[*] Selecting winners...\n");
  215.  
  216.         List<Participant> smalls = participants.stream().filter(p -> !p.inBigRaffle).collect(Collectors.toList());
  217.         Participant smallWinner = smalls.get(new Random().nextInt(smalls.size()));
  218.         System.out.println(String.format("Out of %d participants, the winner of the small raffle is... %s!!!",
  219.                 smalls.size(), smallWinner.ign));
  220.  
  221.         List<Participant> bigs = participants.stream().filter(p -> p.inBigRaffle).collect(Collectors.toList());
  222.         Participant bigWinner = bigs.get(new Random().nextInt(bigs.size()));
  223.         System.out.println(String.format("Out of %d participants, the winner of the small raffle is... %s!!!",
  224.                 bigs.size(), bigWinner.ign));
  225.     }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment