Created classes for communicating with the Miro Guide API
This commit is contained in:
parent
d8d055cf24
commit
2a4b6d3a74
128
src/de/danoeh/antennapod/miroguide/con/MiroConnector.java
Normal file
128
src/de/danoeh/antennapod/miroguide/con/MiroConnector.java
Normal file
@ -0,0 +1,128 @@
|
||||
package de.danoeh.antennapod.miroguide.con;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.net.Uri;
|
||||
|
||||
/** Executes HTTP requests and returns the results. */
|
||||
public class MiroConnector {
|
||||
private HttpClient httpClient;
|
||||
|
||||
private static final String HOST_URL = "www.miroguide.com/api/";
|
||||
private static final String PATH_GET_CHANNELS = "get_channels";
|
||||
private static final String PATH_LIST_CATEGORIES = "list_categories";
|
||||
private static final String PATH_GET_CHANNEL = "get_channel";
|
||||
|
||||
public MiroConnector() {
|
||||
httpClient = new DefaultHttpClient();
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
httpClient.getConnectionManager().shutdown();
|
||||
}
|
||||
|
||||
private Uri.Builder getBaseURIBuilder(String path) {
|
||||
Uri.Builder builder = new Uri.Builder();
|
||||
builder.scheme("https").appendPath(HOST_URL).appendPath(path)
|
||||
.appendQueryParameter("datatype", "json");
|
||||
return builder;
|
||||
}
|
||||
|
||||
public JSONArray getArrayResponse(Uri uri) throws MiroException {
|
||||
try {
|
||||
JSONArray result = new JSONArray(executeRequest(uri));
|
||||
return result;
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
throw new MiroException();
|
||||
}
|
||||
}
|
||||
|
||||
public JSONObject getSingleObjectResponse(Uri uri) throws MiroException {
|
||||
try {
|
||||
JSONObject result = new JSONObject(executeRequest(uri));
|
||||
return result;
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
throw new MiroException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a HTTP GET request with the given URI and returns the content of
|
||||
* the return value.
|
||||
*
|
||||
* @throws MiroException
|
||||
*/
|
||||
private String executeRequest(Uri uri) throws MiroException {
|
||||
HttpGet httpGet = new HttpGet(uri.toString());
|
||||
String result = null;
|
||||
try {
|
||||
HttpResponse response = httpClient.execute(httpGet);
|
||||
if (response.getStatusLine().getStatusCode() == 200) {
|
||||
HttpEntity entity = response.getEntity();
|
||||
if (entity != null) {
|
||||
InputStream in = entity.getContent();
|
||||
|
||||
BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(in));
|
||||
result = reader.readLine();
|
||||
}
|
||||
} else {
|
||||
throw new MiroException(response.getStatusLine()
|
||||
.getReasonPhrase());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new MiroException(e.getMessage());
|
||||
}
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
public Uri createGetChannelsUri(String filter, String filterValue,
|
||||
String sort, String limit, String offset) throws MiroException {
|
||||
Uri.Builder resultBuilder = getBaseURIBuilder(PATH_GET_CHANNELS);
|
||||
resultBuilder.appendQueryParameter("filter", filter)
|
||||
.appendQueryParameter("filter_value", filterValue);
|
||||
|
||||
if (sort != null) {
|
||||
resultBuilder.appendQueryParameter("sort", sort);
|
||||
}
|
||||
if (limit != null) {
|
||||
resultBuilder.appendQueryParameter("limit", limit);
|
||||
}
|
||||
if (offset != null) {
|
||||
resultBuilder.appendQueryParameter("offset", offset);
|
||||
}
|
||||
Uri result = resultBuilder.build();
|
||||
return result;
|
||||
}
|
||||
|
||||
public Uri createListCategoriesURI() throws MiroException {
|
||||
Uri.Builder resultBuilder = getBaseURIBuilder(PATH_LIST_CATEGORIES);
|
||||
Uri result = resultBuilder.build();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public Uri createGetChannelUri(String id) throws MiroException {
|
||||
Uri.Builder resultBuilder = getBaseURIBuilder(PATH_GET_CHANNEL)
|
||||
.appendQueryParameter("id", id);
|
||||
Uri result = resultBuilder.build();
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
23
src/de/danoeh/antennapod/miroguide/con/MiroException.java
Normal file
23
src/de/danoeh/antennapod/miroguide/con/MiroException.java
Normal file
@ -0,0 +1,23 @@
|
||||
package de.danoeh.antennapod.miroguide.con;
|
||||
|
||||
public class MiroException extends Exception {
|
||||
private static final long serialVersionUID = -8834656185748713194L;
|
||||
|
||||
public MiroException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public MiroException(String arg0, Throwable arg1) {
|
||||
super(arg0, arg1);
|
||||
}
|
||||
|
||||
public MiroException(String arg0) {
|
||||
super(arg0);
|
||||
}
|
||||
|
||||
public MiroException(Throwable arg0) {
|
||||
super(arg0);
|
||||
}
|
||||
|
||||
|
||||
}
|
148
src/de/danoeh/antennapod/miroguide/con/MiroService.java
Normal file
148
src/de/danoeh/antennapod/miroguide/con/MiroService.java
Normal file
@ -0,0 +1,148 @@
|
||||
package de.danoeh.antennapod.miroguide.con;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.danoeh.antennapod.miroguide.model.MiroChannel;
|
||||
import de.danoeh.antennapod.miroguide.model.MiroItem;
|
||||
|
||||
|
||||
/** Provides methods to communicate with the Miroguide API on an abstract level. */
|
||||
public class MiroService {
|
||||
public static final int DEFAULT_CHANNEL_LIMIT = 20;
|
||||
|
||||
public static final String FILTER_CATEGORY = "category";
|
||||
public static final String FILTER_NAME = "name";
|
||||
public static final String SORT_NAME = "name";
|
||||
public static final String SORT_POPULAR = "popular";
|
||||
public static final String SORT_RATING = "rating";
|
||||
|
||||
public static final String JSON_DATE_FORMAT_STRING = "yyyy-MM-dd'T'HH:mm:ss";
|
||||
|
||||
private MiroConnector connector;
|
||||
|
||||
private static ThreadLocal<SimpleDateFormat> jSONDateFormat = new ThreadLocal<SimpleDateFormat>() {
|
||||
@Override
|
||||
protected SimpleDateFormat initialValue() {
|
||||
return new SimpleDateFormat(JSON_DATE_FORMAT_STRING, Locale.US);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
public MiroService() {
|
||||
connector = new MiroConnector();
|
||||
}
|
||||
|
||||
public String[] getCategories() throws MiroException {
|
||||
JSONArray resultArray = connector.getArrayResponse(connector
|
||||
.createListCategoriesURI());
|
||||
String[] result = new String[resultArray.length()];
|
||||
for (int i = 0; i < resultArray.length(); i++) {
|
||||
try {
|
||||
result[i] = resultArray.getJSONObject(i).getString("name");
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
throw new MiroException();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Get a list of MiroChannel objects without their items. */
|
||||
public List<MiroChannel> getChannelList(String filter, String filterValue,
|
||||
String sort, int limit, int offset) throws MiroException {
|
||||
JSONArray resultArray = connector.getArrayResponse(connector
|
||||
.createGetChannelsUri(filter, filterValue, sort,
|
||||
Integer.toString(limit), Integer.toString(offset)));
|
||||
int resultLen = resultArray.length();
|
||||
List<MiroChannel> channels = new ArrayList<MiroChannel>(resultLen);
|
||||
for (int i = 0; i < resultLen; i++) {
|
||||
JSONObject content = null;
|
||||
try {
|
||||
content = resultArray.getJSONObject(i);
|
||||
MiroChannel channel = extractMiroChannel(content, false);
|
||||
channels.add(channel);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
throw new MiroException();
|
||||
}
|
||||
}
|
||||
|
||||
return channels;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single channel with its items.
|
||||
*
|
||||
* @throws MiroException
|
||||
*/
|
||||
public MiroChannel getChannel(long id) throws MiroException {
|
||||
JSONObject resultObject = connector.getSingleObjectResponse(connector
|
||||
.createGetChannelUri(Long.toString(id)));
|
||||
MiroChannel result = null;
|
||||
try {
|
||||
result = extractMiroChannel(resultObject, true);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
throw new MiroException();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a MiroChannel object from it's JSON source. The itemlist of the
|
||||
* channel can be included or excluded
|
||||
*
|
||||
* @throws JSONException
|
||||
*/
|
||||
private MiroChannel extractMiroChannel(JSONObject content, boolean withItems)
|
||||
throws JSONException {
|
||||
long id = content.getLong("id");
|
||||
String name = content.getString("name");
|
||||
String description = content.getString("description");
|
||||
String thumbnailUrl = content.getString("thumbnail_url");
|
||||
String downloadUrl = content.getString("url");
|
||||
String websiteUrl = content.getString("website_url");
|
||||
if (!withItems) {
|
||||
return new MiroChannel(id, name, thumbnailUrl, downloadUrl,
|
||||
websiteUrl, description);
|
||||
} else {
|
||||
JSONArray itemData = content.getJSONArray("item");
|
||||
int numItems = itemData.length();
|
||||
ArrayList<MiroItem> items = new ArrayList<MiroItem>(numItems);
|
||||
for (int i = 0; i < numItems; i++) {
|
||||
items.add(extractMiroItem(itemData.getJSONObject(i)));
|
||||
}
|
||||
|
||||
return new MiroChannel(id, name, thumbnailUrl, downloadUrl,
|
||||
websiteUrl, description, items);
|
||||
}
|
||||
}
|
||||
|
||||
/** Get a MiroItem from its JSON source. */
|
||||
private MiroItem extractMiroItem(JSONObject content) throws JSONException {
|
||||
Date date = parseMiroItemDate(content.getString("date"));
|
||||
String description = content.getString("description");
|
||||
String name = content.getString("name");
|
||||
String url = content.getString("url");
|
||||
return new MiroItem(name, description, date, url);
|
||||
}
|
||||
|
||||
private Date parseMiroItemDate(String s) {
|
||||
try {
|
||||
return jSONDateFormat.get().parse(s);
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
75
src/de/danoeh/antennapod/miroguide/model/MiroChannel.java
Normal file
75
src/de/danoeh/antennapod/miroguide/model/MiroChannel.java
Normal file
@ -0,0 +1,75 @@
|
||||
package de.danoeh.antennapod.miroguide.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class MiroChannel {
|
||||
private long id;
|
||||
private String name;
|
||||
private String thumbnailUrl;
|
||||
private String downloadUrl;
|
||||
private String websiteUrl;
|
||||
private String description;
|
||||
private ArrayList<MiroItem> items;
|
||||
|
||||
public MiroChannel(long id, String name, String thumbnailUrl,
|
||||
String downloadUrl, String websiteUrl, String description) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.thumbnailUrl = thumbnailUrl;
|
||||
this.downloadUrl = downloadUrl;
|
||||
this.websiteUrl = websiteUrl;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public MiroChannel(long id, String name, String thumbnailUrl,
|
||||
String downloadUrl, String websiteUrl, String description,
|
||||
ArrayList<MiroItem> items) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.thumbnailUrl = thumbnailUrl;
|
||||
this.downloadUrl = downloadUrl;
|
||||
this.websiteUrl = websiteUrl;
|
||||
this.description = description;
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return id + " " + name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getThumbnailUrl() {
|
||||
return thumbnailUrl;
|
||||
}
|
||||
|
||||
public String getDownloadUrl() {
|
||||
return downloadUrl;
|
||||
}
|
||||
|
||||
public String getWebsiteUrl() {
|
||||
return websiteUrl;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public ArrayList<MiroItem> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
40
src/de/danoeh/antennapod/miroguide/model/MiroItem.java
Normal file
40
src/de/danoeh/antennapod/miroguide/model/MiroItem.java
Normal file
@ -0,0 +1,40 @@
|
||||
package de.danoeh.antennapod.miroguide.model;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class MiroItem {
|
||||
private String name;
|
||||
private String description;
|
||||
private Date date;
|
||||
private String url;
|
||||
|
||||
public MiroItem(String name, String description, Date date, String url) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.date = date;
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name + " " + date.toString();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user