summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/gpoddernet/GpodnetService.java
blob: 7e0a34e0ba0f136a9c0f1c1b585738943cd0273e (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
package de.danoeh.antennapod.gpoddernet;

import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.gpoddernet.model.*;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.auth.AuthenticationException;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.params.CoreProtocolPNames;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

/**
 * Communicates with the gpodder.net service.
 */
public class GpodnetService {

    private static final String BASE_SCHEME = "https";
    private static final String BASE_HOST = "gpodder.net";

    private GpodnetClient httpClient;

    public GpodnetService() {
        httpClient = new GpodnetClient();
        httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, AppConfig.USER_AGENT);
    }

    /**
     * Returns the [count] most used tags.
     */
    public List<GpodnetTag> getTopTags(int count)
            throws GpodnetServiceException {
        URI uri;
        try {
            uri = new URI(BASE_SCHEME, BASE_HOST, String.format(
                    "/api/2/tags/%d.json", count), null);
        } catch (URISyntaxException e1) {
            e1.printStackTrace();
            throw new IllegalStateException(e1);
        }

        HttpGet request = new HttpGet(uri);
        String response = executeRequest(request);
        try {
            JSONArray jsonTagList = new JSONArray(response);
            List<GpodnetTag> tagList = new ArrayList<GpodnetTag>(
                    jsonTagList.length());
            for (int i = 0; i < jsonTagList.length(); i++) {
                JSONObject jObj = jsonTagList.getJSONObject(i);
                String name = jObj.getString("tag");
                int usage = jObj.getInt("usage");
                tagList.add(new GpodnetTag(name, usage));
            }
            return tagList;
        } catch (JSONException e) {
            e.printStackTrace();
            throw new GpodnetServiceException(e);
        }
    }

    /**
     * Returns the [count] most subscribed podcasts for the given tag.
     *
     * @throws IllegalArgumentException if tag is null
     */
    public List<GpodnetPodcast> getPodcastsForTag(GpodnetTag tag, int count)
            throws GpodnetServiceException {
        if (tag == null) {
            throw new IllegalArgumentException(
                    "Tag and title of tag must not be null");
        }
        try {
            URI uri = new URI(BASE_SCHEME, BASE_HOST, String.format(
                    "/api/2/tag/%s/%d.json", tag.getName(), count), null);
            HttpGet request = new HttpGet(uri);
            String response = executeRequest(request);

            JSONArray jsonArray = new JSONArray(response);
            return readPodcastListFromJSONArray(jsonArray);

        } catch (JSONException e) {
            e.printStackTrace();
            throw new GpodnetServiceException(e);
        } catch (URISyntaxException e) {
            e.printStackTrace();
            throw new GpodnetServiceException(e);

        }
    }

    /**
     * Returns the toplist of podcast.
     *
     * @param count of elements that should be returned. Must be in range 1..100.
     * @throws IllegalArgumentException if count is out of range.
     */
    public List<GpodnetPodcast> getPodcastToplist(int count)
            throws GpodnetServiceException {
        if (count < 1 || count > 100) {
            throw new IllegalArgumentException("Count must be in range 1..100");
        }
        try {
            URI uri = new URI(BASE_SCHEME, BASE_HOST, String.format(
                    "/toplist/%d.json", count), null);
            HttpGet request = new HttpGet(uri);
            String response = executeRequest(request);

            JSONArray jsonArray = new JSONArray(response);
            return readPodcastListFromJSONArray(jsonArray);

        } catch (JSONException e) {
            e.printStackTrace();
            throw new GpodnetServiceException(e);
        } catch (URISyntaxException e) {
            e.printStackTrace();
            throw new IllegalStateException(e);

        }
    }

    /**
     * Returns a list of suggested podcasts for the user that is currently
     * logged in.
     * <p/>
     * This method requires authentication.
     *
     * @param count The
     *              number of elements that should be returned. Must be in range
     *              1..100.
     * @throws IllegalArgumentException              if count is out of range.
     * @throws GpodnetServiceAuthenticationException If there is an authentication error.
     */
    public List<GpodnetPodcast> getSuggestions(int count) throws GpodnetServiceException {
        if (count < 1 || count > 100) {
            throw new IllegalArgumentException("Count must be in range 1..100");
        }
        try {
            URI uri = new URI(BASE_SCHEME, BASE_HOST, String.format(
                    "/suggestions/%d.json", count), null);
            HttpGet request = new HttpGet(uri);
            String response = executeRequest(request);

            JSONArray jsonArray = new JSONArray(response);
            return readPodcastListFromJSONArray(jsonArray);
        } catch (URISyntaxException e) {
            e.printStackTrace();
            throw new IllegalStateException(e);
        } catch (JSONException e) {
            e.printStackTrace();
            throw new GpodnetServiceException(e);
        }
    }

    /**
     * Searches the podcast directory for a given string.
     *
     * @param query          The search query
     * @param scaledLogoSize The size of the logos that are returned by the search query.
     *                       Must be in range 1..256. If the value is out of range, the
     *                       default value defined by the gpodder.net API will be used.
     */
    public List<GpodnetPodcast> searchPodcasts(String query, int scaledLogoSize)
            throws GpodnetServiceException {
        String parameters = (scaledLogoSize > 0 && scaledLogoSize <= 256) ? String
                .format("q=%s&scale_logo=%d", query, scaledLogoSize) : String
                .format("q=%s", query);
        try {
            URI uri = new URI(BASE_SCHEME, null, BASE_HOST, -1, "/search.json",
                    parameters, null);
            System.out.println(uri.toASCIIString());
            HttpGet request = new HttpGet(uri);
            String response = executeRequest(request);

            JSONArray jsonArray = new JSONArray(response);
            return readPodcastListFromJSONArray(jsonArray);

        } catch (JSONException e) {
            e.printStackTrace();
            throw new GpodnetServiceException(e);
        } catch (URISyntaxException e) {
            e.printStackTrace();
            throw new IllegalStateException(e);

        }
    }

    /**
     * Returns all devices of a given user.
     * <p/>
     * This method requires authentication.
     *
     * @param username The username. Must be the same user as the one which is
     *                 currently logged in.
     * @throws IllegalArgumentException              If username is null.
     * @throws GpodnetServiceAuthenticationException If there is an authentication error.
     */
    public List<GpodnetDevice> getDevices(String username)
            throws GpodnetServiceException {
        if (username == null) {
            throw new IllegalArgumentException("Username must not be null");
        }
        try {
            URI uri = new URI(BASE_SCHEME, BASE_HOST, String.format(
                    "/api/2/devices/%s.json", username), null);
            HttpGet request = new HttpGet(uri);
            String response = executeRequest(request);
            JSONArray devicesArray = new JSONArray(response);
            List<GpodnetDevice> result = readDeviceListFromJSONArray(devicesArray);

            return result;
        } catch (URISyntaxException e) {
            e.printStackTrace();
            throw new IllegalStateException(e);
        } catch (JSONException e) {
            e.printStackTrace();
            throw new GpodnetServiceException(e);
        }
    }

    /**
     * Configures the device of a given user.
     * <p/>
     * This method requires authentication.
     *
     * @param username The username. Must be the same user as the one which is
     *                 currently logged in.
     * @param deviceId The ID of the device that should be configured.
     * @throws IllegalArgumentException              If username or deviceId is null.
     * @throws GpodnetServiceAuthenticationException If there is an authentication error.
     */
    public void configureDevice(String username, String deviceId,
                                String caption, GpodnetDevice.DeviceType type)
            throws GpodnetServiceException {
        if (username == null || deviceId == null) {
            throw new IllegalArgumentException(
                    "Username and device ID must not be null");
        }
        try {
            URI uri = new URI(BASE_SCHEME, BASE_HOST, String.format(
                    "/api/2/devices/%s/%s.json", username, deviceId), null);
            HttpPost request = new HttpPost(uri);
            if (caption != null || type != null) {
                JSONObject jsonContent = new JSONObject();
                if (caption != null) {
                    jsonContent.put("caption", caption);
                }
                if (type != null) {
                    jsonContent.put("type", type.toString());
                }
                StringEntity strEntity = new StringEntity(
                        jsonContent.toString(), "UTF-8");
                strEntity.setContentType("application/json");
                request.setEntity(strEntity);
            }
            executeRequest(request);
        } catch (URISyntaxException e) {
            e.printStackTrace();
            throw new IllegalArgumentException(e);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new IllegalStateException(e);
        } catch (JSONException e) {
            e.printStackTrace();
            throw new GpodnetServiceException(e);
        }
    }

    /**
     * Returns the subscriptions of a specific device.
     * <p/>
     * This method requires authentication.
     *
     * @param username The username. Must be the same user as the one which is
     *                 currently logged in.
     * @param deviceId The ID of the device whose subscriptions should be returned.
     * @return A list of subscriptions in OPML format.
     * @throws IllegalArgumentException              If username or deviceId is null.
     * @throws GpodnetServiceAuthenticationException If there is an authentication error.
     */
    public String getSubscriptionsOfDevice(String username, String deviceId)
            throws GpodnetServiceException {
        if (username == null || deviceId == null) {
            throw new IllegalArgumentException(
                    "Username and device ID must not be null");
        }
        try {
            URI uri = new URI(BASE_SCHEME, BASE_HOST, String.format(
                    "/subscriptions/%s/%s.opml", username, deviceId), null);
            HttpGet request = new HttpGet(uri);
            String response = executeRequest(request);
            return response;
        } catch (URISyntaxException e) {
            e.printStackTrace();
            throw new IllegalArgumentException(e);
        }
    }

    /**
     * Returns all subscriptions of a specific user.
     * <p/>
     * This method requires authentication.
     *
     * @param username The username. Must be the same user as the one which is
     *                 currently logged in.
     * @return A list of subscriptions in OPML format.
     * @throws IllegalArgumentException              If username is null.
     * @throws GpodnetServiceAuthenticationException If there is an authentication error.
     */
    public String getSubscriptionsOfUser(String username)
            throws GpodnetServiceException {
        if (username == null) {
            throw new IllegalArgumentException("Username must not be null");
        }
        try {
            URI uri = new URI(BASE_SCHEME, BASE_HOST, String.format(
                    "/subscriptions/%s.opml", username), null);
            HttpGet request = new HttpGet(uri);
            String response = executeRequest(request);
            return response;
        } catch (URISyntaxException e) {
            e.printStackTrace();
            throw new IllegalArgumentException(e);
        }
    }

    /**
     * Uploads the subscriptions of a specific device.
     * <p/>
     * This method requires authentication.
     *
     * @param username      The username. Must be the same user as the one which is
     *                      currently logged in.
     * @param deviceId      The ID of the device whose subscriptions should be updated.
     * @param subscriptions A list of feed URLs containing all subscriptions of the
     *                      device.
     * @throws IllegalArgumentException              If username, deviceId or subscriptions is null.
     * @throws GpodnetServiceAuthenticationException If there is an authentication error.
     */
    public void uploadSubscriptions(String username, String deviceId,
                                    List<String> subscriptions) throws GpodnetServiceException {
        if (username == null || deviceId == null || subscriptions == null) {
            throw new IllegalArgumentException(
                    "Username, device ID and subscriptions must not be null");
        }
        try {
            URI uri = new URI(BASE_SCHEME, BASE_HOST, String.format(
                    "/subscriptions/%s/%s.txt", username, deviceId), null);
            HttpPut request = new HttpPut(uri);
            StringBuilder builder = new StringBuilder();
            for (String s : subscriptions) {
                builder.append(s);
                builder.append("\n");
            }
            StringEntity entity = new StringEntity(builder.toString(), "UTF-8");
            request.setEntity(entity);

            executeRequest(request);
        } catch (URISyntaxException e) {
            e.printStackTrace();
            throw new IllegalStateException(e);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new IllegalStateException(e);
        }
    }

    /**
     * Updates the subscription list of a specific device.
     * <p/>
     * This method requires authentication.
     *
     * @param username The username. Must be the same user as the one which is
     *                 currently logged in.
     * @param deviceId The ID of the device whose subscriptions should be updated.
     * @param added    Collection of feed URLs of added feeds. This Collection MUST NOT contain any duplicates
     * @param removed  Collection of feed URLs of removed feeds. This Collection MUST NOT contain any duplicates
     * @return a GpodnetUploadChangesResponse. See {@link de.danoeh.antennapod.gpoddernet.model.GpodnetUploadChangesResponse}
     * for details.
     * @throws java.lang.IllegalArgumentException                      if username, deviceId, added or removed is null.
     * @throws de.danoeh.antennapod.gpoddernet.GpodnetServiceException if added or removed contain duplicates or if there
     *                                                                 is an authentication error.
     */
    public GpodnetUploadChangesResponse uploadChanges(String username, String deviceId, Collection<String> added,
                                                      Collection<String> removed) throws GpodnetServiceException {
        if (username == null || deviceId == null || added == null || removed == null) {
            throw new IllegalArgumentException(
                    "Username, device ID, added and removed must not be null");
        }
        try {
            URI uri = new URI(BASE_SCHEME, BASE_HOST, String.format(
                    "/api/2/subscriptions/%s/%s.json", username, deviceId), null);

            final JSONObject requestObject = new JSONObject();
            requestObject.put("add", new JSONArray(added));
            requestObject.put("remove", new JSONArray(removed));

            HttpPost request = new HttpPost(uri);
            StringEntity entity = new StringEntity(requestObject.toString(), "UTF-8");
            request.setEntity(entity);

            final String response = executeRequest(request);
            return GpodnetUploadChangesResponse.fromJSONObject(response);
        } catch (URISyntaxException e) {
            e.printStackTrace();
            throw new IllegalStateException(e);
        } catch (JSONException e) {
            e.printStackTrace();
            throw new GpodnetServiceException(e);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new IllegalStateException(e);
        }

    }

    /**
     * Returns all subscription changes of a specific device.
     * <p/>
     * This method requires authentication.
     *
     * @param username  The username. Must be the same user as the one which is
     *                  currently logged in.
     * @param deviceId  The ID of the device whose subscription changes should be
     *                  downloaded.
     * @param timestamp A timestamp that can be used to receive all changes since a
     *                  specific point in time.
     * @throws IllegalArgumentException              If username or deviceId is null.
     * @throws GpodnetServiceAuthenticationException If there is an authentication error.
     */
    public GpodnetSubscriptionChange getSubscriptionChanges(String username,
                                                            String deviceId, long timestamp) throws GpodnetServiceException {
        if (username == null || deviceId == null) {
            throw new IllegalArgumentException(
                    "Username and device ID must not be null");
        }
        String params = String.format("since=%d", timestamp);
        String path = String.format("/api/2/subscriptions/%s/%s.json",
                username, deviceId);
        try {
            URI uri = new URI(BASE_SCHEME, null, BASE_HOST, -1, path, params,
                    null);
            HttpGet request = new HttpGet(uri);

            String response = executeRequest(request);
            JSONObject changes = new JSONObject(response);
            return readSubscriptionChangesFromJSONObject(changes);
        } catch (URISyntaxException e) {
            e.printStackTrace();
            throw new IllegalStateException(e);
        } catch (JSONException e) {
            e.printStackTrace();
            throw new GpodnetServiceException(e);
        }

    }

    /**
     * Logs in a specific user. This method must be called if any of the methods
     * that require authentication is used.
     *
     * @throws IllegalArgumentException If username or password is null.
     */
    public void authenticate(String username, String password)
            throws GpodnetServiceException {
        if (username == null || password == null) {
            throw new IllegalArgumentException(
                    "Username and password must not be null");
        }
        URI uri;
        try {
            uri = new URI(BASE_SCHEME, BASE_HOST, String.format(
                    "/api/2/auth/%s/login.json", username), null);
        } catch (URISyntaxException e) {
            e.printStackTrace();
            throw new GpodnetServiceException();
        }
        HttpPost request = new HttpPost(uri);
        executeRequestWithAuthentication(request, username, password);
    }

    /**
     * Shuts down the GpodnetService's HTTP client. The service will be shut down in a separate thread to avoid
     * NetworkOnMainThreadExceptions.
     */
    public void shutdown() {
        new Thread() {
            @Override
            public void run() {
                httpClient.getConnectionManager().shutdown();
            }
        }.start();
    }

    private String executeRequest(HttpRequestBase request)
            throws GpodnetServiceException {
        if (request == null) {
            throw new IllegalArgumentException("request must not be null");
        }
        String responseString = null;
        HttpResponse response = null;
        try {
            response = httpClient.execute(request);
            checkStatusCode(response);
            responseString = getStringFromEntity(response.getEntity());
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            throw new GpodnetServiceException(e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new GpodnetServiceException(e);
        } finally {
            if (response != null) {
                try {
                    response.getEntity().consumeContent();
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new GpodnetServiceException(e);
                }
            }

        }
        return responseString;
    }

    private String executeRequestWithAuthentication(HttpRequestBase request,
                                                    String username, String password) throws GpodnetServiceException {
        if (request == null || username == null || password == null) {
            throw new IllegalArgumentException(
                    "request and credentials must not be null");
        }
        String result = null;
        HttpResponse response = null;
        try {
            Header auth = new BasicScheme().authenticate(
                    new UsernamePasswordCredentials(username, password),
                    request);
            request.addHeader(auth);
            response = httpClient.execute(request);
            checkStatusCode(response);
            result = getStringFromEntity(response.getEntity());
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            throw new GpodnetServiceException(e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new GpodnetServiceException(e);
        } catch (AuthenticationException e) {
            e.printStackTrace();
            throw new GpodnetServiceException(e);
        } finally {
            if (response != null) {
                try {
                    response.getEntity().consumeContent();
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new GpodnetServiceException(e);
                }
            }
        }
        return result;
    }

    private String getStringFromEntity(HttpEntity entity)
            throws GpodnetServiceException {
        if (entity == null) {
            throw new IllegalArgumentException("entity must not be null");
        }
        ByteArrayOutputStream outputStream;
        int contentLength = (int) entity.getContentLength();
        if (contentLength > 0) {
            outputStream = new ByteArrayOutputStream(contentLength);
        } else {
            outputStream = new ByteArrayOutputStream();
        }
        try {
            byte[] buffer = new byte[8 * 1024];
            InputStream in = entity.getContent();
            int count;
            while ((count = in.read(buffer)) > 0) {
                outputStream.write(buffer, 0, count);
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new GpodnetServiceException(e);
        }
        // System.out.println(outputStream.toString());
        return outputStream.toString();
    }

    private void checkStatusCode(HttpResponse response)
            throws GpodnetServiceException {
        if (response == null) {
            throw new IllegalArgumentException("response must not be null");
        }
        int responseCode = response.getStatusLine().getStatusCode();
        if (responseCode != HttpStatus.SC_OK) {
            if (responseCode == HttpStatus.SC_UNAUTHORIZED) {
                throw new GpodnetServiceAuthenticationException("Wrong username or password");
            } else {
                throw new GpodnetServiceBadStatusCodeException(
                        "Bad response code: " + responseCode, responseCode);
            }
        }
    }

    private List<GpodnetPodcast> readPodcastListFromJSONArray(JSONArray array)
            throws JSONException {
        if (array == null) {
            throw new IllegalArgumentException("array must not be null");
        }
        List<GpodnetPodcast> result = new ArrayList<GpodnetPodcast>(
                array.length());
        for (int i = 0; i < array.length(); i++) {
            result.add(readPodcastFromJSONObject(array.getJSONObject(i)));
        }
        return result;

    }

    private GpodnetPodcast readPodcastFromJSONObject(JSONObject object)
            throws JSONException {
        String url = object.getString("url");

        String title;
        Object titleObj = object.opt("title");
        if (titleObj != null && titleObj instanceof String) {
            title = (String) titleObj;
        } else {
            title = url;
        }

        String description;
        Object descriptionObj = object.opt("description");
        if (descriptionObj != null && descriptionObj instanceof String) {
            description = (String) descriptionObj;
        } else {
            description = "";
        }

        int subscribers = object.getInt("subscribers");

        Object logoUrlObj = object.opt("logo_url");
        String logoUrl = (logoUrlObj instanceof String) ? (String) logoUrlObj
                : null;
        if (logoUrl == null) {
            Object scaledLogoUrl = object.opt("scaled_logo_url");
            if (scaledLogoUrl != null && scaledLogoUrl instanceof String) {
                logoUrl = (String) scaledLogoUrl;
            }
        }

        String website = null;
        Object websiteObj = object.opt("website");
        if (websiteObj != null && websiteObj instanceof String) {
            website = (String) websiteObj;
        }
        String mygpoLink = object.getString("mygpo_link");
        return new GpodnetPodcast(url, title, description, subscribers,
                logoUrl, website, mygpoLink);
    }

    private List<GpodnetDevice> readDeviceListFromJSONArray(JSONArray array)
            throws JSONException {
        if (array == null) {
            throw new IllegalArgumentException("array must not be null");
        }
        List<GpodnetDevice> result = new ArrayList<GpodnetDevice>(
                array.length());
        for (int i = 0; i < array.length(); i++) {
            result.add(readDeviceFromJSONObject(array.getJSONObject(i)));
        }
        return result;
    }

    private GpodnetDevice readDeviceFromJSONObject(JSONObject object)
            throws JSONException {
        String id = object.getString("id");
        String caption = object.getString("caption");
        String type = object.getString("type");
        int subscriptions = object.getInt("subscriptions");
        return new GpodnetDevice(id, caption, type, subscriptions);
    }

    private GpodnetSubscriptionChange readSubscriptionChangesFromJSONObject(
            JSONObject object) throws JSONException {
        if (object == null) {
            throw new IllegalArgumentException("object must not be null");
        }
        List<String> added = new LinkedList<String>();
        JSONArray jsonAdded = object.getJSONArray("add");
        for (int i = 0; i < jsonAdded.length(); i++) {
            added.add(jsonAdded.getString(i));
        }

        List<String> removed = new LinkedList<String>();
        JSONArray jsonRemoved = object.getJSONArray("remove");
        for (int i = 0; i < jsonRemoved.length(); i++) {
            removed.add(jsonRemoved.getString(i));
        }

        long timestamp = object.getLong("timestamp");
        return new GpodnetSubscriptionChange(added, removed, timestamp);
    }
}