aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/ui/reader/utils/ReaderVideoUtils.java
blob: 94ad8ece053ac5a37bce6335d1acb4fb916699e9 (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
package org.wordpress.android.ui.reader.utils;

import android.net.Uri;
import android.text.TextUtils;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;

import org.json.JSONArray;
import org.json.JSONObject;
import org.wordpress.android.WordPress;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.AppLog.T;
import org.wordpress.android.util.JSONUtils;

public class ReaderVideoUtils {
	private ReaderVideoUtils() {
		throw new AssertionError();
	}

    /*
     * returns the url to get the full-size (480x360) thumbnail url for the passed video
     * see http://www.reelseo.com/youtube-thumbnail-image/ for other sizes
     */
    public static String getYouTubeThumbnailUrl(final String videoUrl) {
        String videoId = getYouTubeVideoId(videoUrl);
        if (TextUtils.isEmpty(videoId))
            return "";
        // note that this *must* use https rather than http - ex: https://img.youtube.com/vi/ClbE019cLNI/0.jpg
        return "https://img.youtube.com/vi/" + videoId + "/0.jpg";
    }

	/*
	 * returns true if the passed url is a link to a YouTube video
	 */
	public static boolean isYouTubeVideoLink(final String link) {
		return (!TextUtils.isEmpty(getYouTubeVideoId(link)));
	}

	/*
	 * extract the video id from the passed YouTube url
     */
	private static String getYouTubeVideoId(final String link) {
		if (link==null)
			return "";

		Uri uri = Uri.parse(link);
		try {
			String host = uri.getHost();
			if (host==null)
				return "";

			// youtube.com links
			if (host.equals("youtube.com") || host.equals("www.youtube.com")) {
				// if link contains "watch" in the path, then the id is in the "v=" query param
				if (link.contains("watch"))
					return uri.getQueryParameter("v");
                // if the link contains "embed" in the path, then the id is the last path segment
                // ex: https://www.youtube.com/embed/fw3w68YrKwc?version=3&rel=1&
                if (link.contains("/embed/"))
                    return uri.getLastPathSegment();
				return "";
			}

			// youtu.be urls have the videoId as the path - ex:  http://youtu.be/pEnXclbO9jg
			if (host.equals("youtu.be")) {
				String path = uri.getPath();
				if (path==null)
					return "";
				// remove the leading slash
				return path.replace("/", "");
			}

			// YouTube mobile urls include video id in fragment, ex: http://m.youtube.com/?dc=organic&source=mog#/watch?v=t77Vlme_pf8
			if (host.equals("m.youtube.com")) {
				String fragment = uri.getFragment();
				if (fragment==null)
					return "";
				int index = fragment.lastIndexOf("v=");
				if (index!=-1)
					return fragment.substring(index+2, fragment.length());
			}

			return "";
		} catch (UnsupportedOperationException e) {
			AppLog.e(T.READER, e);
			return "";
		} catch (IndexOutOfBoundsException e) {
			// thrown by substring
            AppLog.e(T.READER, e);
			return "";
		}
	}

    /*
     * returns true if the passed url is a link to a Vimeo video
     */
	public static boolean isVimeoLink(final String link) {
		return (!TextUtils.isEmpty(getVimeoVideoId(link)));
	}

    /*
     * extract the video id from the passed Vimeo url
     * ex: http://player.vimeo.com/video/72386905 -> 72386905
     */
	private static String getVimeoVideoId(final String link) {
		if (link==null)
			return "";
        if (!link.contains("player.vimeo.com"))
            return "";

		Uri uri = Uri.parse(link);
		return uri.getLastPathSegment();
	}

    /*
     * unlike YouTube thumbnails, Vimeo thumbnails require network request
     */
    public static void requestVimeoThumbnail(final String videoUrl, final VideoThumbnailListener thumbListener) {
        // useless without a listener
        if (thumbListener==null)
            return;

        String id = getVimeoVideoId(videoUrl);
        if (TextUtils.isEmpty(id)) {
            thumbListener.onResponse(false, null);
            return;
        }

        Response.Listener<JSONArray> listener = new Response.Listener<JSONArray>() {
            public void onResponse(JSONArray response) {
                String thumbnailUrl = null;
                if (response!=null && response.length() > 0) {
                    JSONObject json = response.optJSONObject(0);
                    if (json!=null && json.has("thumbnail_large"))
                        thumbnailUrl = JSONUtils.getString(json, "thumbnail_large");
                }
                if (TextUtils.isEmpty(thumbnailUrl)) {
                    thumbListener.onResponse(false, null);
                } else {
                    thumbListener.onResponse(true, thumbnailUrl);
                }
            }
        };
        Response.ErrorListener errorListener = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                AppLog.e(T.READER, volleyError);
                thumbListener.onResponse(false, null);
            }
        };

        String url = "http://vimeo.com/api/v2/video/" + id + ".json";
        JsonArrayRequest request = new JsonArrayRequest(url, listener, errorListener);

        WordPress.requestQueue.add(request);
    }

    public interface VideoThumbnailListener {
        void onResponse(boolean successful, String thumbnailUrl);
    }
}