Last active
December 26, 2015 21:52
-
-
Save newhavengill/747548e2b4c1ce416ee7 to your computer and use it in GitHub Desktop.
Simple ESPN Video API Parser. GSON. No POJO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.espn.data; | |
import com.google.gson.JsonArray; | |
import com.google.gson.JsonElement; | |
import com.google.gson.JsonObject; | |
import com.google.gson.JsonParser; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.net.URL; | |
public class EspnParser { | |
public static final String APIKEY = "GET YER OWN :)"; | |
public static final String APIURL = "http://api.espn.com/v1/video/clips/"; | |
public static String getUrl(String clipId) { | |
return APIURL + clipId + "?apikey=" + APIKEY; | |
} | |
public static JsonObject getVideosJsonObject(String clipId) throws Exception { | |
final String json = readUrl(getUrl(clipId)); | |
final JsonArray videos = getVideosJsonArray(json); | |
final JsonElement video = videos.get(0); | |
return video.getAsJsonObject(); | |
} | |
public static JsonArray getVideosJsonArray(final String json) { | |
final JsonArray videos = new JsonParser().parse(json).getAsJsonObject().get("videos").getAsJsonArray(); | |
return videos; | |
} | |
public static String readUrl(final String urlString) throws Exception { | |
BufferedReader reader = null; | |
try { | |
final URL url = new URL(urlString); | |
reader = new BufferedReader(new InputStreamReader(url.openStream())); | |
final StringBuffer buffer = new StringBuffer(); | |
int read; | |
final char[] chars = new char[1024]; | |
while ((read = reader.read(chars)) != -1) { | |
buffer.append(chars, 0, read); | |
} | |
return buffer.toString(); | |
} finally { | |
if (reader != null) { | |
reader.close(); | |
} | |
} | |
} | |
/** Simple Test **/ | |
public static void main(final String[] args) { | |
try { | |
//EspnParser ep = new EspnParser(); | |
//JsonObject video = ep.getVideosJsonObject("14418150"); | |
JsonObject video = getVideosJsonObject("14418150"); | |
System.out.println(video.getAsJsonObject() | |
.get("links").getAsJsonObject() | |
.get("source").getAsJsonObject() | |
.get("HLS").getAsJsonObject() | |
.get("HD").getAsJsonObject() | |
.get("href")); | |
Thread.sleep(100); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment