apiPull.getItem.java Source code

Java tutorial

Introduction

Here is the source code for apiPull.getItem.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package apiPull;

import Persistance.apiDump;
import com.sun.jersey.api.client.WebResource;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import me.nithanim.gw2api.v2.GuildWars2Api;
import me.nithanim.gw2api.v2.api.items.GameType;
import me.nithanim.gw2api.v2.api.items.ItemInfo;
import me.nithanim.gw2api.v2.api.items.ItemRarity;
import me.nithanim.gw2api.v2.api.items.ItemType;
import org.apache.commons.lang.enums.EnumUtils;

/**
 *
 * @author olive
 */
public class getItem {

    private ArrayList items = new ArrayList();

    public static ItemInfo test(int id) {

        System.out.println("start of pull test");
        GuildWars2Api gw2api = new GuildWars2Api();
        ItemInfo get = gw2api.items().get(id);
        System.out.println("item returned");
        System.out.println(get.toString());
        return get;
    }

    //get item
    //discard if null
    // check type = one of the allowed types
    //discard if not level 80
    //discard if not exotic
    //discard if restricted
    //else add to array
    public ArrayList getItems() {

        ItemInfo item;
        ArrayList itemList = new ArrayList();
        GuildWars2Api gw2api = new GuildWars2Api();
        ArrayList idList = numberOfIds();
        int myIndex = 0;
        do {
            try {

                String cleanedString = idList.get(myIndex).toString();
                cleanedString = cleanedString.replace("[", ""); //remove bracket
                cleanedString = cleanedString.replace(" ", "");
                cleanedString = cleanedString.replace("]", "");
                System.out.println(cleanedString);
                int idInt = Integer.parseInt(cleanedString);
                item = gw2api.items().get(idInt);
                boolean check = itemCheck(item);
                if (check) {
                    itemList.add(item);
                    System.out.println("ItemAdded! size: " + itemList.size() + " ID: " + idInt);
                }
                myIndex++;
            } catch (Exception ex) {

                System.out.println("Error; " + ex);
            }
        } while (myIndex != idList.size() - 1);

        return itemList;
    }

    public static boolean itemCheck(ItemInfo myItem) {
        boolean bVal = false;
        try {

            if (typeCheck(myItem) && rarityCheck(myItem) && lvlCheck(myItem) && gameTypeCheck(myItem)) {

                bVal = true;
            }
        } catch (Exception ex) {
            System.out.println("Error; " + ex);

        }
        return bVal;

    }

    private static boolean typeCheck(ItemInfo myItem) {
        boolean type = false;
        try {
            List tValid = Arrays.asList(myItem.getType());
            type = (tValid.contains(ItemType.ARMOR) || (tValid.contains(ItemType.BACK)
                    || (tValid.contains(ItemType.WEAPON) || (tValid.contains(ItemType.TRINKET)))));
        } catch (Exception ex) {
            System.out.println("Error; " + ex);
        }
        return type;
    }

    private static boolean rarityCheck(ItemInfo myItem) {
        boolean r = false;
        try {
            List rValid = Arrays.asList(myItem.getRarity());
            r = rValid.contains(ItemRarity.EXOTIC);
        } catch (Exception ex) {
            System.out.println("Error; " + ex);
        }
        return r;
    }

    private static boolean lvlCheck(ItemInfo myItem) {
        boolean lvl = false;
        try {
            lvl = (myItem.getLevel() == 80);
        } catch (Exception ex) {
            System.out.println("Error; " + ex);
        }
        return lvl;
    }

    private static boolean gameTypeCheck(ItemInfo myItem) {
        boolean gt = false;
        try {
            List gtValid = Arrays.asList(myItem.getGameTypes());
            gt = gtValid.contains(GameType.PVE);
        } catch (Exception ex) {
            System.out.println("Error; " + ex);
        }
        return gt;
    }

    public static void main(String[] args) throws IOException {
        ItemInfo test = test(70527);

        // dumpList.dumpList(items1);
        //  System.out.println(items1.toString());
        ;
    }

    public static ArrayList numberOfIds() {
        // go to gw2 https://api.guildwars2.com/v2/items and get a list of items
        // return the arraylist, use to loop through implimentations

        ArrayList ids = new ArrayList();

        // Make a URL to the web page
        URL url = null;
        try {
            url = new URL("https://api.guildwars2.com/v2/items");
        } catch (MalformedURLException ex) {
            Logger.getLogger(getItem.class.getName()).log(Level.SEVERE, null, ex);
        }

        // Get the input stream through URL Connection
        URLConnection con = null;
        try {
            con = url.openConnection();
        } catch (IOException ex) {
            Logger.getLogger(getItem.class.getName()).log(Level.SEVERE, null, ex);
        }
        InputStream is = null;
        try {
            is = con.getInputStream();
            // Once you have the Input Stream, it's just plain old Java IO stuff.
            // For this case, since you are interested in getting plain-text web page
            // I'll use a reader and output the text content to System.out.
            // For binary content, it's better to directly read the bytes from stream and write
            // to the target file.

        } catch (IOException ex) {
            Logger.getLogger(getItem.class.getName()).log(Level.SEVERE, null, ex);
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(is));

        String line = null;

        try {
            // read each line 
            while ((line = br.readLine()) != null) {

                Scanner scanner = new Scanner(line);
                scanner.useDelimiter(",");
                while (scanner.hasNext()) {
                    ids.add(scanner.next());
                }
                scanner.close();
            }
        } catch (IOException ex) {
            Logger.getLogger(getItem.class.getName()).log(Level.SEVERE, null, ex);
        }

        return ids;
    }

}