Monday, June 30, 2014

Collect your linkedin socail contact on MongoDb

I haven;t written my blog for sometimes.These days I write a simple application to collect my Linked social contact into MongoDB from BlueMix.I will share my experience.I put my code at jazzhub,you can get complete code from here.

LinkedIn  is a business-oriented social networking service.People can know other one's working status and job info from this website.Sometimes poeple want to save his connection into his database for information collection.

When you use my application to deploy on the bluemix,the first things you need to do you need to apply for a key for API usage.You can apply the key from https://developer.linkedin.com/apply.

You can fill the below form,after that you can get the oath key.


The key class is  SocialServlet .

package br.com.ibm.bluemix.mongodb;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cloudfoundry.runtime.env.CloudEnvironment;

import br.com.bigdata.linkedin.api.ConnectionsAPI;
import br.com.bigdata.linkedin.api.ProfileAPI;
import br.com.bigdata.linkedin.factory.ClientFactory;

import com.google.code.linkedinapi.client.LinkedInApiClient;
import com.google.code.linkedinapi.schema.Person;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
@WebServlet("/Social")
public class SocialServlet extends HttpServlet implements Servlet {

private static final String CONSUMER_KEY_OPTION = "";
private static final String CONSUMER_SECRET_OPTION = "";
private static final String ACCESS_TOKEN_OPTION = "";
private static final String ACCESS_TOKEN_SECRET_OPTION = "";
private static final int QUERY_MAX = 20;

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
try {
String connURL = getServiceURI();
MongoClient mongoClient = new MongoClient(new MongoClientURI(
connURL));

ClientFactory client = new ClientFactory(
SocialServlet.CONSUMER_KEY_OPTION,
SocialServlet.CONSUMER_SECRET_OPTION);
final LinkedInApiClient accessApi = client.getFactory()
.createLinkedInApiClient(SocialServlet.ACCESS_TOKEN_OPTION,
SocialServlet.ACCESS_TOKEN_SECRET_OPTION);
long timeStart = System.currentTimeMillis();
System.out.println("Process start..");
ProfileAPI profileAPI = new ProfileAPI(accessApi);
DB mongoDB = mongoClient.getDB("db");
DBCollection usersProfile = mongoDB.getCollection("usersProfile");
BasicDBObject userDocument = new BasicDBObject();
int countQuery = 0;
Person profile = profileAPI.getProfile();
ConnectionsAPI connection = new ConnectionsAPI(accessApi);
List<Person> peopleConnection = connection.getConnections()
.getPersonList();
String parentId = profile.getId();
while (SocialServlet.QUERY_MAX > countQuery++) {
userDocument.put("lastName", profile.getFirstName());
if (profile.getLocation() != null) {
System.out.println("lastName is"
+ profile.getLocation().getName());
}
System.out.println("lastName is" + profile.getFirstName());
userDocument.put("lastName", profile.getLastName());
System.out.println("lastName is" + profile.getLastName());
userDocument.put("headline", profile.getHeadline());
System.out.println("headline is" + profile.getHeadline());
userDocument.put("summary", profile.getSummary());
System.out.println("summary is" + profile.getSummary());
userDocument.put("profileUrl", profile.getPublicProfileUrl());
System.out.println("profile url"
+ profile.getPublicProfileUrl());
userDocument.put("profileId", profile.getId());
userDocument.put("birthdate", profile.getDateOfBirth());
userDocument.put("profile birthdate", profile.getDateOfBirth());
System.out.println(" birthdate" + profile.getDateOfBirth());
userDocument.put("parentUser",
parentId.equals(profile.getId()) ? "" : parentId);
usersProfile.save(userDocument);
userDocument.clear();
if (peopleConnection.size() > 0
&& peopleConnection.size() > countQuery) {
profile = profileAPI.getProfile(peopleConnection.get(
countQuery).getId());

} else {
countQuery = SocialServlet.QUERY_MAX;
}

}
mongoClient.close();
long timeEnd = System.currentTimeMillis();
out
.println("Your Linked social contact has been saved in the mongodb");
System.out
.println("Time schedule " + (timeEnd - timeStart) + " ms");

} catch (Exception e) {
out.println("Failed: " + e.getMessage());
e.printStackTrace();
}
}

public String getServiceURI() throws Exception {
CloudEnvironment environment = new CloudEnvironment();
if (environment.getServiceDataByLabels("mongodb").size() == 0) {
throw new Exception("No MongoDB service is bund to this app!!");
}

Map credential = (Map) ((Map) environment.getServiceDataByLabels(
"mongodb").get(0)).get("credentials");
System.out.println(" I have get credentials url"+(String) credential.get("url"));
return (String) credential.get("url");
}

}

Notes:You need to add cloudfoundry.jar to get the MongoDB credential url.In order to use Linked API,you need to download the api jar from the https://code.google.com/p/linkedin-j/

After export the war from the eclipse project ,you can use cf command to deploy this way to the BlueMix.
Use command like:
cf p Mysocailcontact -p social.war 

Then bind mongodb service to the Mysocailcontact app.

After execute the below url
http://mysocailcontact.mybluemix.net/Social

You can find the log we wrote in the code on the bluemix.

Login into the Mongo DB console on the bluemix,you can find the record on that db,the record will save my connection from this website.








No comments:

Post a Comment