summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/miroguide/con/MiroGuideService.java
blob: 1013f466d801f58fa163ad7986cb90a1b14369ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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.MiroGuideChannel;
import de.danoeh.antennapod.miroguide.model.MiroGuideItem;


/** Provides methods to communicate with the Miroguide API on an abstract level. */
public class MiroGuideService {
	private static final String TAG = "MiroGuideService";
	
	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 MiroGuideConnector connector;

	private static ThreadLocal<SimpleDateFormat> jSONDateFormat = new ThreadLocal<SimpleDateFormat>() {
		@Override
		protected SimpleDateFormat initialValue() {
			return new SimpleDateFormat(JSON_DATE_FORMAT_STRING, Locale.US);
		}

	};

	public MiroGuideService() {
		connector = new MiroGuideConnector();
	}
	
	public void close() {
		connector.shutdown();
	}

	public String[] getCategories() throws MiroGuideException {
		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 MiroGuideException();
			}
		}
		return result;
	}

	/** Get a list of MiroGuideChannel objects without their items. */
	public List<MiroGuideChannel> getChannelList(String filter, String filterValue,
			String sort, int limit, int offset) throws MiroGuideException {
		JSONArray resultArray = connector.getArrayResponse(connector
				.createGetChannelsUri(filter, filterValue, sort,
						Integer.toString(limit), Integer.toString(offset)));
		int resultLen = resultArray.length();
		List<MiroGuideChannel> channels = new ArrayList<MiroGuideChannel>(resultLen);
		for (int i = 0; i < resultLen; i++) {
			JSONObject content = null;
			try {
				content = resultArray.getJSONObject(i);
				MiroGuideChannel channel = extractMiroChannel(content, false);
				channels.add(channel);
			} catch (JSONException e) {
				e.printStackTrace();
				throw new MiroGuideException();
			}
		}

		return channels;
	}

	/**
	 * Get a single channel with its items.
	 * 
	 * @throws MiroGuideException
	 */
	public MiroGuideChannel getChannel(long id) throws MiroGuideException {
		JSONObject resultObject = connector.getSingleObjectResponse(connector
				.createGetChannelUri(Long.toString(id)));
		MiroGuideChannel result = null;
		try {
			result = extractMiroChannel(resultObject, true);
		} catch (JSONException e) {
			e.printStackTrace();
			throw new MiroGuideException();
		}
		return result;
	}

	/**
	 * Get a MiroGuideChannel object from it's JSON source. The itemlist of the
	 * channel can be included or excluded
	 * 
	 * @throws JSONException
	 */
	private MiroGuideChannel 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.optString("thumbnail_url");
		String downloadUrl = content.getString("url");
		String websiteUrl = content.getString("website_url");
		if (!withItems) {
			return new MiroGuideChannel(id, name, thumbnailUrl, downloadUrl,
					websiteUrl, description);
		} else {
			JSONArray itemData = content.getJSONArray("item");
			int numItems = itemData.length();
			ArrayList<MiroGuideItem> items = new ArrayList<MiroGuideItem>(numItems);
			for (int i = 0; i < numItems; i++) {
				items.add(extractMiroItem(itemData.getJSONObject(i)));
			}

			return new MiroGuideChannel(id, name, thumbnailUrl, downloadUrl,
					websiteUrl, description, items);
		}
	}

	/** Get a MiroGuideItem from its JSON source. */
	private MiroGuideItem 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 MiroGuideItem(name, description, date, url);
	}

	private Date parseMiroItemDate(String s) {
		try {
			return jSONDateFormat.get().parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

}