Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import org.jsoup.Jsoup;
- import org.jsoup.select.Elements;
- import org.jsoup.nodes.*;
- /**
- * This program requires Java 8 since it is using the new Lambda expressions and Streams.
- * @author Brian Wiegand
- * @since Java 8
- */
- public class Main {
- /**
- * The list of the acceptable consoles.
- * You can add/remove as many as you like, but at least one is required.
- */
- public static final List<String> CONSOLES = Arrays.asList (
- "atari2600", "atari5200", "atari7800", "atarijaguar", "atarilynx", "c64", "cps1", "cps2", "gba",
- "gbc", "mame", "namcosystem22", "neogeo", "neogeocd", "neogeopocket", "nes", "n64", "nds", "gcn",
- "segacd", "dc", "gamegear", "genesis", "mastersystem", "model2", "saturn", "psx", "ps2", "snes"
- );
- /**
- * The list of the acceptable pages.
- * You can add/remove as many as you like, but at least one is required.
- */
- public static final List<String> LETTERS = Arrays.asList (
- "0", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
- "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
- );
- /**
- * The list of the acceptable regions.
- * You can add/remove as many as you like, but at least one is required.
- */
- public static final List<String> REGIONS = Arrays.asList( "USA" );
- /**
- * A thread-safe list we use to hold all the Game objects we create.
- */
- public static List<Game> list = Collections.synchronizedList(new ArrayList<Game>());
- /**
- * Main program entry point.
- */
- public static void main(String[] args) {
- // Brand new Streams and Lambda expressions with parallelism built-in.
- CONSOLES.parallelStream().forEach(console ->
- LETTERS.parallelStream().forEach(letter ->
- REGIONS.parallelStream().forEach(region ->
- tryIt(console, letter, region)
- )
- )
- );
- // Quickly sort the list
- Collections.sort(list);
- // Do something with the list. In this case simply output the results.
- list.forEach(System.out::println);
- }
- /**
- * Take the console, letter, and region: Try and connect to that page, if so then proceed to find each game
- * in parallel and {@code Main::processLink} on each of the results.
- */
- public static void tryIt(String console, String letter, String region) {
- try {
- // Get the HTML of the page.
- Document doc = Jsoup.connect("http://coolrom.com/roms/" + console + "/" + letter + "/").get();
- // Region is important since it's the easiest way to quickly find each game on the page.
- doc.getElementsByClass(region).parallelStream().forEach(Main::processLink);
- }
- catch (Exception e) {}
- }
- /**
- * Basically this will create and add a Game object to the thread-safe list.
- */
- public static void processLink(Element region) {
- // Find the game by the fact that it is the only link inside the Region.
- Elements gameElement = region.getElementsByAttribute("href");
- // Create the Game Object whereby text() is the game's name, and attr("href") is the page link.
- Game game = new Game(gameElement.text(), "http://coolrom.com" + gameElement.attr("href"));
- // Add the game to the thread-safe list.
- synchronized (list) {
- list.add(game);
- }
- }
- }
- class Game implements Comparable<Game> {
- public final String url;
- public final String console;
- public final String name;
- public final int id;
- public Game(String name, String url) {
- this.url = url;
- String[] strs = url.replace("http://coolrom.com/roms/", "").split("/");
- this.console = strs[0].toUpperCase();
- this.id = Integer.parseInt(strs[1]);
- this.name = name.trim();
- }
- @Override
- public int hashCode() {
- return id;
- }
- @Override
- public String toString() {
- return id + ": " + console + ": " + name + " - " + url;
- }
- @Override
- public int compareTo(Game o) {
- return Integer.compare(this.id, o.id);
- }
- }