main.summonerinfo.SummonerInfo.java Source code

Java tutorial

Introduction

Here is the source code for main.summonerinfo.SummonerInfo.java

Source

/**
 * Copyright (c) 2014 Ahmad Sakr (http://github.com/ahmadsakr)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package main.summonerinfo;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.IOException;

import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * SummonerInfo {@link SummonerInfo} is the class that handles all data for a certain
 * Summoner i.e  Summoner minion = new Summoner("A Minion With IE", "EUNE");
 * The Whole methods all use up 1 count together in your rate limit only.
 * 
 * @author Ahmad Sakr
 */

public class SummonerInfo {

    private String summonerName;
    private String region;
    private String API_KEY;
    private JsonObject accountInfo;
    private JsonObject realmInfo;
    private int responseCode;

    /**
     * Constructor for {@link SummonerInfo}.
     */
    public SummonerInfo(String summonerName, String region) {

        this.summonerName = summonerName;
        this.region = region;

    }

    /**
     * Checks if the API Key is valid, if it is it will return true, otherwise false
     * NOTE: this request is not counted in your count limit as specified by riot in the API.
     * @return API Key's validity.
     * @throws IOException
     */
    public boolean isAuthorized() throws IOException {
        URL request;
        request = new URL("https://eune.api.pvp.net/api/lol/static-data/eune/v1.2/realm?api_key=" + getAPIKey());
        HttpURLConnection connect = (HttpURLConnection) request.openConnection();

        return connect.getResponseCode() != 401;
    }

    /**
     * Opens connection to the Riot API, gets data in JSON, decodes it and assigns it to a variable.
     * @throws IOException
     */
    private void getAccountInfo() throws IOException {
        JsonObject json;
        URL request;
        request = new URL("https://" + region + ".api.pvp.net/api/lol/" + region + "/v1.4/summoner/by-name/"
                + formatNameForWeb(summonerName) + "?api_key=" + getAPIKey());
        HttpURLConnection connect = (HttpURLConnection) request.openConnection();

        setResponseCode(connect.getResponseCode());

        if (connect.getResponseCode() == 401) {
            return;
        }

        if (connect.getResponseCode() == 404) {
            return;
        }

        BufferedReader in = new BufferedReader(new InputStreamReader(connect.getInputStream(), "UTF-8"));

        JsonParser parser = new JsonParser();
        JsonElement element = parser.parse(in);

        json = element.getAsJsonObject();

        if (json.isJsonObject()) {
            accountInfo = json.getAsJsonObject(formatNameForJson(summonerName));
        }

        in.close();
    }

    /**
     * Opens connection to the Riot API, gets data in JSON, decodes it and assigns it to a variable.
     * @throws IOException
     */
    private void getRealmData() throws IOException {

        URL request;
        request = new URL("https://" + region + ".api.pvp.net/api/lol/static-data/" + region
                + "/v1.2/realm?api_key=" + getAPIKey());
        HttpURLConnection connect = (HttpURLConnection) request.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(connect.getInputStream(), "UTF-8"));

        setResponseCode(connect.getResponseCode());

        if (connect.getResponseCode() == 401) {
            return;
        }

        if (connect.getResponseCode() == 404) {
            return;
        }

        JsonParser parser = new JsonParser();
        JsonElement element = parser.parse(in);

        realmInfo = element.getAsJsonObject();

        in.close();
    }

    /**
     * Gets the League of legends live version.
     *
     * @return The Version
     * @throws IOException
     */
    private String getRealmVersion() throws IOException {
        if (realmInfo == null) {
            getRealmData();
        }

        return realmInfo.get("v").toString().replaceAll("\"", "");
    }

    /**
     * Gets Id of the summoner.
     *
     * @return the id of the summoner
     * @throws IOException
     */
    public int getId() throws IOException {
        if (accountInfo == null) {
            getAccountInfo();
        }

        return accountInfo == null ? -1 : accountInfo.get("id").getAsInt();
    }

    /**
     * Gets The Summoner level of the player
     *
     * @return The Summoner level
     * @throws IOException
     */
    public int getSummonerLevel() throws IOException {
        if (accountInfo == null) {
            getAccountInfo();
        }

        return accountInfo == null ? -1 : accountInfo.get("summonerLevel").getAsInt();
    }

    /**
     * Gets the Summoner name for public use.
     *
     * @return The Summoner name
     * @throws IOException
     */
    public String getSummonerName() throws IOException {
        if (accountInfo == null) {
            getAccountInfo();
        }

        return accountInfo == null ? "ERROR GETTING NAME" : accountInfo.get("name").toString();
    }

    /**
     * Gets the link to the image of the currently set profile icon of the summoner
     *
     * @return The URL of the profile icon image.
     * @throws IOException
     */
    public String getProfileIconLink() throws IOException {
        if (accountInfo == null) {
            getAccountInfo();
        }
        return "http://ddragon.leagueoflegends.com/cdn/" + getRealmVersion() + "/img/profileicon/"
                + accountInfo.get("profileIconId").toString() + ".png";
    }

    /**
     * Gets The Region of the summoner.
     *
     * @return The Region
     */
    public String getRegion() {
        return region.toUpperCase();
    }

    /**
     * The Last time any change has been made to the summoner account
     *
     * @return time in milliseconds
     */
    public long lastUpdate() {
        if (accountInfo == null)
            return -1;
        long update = accountInfo.get("revisionDate").getAsLong();

        return (System.currentTimeMillis() - update);
    }

    /**
     * Sets The API Key that will be used to grab data from the Riot Games' Servers.
     *
     * @param key The API Key
     */
    public void setAPIKey(String key) {
        this.API_KEY = key;
    }

    /**
     * @return The API Key
     */
    private String getAPIKey() {
        return API_KEY;
    }

    /**
     * @return the response code of the HTTP request
     */
    public int getResponseCode() {
        return responseCode;
    }

    /**
     * @param code the response code we are setting
     */
    private void setResponseCode(int code) {
        this.responseCode = code;
    }

    /**
     * The Method is vital to be used upon composing a URL as all spaces have to be replaced
     * with %20.
     * toLowerCase() used as it is required.
     *
     * @param name The Summoner name before formatting.
     * @return The Summoner name after formatting.
     */
    private String formatNameForWeb(String name) {
        return name.replaceAll(" ", "%20").toLowerCase();
    }

    /**
     * The Method is vital to be used upon using the json object as there are no spaces.
     * toLowerCase() used as it is required.
     *
     * @param name The Summoner name before formatting.
     * @return The Summoner name after formatting.
     */
    private String formatNameForJson(String name) {
        return name.replaceAll(" ", "").toLowerCase();
    }

}