GetItemList.java Source code

Java tutorial

Introduction

Here is the source code for GetItemList.java

Source

import org.json.*;
import java.util.*;
import java.net.*;
import java.io.*;

/*
Copyright (c) 2013 Connor Davison
    
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

public class GetItemList {

    // Number of categories available
    public static int max_cat = 37;

    // All possible item first letters
    public static String[] alpha = { "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", "%23" };

    // Items shown per JSON page
    public static int items_per_page = 12;

    // The location of the items script
    public static String script_url = "http://services.runescape.com/m=itemdb_rs/api/catalogue/items.json?";

    // Whether to echo notes or not
    public static boolean verbose = true;

    // The string with which to separate lines when reading from URLs
    public static final String EOL_URL = " ";

    // The string with which to separate lines when outputting data
    public static final String EOL_OUT = "\n";

    // The string which to separate data fields when outputting data
    public static String EOR = "\t";

    public static void main(String[] args) {
        verbose = false;
        getItemList();
    }

    /**
     * Start pulling information from the GE API. Information is posted, by default,
     * to the standard output. When verbose = false, the standard output may be
     * redirected to a file to format all pairs:
     *          
     *            [ID]:[NAME]\n
     *
     *
     */
    public static void getItemList() {

        // Go through each category
        for (int i = 0; i <= max_cat; i++) {

            // Go through each character
            for (int j = 0; j < alpha.length; j++) {

                // k is temporarily set to a large value until the amount of pages 
                // is ascertained, at which point it represents the amount of pages.
                //
                // This part is only here to prevent some unnecessary requests to RS, 
                // and make the code a little more comfortable.
                //
                // It works entirely on the premise that l will never = k.

                int k = Integer.MAX_VALUE;

                // Go through each page
                for (int l = 1; l <= k; l++) {
                    String url = script_url + "category=" + i + "&" + "alpha=" + alpha[j] + "&" + "page=" + l;

                    String ge_items_dump = "";

                    try {
                        // Get the page
                        ge_items_dump = getPage(url);

                        // And if page couldn't be retrieved
                    } catch (Exception e) {
                        // Try again
                        l--;
                        continue;

                        // I left this bit in just in case of unstable connections
                    }

                    // If JaGeX is stone-walling us
                    if (!ge_items_dump.startsWith("{")) {
                        // Sleep a bit.
                        try {
                            Thread.sleep(1234);
                        } catch (InterruptedException ie) {
                            if (verbose)
                                System.out.println("Bloody Java");
                        }
                        if (verbose)
                            System.out.println("Bandwidth at JaGeX API exceeded, retrying.");

                        // And retry this page.
                        l--;
                        continue;
                    }

                    if (verbose)
                        System.out.println(url);

                    // Convert the dump to a JSON object and do some manipulation
                    JSONObject jo = new JSONObject(ge_items_dump);

                    if (k == Integer.MAX_VALUE) {
                        // Count how many items there are in the JSON object
                        int num_items = jo.getInt("total");

                        // Deal with the case that we've made an unnecessary call
                        if (num_items == 0)
                            break;

                        // If we've struck gold, find out how many pages there are
                        k = (num_items + items_per_page) / items_per_page;
                    }

                    // Get the array of items
                    JSONArray ja = jo.getJSONArray("items");

                    // And then loop through them
                    for (int m = 0; m < ja.length(); m++) {
                        jo = ja.getJSONObject(m);
                        int id = jo.getInt("id");
                        String name = jo.getString("name");

                        // Write the pairing to the database
                        /*if(verbose) */System.out.print(id + EOR + name + EOL_OUT);
                    }
                    try {
                        Thread.sleep(1234);
                    } catch (InterruptedException ie) {
                        System.out.println("bloody java");
                    }
                }

            }

        }

    }

    /**
     * Retrieve the contents of a URL
     *
     */
    public static String getPage(String path) throws Exception {
        // Create the URL from the given path
        URL url = new URL(path);

        // We won't bother checking that everything's okay
        URLConnection url_conn = url.openConnection();

        // Create a reader for the input stream
        BufferedReader read = new BufferedReader(new InputStreamReader(url_conn.getInputStream()));

        // read through each line and separate with the EOL string
        String out = "";
        String line = "";
        while ((line = read.readLine()) != null) {
            out += line + EOL_URL;
        }

        // Be sure to tidy up
        read.close();

        // Return the contents of the URL
        return out;
    }

}