Advertisement
jonalu

alts.min.vc scraper source

Jan 17th, 2012
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.29 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintStream;
  7. import java.net.URL;
  8. import java.util.ArrayList;
  9.  
  10.  
  11. public class Scraper {
  12.     public static void main(String[] args) {
  13.         init();
  14.        
  15.         System.out.println("-=Jonalu's online alt database account scraper=-");
  16.         System.out.println("type help for commands");
  17.         BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
  18.         String uIn;
  19.         try {
  20.             while((uIn = stdIn.readLine()) != null) {
  21.                 command(uIn);
  22.             }
  23.         } catch (IOException e) {
  24.             e.printStackTrace();
  25.         }
  26.         exit();
  27.     }
  28.     private static void exit() {
  29.         exit = true;
  30.         System.out.println("-=Exiting=-");
  31.         System.exit(0);
  32.     }
  33.     private static void init() {
  34.         exit = false;
  35.         scrapedUsernames = new ArrayList<String>();
  36.     }
  37.  
  38.     private static void command(String uIn) {
  39.         if(uIn.equalsIgnoreCase("help")) {
  40.             showHelp();
  41.         } else if(uIn.equalsIgnoreCase("save")) {
  42.             save();
  43.         } else if(uIn.toLowerCase().startsWith("scrape")) {
  44.             scrapeCommand(uIn);
  45.         } else if(uIn.toLowerCase().startsWith("setfile")) {
  46.             setfile(uIn);
  47.         } else if(uIn.equalsIgnoreCase("exit")) {
  48.             exit();
  49.         } else if(uIn.equalsIgnoreCase("abort")) {
  50.             abortCommand(uIn);
  51.         }
  52.     }
  53.  
  54.     private static void setfile(String uIn) {
  55.         try {
  56.             String[] as = uIn.split(" ");
  57.             if(as.length == 2) {
  58.                 outputFile = new File(as[1]);
  59.                 if(outputFile.exists() && outputFile.isDirectory())
  60.                     System.out.println("That's a directory!");
  61.                 else if(!outputFile.exists() && !outputFile.createNewFile())
  62.                     System.out.println("Couldn't create file");
  63.                 else
  64.                     System.out.println("Output file set");
  65.             } else {
  66.                 System.out.println("Syntax Error. Correct Usage: setfile <path>");
  67.             }
  68.         } catch(IOException ioe) {
  69.             ioe.printStackTrace();
  70.         }
  71.     }
  72.     private static void abortCommand(String uIn) {
  73.         abortScraping();
  74.     }
  75.     private static void scrapeCommand(String uIn) {
  76.         String[] as = uIn.split(" ");
  77.         if(as.length == 2) {
  78.             try {
  79.                 int i = Integer.parseInt(as[1]);
  80.                 scrape(i);
  81.             } catch(Exception e) {
  82.                 System.out.println("Syntax Error. Correct Usage: scrape <count>");
  83.             }
  84.         } else {
  85.             System.out.println("Syntax Error. Correct Usage: scrape <count>");
  86.         }
  87.     }
  88.     private static Thread scrapeThread = null;
  89.     private static void scrape(final int i) {
  90.         System.out.println("Scraping "+i+ (i == 1 ? " username" : " usernames"));
  91.         scrapeThread = new Thread() {
  92.             public void run() {
  93.                 try {
  94.                     for(int i1 = 0; i1 < i; i1++) {
  95.                         String s = scrape();
  96.                         if(s == null) {
  97.                             if(i1 < i-1) {
  98.                                 System.out.println("Waiting 2 minutes");
  99.                                 Thread.sleep(1000L*60L*2L);
  100.                             }
  101.                             continue;
  102.                         }
  103.                         System.out.println(s);
  104.                         break;
  105.                     }
  106.                 } catch(IOException ioe) {
  107.                     ioe.printStackTrace();
  108.                 } catch (InterruptedException e) {
  109.                 }
  110.                 System.out.println("Scraping finished.");
  111.                 showTotalUsernames();
  112.             }
  113.         };
  114.         scrapeThread.start();
  115.     }
  116.     private static void showTotalUsernames() {
  117.         System.out.println("Total Usernames: "+scrapedUsernames.size());
  118.     }
  119.     private static void abortScraping() {
  120.         if(scrapeThread != null && scrapeThread.isAlive()) {
  121.             scrapeThread.interrupt();
  122.             showTotalUsernames();
  123.         } else {
  124.             System.out.println("Not scraping.");
  125.         }
  126.     }
  127.     private static void save() {
  128.         try {
  129.             if(outputFile == null || (!outputFile.exists() && !outputFile.mkdirs() && !outputFile.createNewFile())) {
  130.                 System.out.println("no output file set. use setfile <path>.");
  131.             } else {
  132.                 BufferedReader br = new BufferedReader(new FileReader(outputFile));
  133.                 ArrayList<String> usernames = new ArrayList<String>();
  134.                
  135.                 String s1;
  136.                 while((s1 = br.readLine()) != null) {
  137.                     usernames.add(s1);
  138.                 }
  139.                 br.close();
  140.                 for(String s : scrapedUsernames) {
  141.                     if(!usernames.contains(s))
  142.                         usernames.add(s);
  143.                 }
  144.                
  145.                 PrintStream ps = new PrintStream(outputFile);
  146.                
  147.                 for(String s : usernames) {
  148.                     ps.println(s);
  149.                 }
  150.                
  151.                 ps.flush();
  152.                 ps.close();
  153.                 System.out.println("Successfully saved.");
  154.             }
  155.         } catch (IOException e) {
  156.             e.printStackTrace();
  157.         }
  158.     }
  159.  
  160.     private static void showHelp() {
  161.         System.out.println("-=Help=-");
  162.         System.out.println("scrape <count> - scrape accounts");
  163.         System.out.println("abort - abort scraping accounts");
  164.         System.out.println("save - save scraped accounts to file");
  165.         System.out.println("setfile <path> - set output file path");
  166.         System.out.println("help - show this help");
  167.         System.out.println("exit - exit the scraper");
  168.     }
  169.    
  170.     private static String scrape() throws IOException {
  171.         String s = "http://alts.min.vc/?getalt";
  172.         URL url = new URL(s);
  173.         InputStreamReader isr = new InputStreamReader(url.openStream());
  174.         BufferedReader br = new BufferedReader(isr);
  175.         //String s1;
  176.         String[] as;
  177.         as = br.readLine().split(":");
  178.         br.close();
  179.         isr.close();
  180.         if(as.length == 2) {
  181.             if(!scrapedUsernames.contains(as[0])) {
  182.                 scrapedUsernames.add(as[0]);
  183.                 System.out.println("Scraped "+as[0]);
  184.             }
  185.             return null;
  186.         } else if(as.length > 0) {
  187.             return as[0];
  188.         } else {
  189.             return "invalid answer";
  190.         }
  191.     }
  192.    
  193.     private static File outputFile;
  194.     private static ArrayList<String> scrapedUsernames;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement