org.apache.marmotta.ldclient.provider.facebook.FacebookGraphProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ldclient-provider-facebook
Show all versions of ldclient-provider-facebook
This package enables the Linked Data Client to access the metadata of Facebook objects using the
Facebook Graph API.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.marmotta.ldclient.provider.facebook;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.DateUtils;
import org.apache.http.util.EntityUtils;
import org.apache.marmotta.commons.http.UriUtil;
import org.apache.marmotta.commons.vocabulary.DCTERMS;
import org.apache.marmotta.commons.vocabulary.FOAF;
import org.apache.marmotta.commons.vocabulary.SCHEMA;
import org.apache.marmotta.ldclient.api.endpoint.Endpoint;
import org.apache.marmotta.ldclient.api.ldclient.LDClientService;
import org.apache.marmotta.ldclient.api.provider.DataProvider;
import org.apache.marmotta.ldclient.exception.DataRetrievalException;
import org.apache.marmotta.ldclient.model.ClientResponse;
import org.openrdf.model.Model;
import org.openrdf.model.URI;
import org.openrdf.model.ValueFactory;
import org.openrdf.model.impl.TreeModel;
import org.openrdf.model.impl.ValueFactoryImpl;
import org.openrdf.model.vocabulary.RDF;
import org.openrdf.model.vocabulary.SKOS;
import org.openrdf.repository.RepositoryException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.google.common.net.HttpHeaders.ACCEPT;
import static com.google.common.net.HttpHeaders.ACCEPT_LANGUAGE;
/**
* A provider that accesses objects exposed by the Facebook Graph API (in JSON format). The provider will map the
* properties of Facebook Objects to RDF and tries choosing the most appropriate RDF types and vocabularies. The base
* vocabulary used for describing objects is dcterms; the rdf types of objects will be taken from schema.org.
*
* @see Facebook Graph API
* @author Sebastian Schaffert ([email protected])
*/
public class FacebookGraphProvider implements DataProvider {
public static final String PROVIDER_NAME = "Facebook Graph API";
private static final Pattern pattern = Pattern.compile("http://www\\.facebook\\.com/([^/]+/)*([^/]+)");
public static final int RETRY_AFTER = 60;
private static Logger log = LoggerFactory.getLogger(FacebookGraphProvider.class);
private static String[] defaultLanguages = new String[] {"en", "de", "fr", "es", "it"};
private static Map facebookCategories = new HashMap();
static {
// see http://www.marketinggum.com/types-of-facebook-pages-for-business/
// Local Business or Place
facebookCategories.put("attractions/things to do", SCHEMA.TouristAttraction);
facebookCategories.put("bank", SCHEMA.BankOrCreditUnion);
facebookCategories.put("bar", SCHEMA.BarOrPub);
facebookCategories.put("book store", SCHEMA.BookStore);
facebookCategories.put("concert venue", SCHEMA.MusicVenue);
facebookCategories.put("food/grocery", SCHEMA.GroceryStore);
facebookCategories.put("hotel", SCHEMA.Hotel);
facebookCategories.put("local business", SCHEMA.LocalBusiness);
facebookCategories.put("movie theatre", SCHEMA.MovieTheater);
facebookCategories.put("museum/art gallery", SCHEMA.Museum);
facebookCategories.put("outdoor gear/sporting goods", SCHEMA.SportingGoodsStore);
facebookCategories.put("real estate", SCHEMA.RealEstateAgent);
facebookCategories.put("restaurant/café", SCHEMA.CafeOrCoffeeShop);
facebookCategories.put("arts/entertainment/nightlife", SCHEMA.NightClub);
facebookCategories.put("school", SCHEMA.School);
// Company Organization or Institution
facebookCategories.put("automobiles and parts", SCHEMA.AutoPartsStore);
facebookCategories.put("church", SCHEMA.Church);
facebookCategories.put("company", SCHEMA.Corporation);
facebookCategories.put("computers/technology", SCHEMA.ComputerStore);
facebookCategories.put("consulting/business services", SCHEMA.Corporation);
facebookCategories.put("insurance company", SCHEMA.InsuranceAgency);
facebookCategories.put("internet/software", SCHEMA.SoftwareApplication);
facebookCategories.put("legal/law", SCHEMA.Attorney);
facebookCategories.put("retail and consumer merchandise", SCHEMA.ConvenienceStore);
facebookCategories.put("media/news/publishing", SCHEMA.Corporation);
facebookCategories.put("travel/leisure", SCHEMA.TravelAgency);
// Brand or Product
facebookCategories.put("app", SCHEMA.MobileApplication);
facebookCategories.put("appliance", SCHEMA.Product);
facebookCategories.put("baby goods/kids goods", SCHEMA.Product);
facebookCategories.put("cars", SCHEMA.AutoPartsStore);
facebookCategories.put("clothing", SCHEMA.ClothingStore);
facebookCategories.put("electronics", SCHEMA.ElectronicsStore);
facebookCategories.put("food/beverages", SCHEMA.FoodEstablishment);
facebookCategories.put("furniture", SCHEMA.FurnitureStore);
facebookCategories.put("games/toys", SCHEMA.ToyStore);
facebookCategories.put("health/beauty", SCHEMA.BeautySalon);
facebookCategories.put("jewelry/watches", SCHEMA.JewelryStore);
facebookCategories.put("kitchen/cooking", SCHEMA.HomeGoodsStore);
facebookCategories.put("pet supplies", SCHEMA.PetStore);
facebookCategories.put("vitamins/minerals", SCHEMA.Product);
// Artist, Band or Public Figure
facebookCategories.put("actor/director", SCHEMA.Person);
facebookCategories.put("artist", SCHEMA.Person);
facebookCategories.put("athlete", SCHEMA.Person);
facebookCategories.put("author", SCHEMA.Person);
facebookCategories.put("business person", SCHEMA.Person);
facebookCategories.put("chef", SCHEMA.Person);
facebookCategories.put("coach", SCHEMA.Person);
facebookCategories.put("doctor", SCHEMA.Person);
facebookCategories.put("entertainer", SCHEMA.Person);
facebookCategories.put("journalist", SCHEMA.Person);
facebookCategories.put("lawyer", SCHEMA.Person);
facebookCategories.put("musician/band", SCHEMA.MusicGroup);
facebookCategories.put("politician", SCHEMA.Person);
facebookCategories.put("teacher", SCHEMA.Person);
facebookCategories.put("writer", SCHEMA.Person);
// entertainment
facebookCategories.put("movie",SCHEMA.Movie);
facebookCategories.put("album",SCHEMA.MusicAlbum);
facebookCategories.put("book", SCHEMA.Book);
facebookCategories.put("concert tour", SCHEMA.MusicEvent);
facebookCategories.put("library", SCHEMA.Library);
facebookCategories.put("magazine", SCHEMA.CreativeWork);
facebookCategories.put("radio station", SCHEMA.RadioStation);
facebookCategories.put("record label", SCHEMA.Brand);
facebookCategories.put("sports venue", SCHEMA.SportsActivityLocation);
facebookCategories.put("tv channel", SCHEMA.TelevisionStation);
facebookCategories.put("tv show", SCHEMA.TVSeries);
// NGO
facebookCategories.put("non-profit organization", SCHEMA.NGO);
facebookCategories.put("cause", SCHEMA.NGO);
facebookCategories.put("health/medical/pharmaceuticals", SCHEMA.MedicalOrganization);
facebookCategories.put("health/medical/pharmacy", SCHEMA.MedicalOrganization);
facebookCategories.put("community/government", SCHEMA.GovernmentOrganization);
facebookCategories.put("church/religious organization", SCHEMA.Church);
facebookCategories.put("education", SCHEMA.EducationalOrganization);
facebookCategories.put("hospital/clinic", SCHEMA.Hospital);
// interests
facebookCategories.put("interest", SKOS.CONCEPT);
}
/**
* Parse the HTTP response entity returned by the web service call and return its contents in a Sesame RDF
* repository also passed as argument. The content type returned by the web service is passed as argument to help
* the implementation decide how to parse the data. The implementation can return a list of additional pages to
* retrieve for completing the data of the resource
*
* @param resourceUri
* @param model an RDF model for storing an RDF representation of the dataset located at the remote resource.
* @param in input stream as returned by the remote webservice
* @param language content language as returned in the HTTP headers of the remote webservice
* @return a possibly empty list of URLs of additional resources to retrieve to complete the content
* @throws java.io.IOException in case an error occurs while reading the input stream
*/
protected List parseResponse(String resourceUri, String requestUrl, Model model, InputStream in, String language) throws DataRetrievalException {
ObjectMapper mapper = new ObjectMapper();
try {
Map data = mapper.readValue(in, new TypeReference