View Single Post
  #10   Spotlight this post!  
Unread 17-12-2012, 14:20
stingray27's Avatar
stingray27 stingray27 is offline
Registered User
AKA: Michael Ray
FRC #0027 (Team RUSH)
Team Role: Alumni
 
Join Date: Mar 2011
Rookie Year: 2010
Location: Clarkston, MI
Posts: 209
stingray27 is a name known to allstingray27 is a name known to allstingray27 is a name known to allstingray27 is a name known to allstingray27 is a name known to allstingray27 is a name known to all
Re: Retrieve data from TheBlueAlliance

Thank you very much for your responses! I was able to get both TFA and TBA up and running. I decided to stick with my html parser for awards, match data, and rankings because it is faster on my android device when refreshing information but as for the team listings, I decided to use The Blue Alliance. For those of you interested, I added my code that I used to develop my methods (in netbeans) before I moved in to Eclipse. So the code was just temporary but it works!

Code:
package bluealliance;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
import org.json.*;

public class BlueAlliance {
    
    private static final String API = "http://www.thebluealliance.com/api/v1/";

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String choiceS;
        System.out.println("Would you like to view Event(1) or Team(2) information? ");
        int choice = in.nextInt();
        switch (choice) {
            case 1:
                System.out.println("Would you like to view a single event(1), or view all events with(2) or without(3) the team listings? ");
                choice = in.nextInt();
                EventDetails[] events;
                String[] team_keys;
                switch (choice) {
                    case 1:
                        System.out.println("Enter the event code of the 2013 regional: ");
                        choiceS = in.next();
                        EventDetails event = getEvent("2013" + choiceS);
                        team_keys = event.getTeamKeys();
                        if (event.getOfficiality()) {
                            System.out.println(event.getName() + "(KN: " + event.getKeyName() + " EC: " + event.getEventCode() + " SN: " + event.getShortName() + ") " + event.getLocation());
                            for (int j=0; j<team_keys.length; j++) {
                                System.out.println(team_keys[j]);
                            }
                        }
                        break;
                    case 2:
                        System.out.println("Enter the year: ");
                        choiceS = in.next();
                        System.out.println("CAUTION: Takes around 60 sec");
                        events = getAllEventsWithTeams(choiceS);
                        for (int i=0; i<events.length; i++) {
                            team_keys = events[i].getTeamKeys();
                            if (events[i].getOfficiality()) {
                                System.out.println(events[i].getName() + "(KN: " + events[i].getKeyName() + " EC: " + events[i].getEventCode() + " SN: " + events[i].getShortName() + ") " + events[i].getLocation());
                                for (int j=0; j<team_keys.length; j++) {
                                    System.out.println(team_keys[j]);
                                }
                            }
                        }
                        break;
                    case 3:
                        System.out.print("Enter the year: ");
                        choiceS = in.next();
                        events = getAllEvents(choiceS);
                        for (int i=0; i<events.length; i++) {
                            team_keys = events[i].getTeamKeys();
                            if (events[i].getOfficiality()) {
                                System.out.println(events[i].getName() + "(KN: " + events[i].getKeyName() + " EC: " + events[i].getEventCode() + " SN: " + events[i].getShortName() + ") " + events[i].getLocation());
                            }
                        }
                        break;
                    default:
                        System.out.println("Incorrect Selection");
                        break;
                }
                break;
            case 2:
                System.out.println("Enter the team: ");
                choiceS = in.next();
                TeamDetails team = getTeam(choiceS);
                String[] event_keys = team.getEventKeys();
                System.out.println( team.getNickName() + " " + team.getKeyName() + " " + team.getLocation() + " " + team.getWebsite() + "\n" + team.getName());
                for (int j=0; j<event_keys.length; j++) {
                    System.out.println(event_keys[j]);
                }
                break;
            default:
                System.out.println("Incorrect Selection");
                break;
        }
    }
    
    public static EventDetails[] getAllEvents(String year) {
        EventDetails[] competitions = new EventDetails[0];
        String url = API + "events/list?year=" + year;
        JSONArray events = getArray(url);
        competitions = new EventDetails[events.length()];
        try {
            for (int i = 0; i < events.length(); i++) {
                JSONObject event = events.getJSONObject(i);
                competitions[i] = new EventDetails(event);
            }
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
        return competitions;
    }
    
    public static EventDetails[] getAllEventsWithTeams(String year) {
        EventDetails[] competitions = getAllEvents(year);
        EventDetails[] competitionWithTeams = new EventDetails[competitions.length];
            for (int i=0; i<competitions.length; i++) {
                competitionWithTeams[i] = getEvent(competitions[i].getKeyName());
            }
        return competitionWithTeams;
    }
    
    public static EventDetails getEvent(String event_key) {
        String url = API + "event/details?event=" + event_key;
        JSONObject json = getJson(url);
        return new EventDetails(json);
    }
    
    public static TeamDetails getTeam(String team) {
        String url = API + "team/details?team=frc" + team;
        JSONObject json = getJson(url);
        return new TeamDetails(json);
    }
    
    public static TeamDetails[] getEventTeams(String[] teams) {
        String teamList = "teams/show?teams=";
        for (int i=0; i<teams.length; i++) {
            if (i==0)
                teamList += teams[i];
            else
                teamList += "," + teams[i];
        }
        String url = API + teamList;
        System.out.println(url);
        TeamDetails[] teamInfo;
        JSONArray teamArray = getArray(url);
        System.out.println(Integer.toString(teamArray.length()));
        teamInfo = new TeamDetails[teamArray.length()];
        try {
            for (int i = 0; i < teamArray.length(); i++) {
                JSONObject event = teamArray.getJSONObject(i);
                teamInfo[i] = new TeamDetails(event);
            }
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
        return teamInfo;
    }

    private static JSONArray getArray(String url) {
        try {
            HttpURLConnection c = (HttpURLConnection) new URL(url).openConnection();
            BufferedReader reader = new BufferedReader(new InputStreamReader(c.getInputStream()));
            String line, input = "";
            while ((line = reader.readLine()) != null)
                    input += line;
            JSONArray json = new JSONArray(input);
            return json;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    private static JSONObject getJson(String url){
        try {
            HttpURLConnection c = (HttpURLConnection) new URL(url).openConnection();
            BufferedReader reader = new BufferedReader(new InputStreamReader(c.getInputStream()));
            String line, input = "";
            while ((line = reader.readLine()) != null)
                    input += line;
            JSONObject json = new JSONObject(input);
            return json;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    private static class EventDetails {

            private String key_name, year, name, short_name, location, facebook_eid, start_date, end_date, officiality;
            private String[] team_keys;
            
            private EventDetails(JSONObject json) {
                this.key_name = json.optString("key");
                this.year = json.optString("year");
                this.name = json.optString("name");
                this.short_name = json.optString("short_name");
                this.location = json.optString("location");
                this.officiality = json.optString("official");
                this.facebook_eid = json.optString("facebook_eid");
                this.start_date = json.optString("start_date");
                this.end_date = json.optString("end_date");
                JSONArray temp = json.optJSONArray("teams");
                if (temp!=null) {
                    String[] temp2 = new String[temp.length()];
                    try {
                        for (int i=0; i<temp2.length; i++) {
                            temp2[i] = temp.getString(i);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    this.team_keys = temp2;
                }
                
            }

            public String getKeyName(){
                return key_name;
            }

            public int getYear(){
                return Integer.parseInt(year);
            }

            public String getEventCode(){
                return key_name.substring(4);
            }

            public String getName(){
                return name;
            }

            public String getShortName(){
                return short_name;
            }
            
            public String[] getTeamKeys(){
                return team_keys;
            }
            
            public String getLocation() {
                return location;
            }
            
            public boolean getOfficiality() {
                if (officiality.equals("true"))
                    return true;
                else
                    return false;
            }
            
            public String getFacebookEID() {
                return facebook_eid;
            }
            
            public String getStartDate() {
                return start_date;
            }
            
            public String getEndDate() {
                return end_date;
            }
    }
    
    private static class TeamDetails {

            private String key_name, team_number, name, nickname, website, location;
            private String[] event_keys;
            
            private TeamDetails(JSONObject json) {
                this.key_name = json.optString("key");
                this.team_number = json.optString("team_number");
                this.name = json.optString("name");
                this.nickname = json.optString("nickname");
                this.location = json.optString("location");
                this.website = json.optString("website");
                JSONArray temp = json.optJSONArray("events");
                if (temp!=null) {
                    String[] temp2 = new String[temp.length()];
                    try {
                        for (int i=0; i<temp2.length; i++) {
                            temp2[i] = temp.getString(i);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    this.event_keys = temp2;
                }
            }

            public String getKeyName(){
                return key_name;
            }

            public int getTeamNumber(){
                return Integer.parseInt(team_number);
            }

            public String getName(){
                return name;
            }

            public String getNickName(){
                return nickname;
            }

            public String getWebsite(){
                return website;
            }

            public String[] getEventKeys(){
                return event_keys;
            }
            
            public String getLocation() {
                return location;
            }
        }
    }
__________________
Michael Ray
Team RUSH 27

Driving Record: 93-64-0 Best Finish: Finalist (x4 FiM Districts)
Coaching Record: 16-7-0 Best Finish: Winner (Kettering Invitationa)l

Reply With Quote