GenuineSounds

CoolROM Game Dumper

May 9th, 2014
380
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.80 KB | None | 0 0
  1. import java.util.*;
  2. import org.jsoup.Jsoup;
  3. import org.jsoup.select.Elements;
  4. import org.jsoup.nodes.*;
  5.  
  6. /**
  7.  * This program requires Java 8 since it is using the new Lambda expressions and Streams.
  8.  * @author Brian Wiegand
  9.  * @since Java 8
  10.  */
  11. public class Main {
  12.  
  13.     /**
  14.      *  The list of the acceptable consoles.
  15.      *  You can add/remove as many as you like, but at least one is required.
  16.      */
  17.     public static final List<String> CONSOLES = Arrays.asList (
  18.         "atari2600", "atari5200", "atari7800", "atarijaguar", "atarilynx", "c64", "cps1", "cps2", "gba",
  19.         "gbc", "mame", "namcosystem22", "neogeo", "neogeocd", "neogeopocket", "nes", "n64", "nds", "gcn",
  20.         "segacd", "dc", "gamegear", "genesis", "mastersystem", "model2", "saturn", "psx", "ps2", "snes"
  21.     );
  22.  
  23.     /**
  24.      *  The list of the acceptable pages.
  25.      *  You can add/remove as many as you like, but at least one is required.
  26.      */
  27.     public static final List<String> LETTERS = Arrays.asList (
  28.         "0", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
  29.         "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
  30.     );
  31.  
  32.     /**
  33.      *  The list of the acceptable regions.
  34.      *  You can add/remove as many as you like, but at least one is required.
  35.      */
  36.     public static final List<String> REGIONS = Arrays.asList( "USA" );
  37.  
  38.     /**
  39.      * A thread-safe list we use to hold all the Game objects we create.
  40.      */
  41.     public static List<Game> list = Collections.synchronizedList(new ArrayList<Game>());
  42.  
  43.     /**
  44.      * Main program entry point.
  45.      */
  46.     public static void main(String[] args) {
  47.         // Brand new Streams and Lambda expressions with parallelism built-in.
  48.         CONSOLES.parallelStream().forEach(console ->
  49.             LETTERS.parallelStream().forEach(letter ->
  50.                 REGIONS.parallelStream().forEach(region ->
  51.                     tryIt(console, letter, region)
  52.                 )
  53.             )
  54.         );
  55.  
  56.         // Quickly sort the list
  57.         Collections.sort(list);
  58.  
  59.         // Do something with the list. In this case simply output the results.
  60.         list.forEach(System.out::println);
  61.     }
  62.  
  63.     /**
  64.      * Take the console, letter, and region: Try and connect to that page, if so then proceed to find each game
  65.      * in parallel and {@code Main::processLink} on each of the results.
  66.      */
  67.     public static void tryIt(String console, String letter, String region) {
  68.         try {
  69.             // Get the HTML of the page.
  70.             Document doc = Jsoup.connect("http://coolrom.com/roms/" + console + "/" + letter + "/").get();
  71.  
  72.             // Region is important since it's the easiest way to quickly find each game on the page.
  73.             doc.getElementsByClass(region).parallelStream().forEach(Main::processLink);
  74.         }
  75.         catch (Exception e) {}
  76.     }
  77.  
  78.     /**
  79.      * Basically this will create and add a Game object to the thread-safe list.
  80.      */
  81.     public static void processLink(Element region) {
  82.         // Find the game by the fact that it is the only link inside the Region.
  83.         Elements gameElement = region.getElementsByAttribute("href");
  84.        
  85.         // Create the Game Object whereby text() is the game's name, and attr("href") is the page link.
  86.         Game game = new Game(gameElement.text(), "http://coolrom.com" + gameElement.attr("href"));
  87.  
  88.         // Add the game to the thread-safe list.
  89.         synchronized (list) {
  90.             list.add(game);
  91.         }
  92.     }
  93. }
  94.  
  95. class Game implements Comparable<Game> {
  96.     public final String url;
  97.     public final String console;
  98.     public final String name;
  99.     public final int id;
  100.  
  101.     public Game(String name, String url) {
  102.         this.url = url;
  103.         String[] strs = url.replace("http://coolrom.com/roms/", "").split("/");
  104.         this.console = strs[0].toUpperCase();
  105.         this.id = Integer.parseInt(strs[1]);
  106.         this.name = name.trim();
  107.     }
  108.  
  109.     @Override
  110.     public int hashCode() {
  111.         return id;
  112.     }
  113.  
  114.     @Override
  115.     public String toString() {
  116.         return  id + ": " + console + ": " + name + " - " + url;
  117.     }
  118.  
  119.     @Override
  120.     public int compareTo(Game o) {
  121.         return Integer.compare(this.id, o.id);
  122.     }
  123. }
Comments
  • User was banned
Add Comment
Please, Sign In to add comment