summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/danoeh/antennapod/discovery/PodcastSearchResult.java
blob: c8096e9c5e1e98d9054ef1949e4a9052e98ec1e3 (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
package de.danoeh.antennapod.discovery;

import androidx.annotation.Nullable;
import de.danoeh.antennapod.net.sync.gpoddernet.model.GpodnetPodcast;
import de.mfietz.fyydlin.SearchHit;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class PodcastSearchResult {

    /**
     * The name of the podcast
     */
    public final String title;

    /**
     * URL of the podcast image
     */
    @Nullable
    public final String imageUrl;
    /**
     * URL of the podcast feed
     */
    @Nullable
    public final String feedUrl;

    /**
     * artistName of the podcast feed
     */
    @Nullable
    public final String author;


    private PodcastSearchResult(String title, @Nullable String imageUrl, @Nullable String feedUrl, @Nullable String author) {
        this.title = title;
        this.imageUrl = imageUrl;
        this.feedUrl = feedUrl;
        this.author = author;
    }

    public static PodcastSearchResult dummy() {
        return new PodcastSearchResult("", "", "", "");
    }

    /**
     * Constructs a Podcast instance from a iTunes search result
     *
     * @param json object holding the podcast information
     * @throws JSONException
     */
    public static PodcastSearchResult fromItunes(JSONObject json) {
        String title = json.optString("collectionName", "");
        String imageUrl = json.optString("artworkUrl100", null);
        String feedUrl = json.optString("feedUrl", null);
        String author = json.optString("artistName", null);
        return new PodcastSearchResult(title, imageUrl, feedUrl, author);
    }

    /**
     * Constructs a Podcast instance from iTunes toplist entry
     *
     * @param json object holding the podcast information
     * @throws JSONException
     */
    public static PodcastSearchResult fromItunesToplist(JSONObject json) throws JSONException {
        String title = json.getJSONObject("title").getString("label");
        String imageUrl = null;
        JSONArray images =  json.getJSONArray("im:image");
        for(int i=0; imageUrl == null && i < images.length(); i++) {
            JSONObject image = images.getJSONObject(i);
            String height = image.getJSONObject("attributes").getString("height");
            if(Integer.parseInt(height) >= 100) {
                imageUrl = image.getString("label");
            }
        }
        String feedUrl = "https://itunes.apple.com/lookup?id=" +
                json.getJSONObject("id").getJSONObject("attributes").getString("im:id");

        String author = null;
        try {
            author = json.getJSONObject("im:artist").getString("label");
        } catch (Exception e) {
            // Some feeds have empty artist
        }
        return new PodcastSearchResult(title, imageUrl, feedUrl, author);
    }

    public static PodcastSearchResult fromFyyd(SearchHit searchHit) {
        return new PodcastSearchResult(searchHit.getTitle(),
                                       searchHit.getThumbImageURL(),
                                       searchHit.getXmlUrl(),
                                       searchHit.getAuthor());
    }

    public static PodcastSearchResult fromGpodder(GpodnetPodcast searchHit) {
        return new PodcastSearchResult(searchHit.getTitle(),
                                       searchHit.getLogoUrl(),
                                       searchHit.getUrl(),
                                       searchHit.getAuthor());
    }

    public static PodcastSearchResult fromPodcastIndex(JSONObject json) {
        String title = json.optString("title", "");
        String imageUrl = json.optString("image", null);
        String feedUrl = json.optString("url", null);
        String author = json.optString("author", null);
        return new PodcastSearchResult(title, imageUrl, feedUrl, author);
    }
}