001/**
002 * Copyright 2011 The University of Southampton, Yahoo Inc., and the
003 * individual contributors. All rights reserved.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.openimaj.web.scraping.images;
018
019import java.io.InputStreamReader;
020import java.net.URL;
021import java.util.Arrays;
022import java.util.List;
023import java.util.Map;
024
025import org.openimaj.web.scraping.SiteSpecificConsumer;
026
027import com.google.gson.Gson;
028
029/**
030 * Use the instagram api to download images
031 * 
032 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
033 * 
034 */
035public class InstagramConsumer implements SiteSpecificConsumer {
036        String apiCallFormat = "http://api.instagram.com/oembed?url=http://instagr.am/p/%s";
037        private transient Gson gson = new Gson();
038
039        @Override
040        public boolean canConsume(URL url) {
041                // http://instagram.com/p/Mbr57UC7L6
042                return url.getHost().equals("instagr.am") || url.getHost().equals("instagram.com");
043        }
044
045        @SuppressWarnings("unchecked")
046        @Override
047        public List<URL> consume(URL url) {
048                String file = url.getFile();
049                if (file.endsWith("/"))
050                        file = file.substring(0, file.length() - 1);
051                final String[] splits = file.split("/");
052                final String shortID = splits[splits.length - 1];
053                final String apiCall = String.format(apiCallFormat, shortID);
054                try {
055                        final Map<String, Object> res = gson.fromJson(new InputStreamReader(new URL(apiCall).openConnection()
056                                        .getInputStream()), Map.class);
057                        final String instagramURL = (String) res.get("url");
058                        final URL u = new URL(instagramURL);
059                        return Arrays.asList(u);
060                } catch (final Exception e) {
061                        return null;
062                }
063        }
064
065}