You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streams.apache.org by sb...@apache.org on 2014/10/11 22:38:01 UTC
[11/17] git commit: STREAMS-180 | InstagramTypeConverter is now able
to properly serialize UserDataItem objects into Activities.
STREAMS-180 | InstagramTypeConverter is now able to properly serialize UserDataItem objects into Activities.
Project: http://git-wip-us.apache.org/repos/asf/incubator-streams/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-streams/commit/119608d2
Tree: http://git-wip-us.apache.org/repos/asf/incubator-streams/tree/119608d2
Diff: http://git-wip-us.apache.org/repos/asf/incubator-streams/diff/119608d2
Branch: refs/heads/STREAMS-170
Commit: 119608d2f196b24cfba4db8aaab19022a7131cf5
Parents: 2397f4b
Author: Robert Douglas <rd...@w2odigital.com>
Authored: Mon Sep 22 16:49:19 2014 -0500
Committer: Robert Douglas <rd...@w2odigital.com>
Committed: Mon Sep 22 16:49:19 2014 -0500
----------------------------------------------------------------------
.../processor/InstagramTypeConverter.java | 5 +-
.../serializer/util/InstagramActivityUtil.java | 46 ++++++++++++++++-
.../test/InstagramActivitySerDeTest.java | 53 +++++++++++++++++++-
.../src/test/resources/testUserInfoData.txt | 2 +
4 files changed, 102 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/119608d2/streams-contrib/streams-provider-instagram/src/main/java/org/apache/streams/instagram/processor/InstagramTypeConverter.java
----------------------------------------------------------------------
diff --git a/streams-contrib/streams-provider-instagram/src/main/java/org/apache/streams/instagram/processor/InstagramTypeConverter.java b/streams-contrib/streams-provider-instagram/src/main/java/org/apache/streams/instagram/processor/InstagramTypeConverter.java
index 90ec3a8..f0101fd 100644
--- a/streams-contrib/streams-provider-instagram/src/main/java/org/apache/streams/instagram/processor/InstagramTypeConverter.java
+++ b/streams-contrib/streams-provider-instagram/src/main/java/org/apache/streams/instagram/processor/InstagramTypeConverter.java
@@ -78,9 +78,10 @@ public class InstagramTypeConverter implements StreamsProcessor {
} else if(item instanceof UserInfoData) {
- activity = this.userInfoSerializer.deserialize((UserInfoData) item );
+ activity = new Activity();
+ instagramActivityUtil.updateActivity((UserInfoData) item, activity);
}
- if(activity != null && activity.getId() != null) {
+ if(activity != null) {
result = new StreamsDatum(activity);
count++;
}
http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/119608d2/streams-contrib/streams-provider-instagram/src/main/java/org/apache/streams/instagram/serializer/util/InstagramActivityUtil.java
----------------------------------------------------------------------
diff --git a/streams-contrib/streams-provider-instagram/src/main/java/org/apache/streams/instagram/serializer/util/InstagramActivityUtil.java b/streams-contrib/streams-provider-instagram/src/main/java/org/apache/streams/instagram/serializer/util/InstagramActivityUtil.java
index 499d0e7..c210be8 100644
--- a/streams-contrib/streams-provider-instagram/src/main/java/org/apache/streams/instagram/serializer/util/InstagramActivityUtil.java
+++ b/streams-contrib/streams-provider-instagram/src/main/java/org/apache/streams/instagram/serializer/util/InstagramActivityUtil.java
@@ -26,6 +26,8 @@ import org.apache.streams.exceptions.ActivitySerializerException;
import org.apache.streams.pojo.json.*;
import org.jinstagram.entity.comments.CommentData;
import org.jinstagram.entity.common.*;
+import org.jinstagram.entity.users.basicinfo.Counts;
+import org.jinstagram.entity.users.basicinfo.UserInfoData;
import org.jinstagram.entity.users.feed.MediaFeedData;
import org.joda.time.DateTime;
import org.slf4j.Logger;
@@ -72,11 +74,53 @@ public class InstagramActivityUtil {
}
/**
+ * Updates the given Activity object with the values from the item
+ * @param item the object to use as the source
+ * @param activity the target of the updates. Will receive all values from the tweet.
+ * @throws ActivitySerializerException
+ */
+ public static void updateActivity(UserInfoData item, Activity activity) throws ActivitySerializerException {
+ activity.setActor(buildActor(item));
+ activity.setId(null);
+ activity.setProvider(getProvider());
+ }
+
+ public static Actor buildActor(UserInfoData item) {
+ Actor actor = new Actor();
+
+ try {
+ Image image = new Image();
+ image.setUrl(item.getProfile_picture());
+
+ Counts counts = item.getCounts();
+
+ Map<String, Object> extensions = new HashMap<String, Object>();
+
+ extensions.put("followers", counts.getFollwed_by());
+ extensions.put("follows", counts.getFollows());
+ extensions.put("screenName", item.getUsername());
+
+ actor.setId(formatId(String.valueOf(item.getId())));
+ actor.setImage(image);
+ actor.setDisplayName(item.getFullName());
+ actor.setSummary(item.getBio());
+ actor.setUrl(item.getWebsite());
+
+ actor.setAdditionalProperty("handle", item.getUsername());
+ actor.setAdditionalProperty("extensions", extensions);
+ } catch (Exception e) {
+ LOGGER.error("Exception trying to build actor object: {}", e.getMessage());
+ }
+
+ return actor;
+ }
+
+ /**
* Builds the actor
* @param item the item
* @return a valid Actor
*/
- public static Actor buildActor(MediaFeedData item) {
+ public static Actor buildActor(MediaFeedData item) {
Actor actor = new Actor();
try {
http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/119608d2/streams-contrib/streams-provider-instagram/src/test/java/org/apache/streams/twitter/test/InstagramActivitySerDeTest.java
----------------------------------------------------------------------
diff --git a/streams-contrib/streams-provider-instagram/src/test/java/org/apache/streams/twitter/test/InstagramActivitySerDeTest.java b/streams-contrib/streams-provider-instagram/src/test/java/org/apache/streams/twitter/test/InstagramActivitySerDeTest.java
index 075da80..de5e466 100644
--- a/streams-contrib/streams-provider-instagram/src/test/java/org/apache/streams/twitter/test/InstagramActivitySerDeTest.java
+++ b/streams-contrib/streams-provider-instagram/src/test/java/org/apache/streams/twitter/test/InstagramActivitySerDeTest.java
@@ -24,6 +24,7 @@ import org.apache.streams.instagram.serializer.util.InstagramDeserializer;
import org.apache.streams.instagram.serializer.InstagramJsonActivitySerializer;
import org.apache.streams.jackson.StreamsJacksonMapper;
import org.apache.streams.pojo.json.Activity;
+import org.jinstagram.entity.users.basicinfo.UserInfoData;
import org.jinstagram.entity.users.feed.MediaFeedData;
import org.junit.Assert;
import org.junit.Test;
@@ -33,6 +34,7 @@ import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.util.Map;
import static org.apache.streams.instagram.serializer.util.InstagramActivityUtil.updateActivity;
import static org.hamcrest.CoreMatchers.*;
@@ -50,7 +52,7 @@ public class InstagramActivitySerDeTest {
private final static Logger LOGGER = LoggerFactory.getLogger(InstagramActivitySerDeTest.class);
@Test
- public void Tests() {
+ public void TestMediaFeedObjects() {
InstagramDeserializer instagramDeserializer = new InstagramDeserializer("");
InputStream is = InstagramActivitySerDeTest.class.getResourceAsStream("/testMediaFeedObjects.txt");
InputStreamReader isr = new InputStreamReader(is);
@@ -85,4 +87,53 @@ public class InstagramActivitySerDeTest {
Assert.fail();
}
}
+
+ @Test
+ public void TestUserInfoData() {
+ InstagramDeserializer instagramDeserializer = new InstagramDeserializer("");
+ InputStream is = InstagramActivitySerDeTest.class.getResourceAsStream("/testUserInfoData.txt");
+ InputStreamReader isr = new InputStreamReader(is);
+ BufferedReader br = new BufferedReader(isr);
+
+ try {
+ while (br.ready()) {
+ String line = br.readLine();
+ if(!StringUtils.isEmpty(line))
+ {
+ LOGGER.info("raw: {}", line);
+
+ UserInfoData userInfoData = instagramDeserializer.createObjectFromResponse(UserInfoData.class, line);
+
+ Activity activity = new Activity();
+
+ LOGGER.info("activity: {}", activity.toString());
+
+ updateActivity(userInfoData, activity);
+ assertThat(activity, is(not(nullValue())));
+
+ assertThat(activity.getId(), is(nullValue()));
+ assertThat(activity.getActor(), is(not(nullValue())));
+ assertThat(activity.getActor().getImage(), is(not(nullValue())));
+ assertThat(activity.getActor().getDisplayName(), is(not(nullValue())));
+ assertThat(activity.getActor().getSummary(), is(not(nullValue())));
+
+ Map<String, Object> extensions = (Map<String, Object>)activity.getActor().getAdditionalProperties().get("extensions");
+ assertThat(extensions, is(not(nullValue())));
+ assertThat(extensions.get("follows"), is(not(nullValue())));
+ assertThat(extensions.get("followers"), is(not(nullValue())));
+ assertThat(extensions.get("screenName"), is(not(nullValue())));
+
+ assertThat(activity.getActor().getAdditionalProperties().get("handle"), is(not(nullValue())));
+ assertThat(activity.getActor().getId(), is(not(nullValue())));
+ assertThat(activity.getActor().getUrl(), is(not(nullValue())));
+ assertThat(activity.getVerb(), is(not(nullValue())));
+ assertThat(activity.getProvider(), is(not(nullValue())));
+ }
+ }
+ } catch( Exception e ) {
+ System.out.println(e);
+ e.printStackTrace();
+ Assert.fail();
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/119608d2/streams-contrib/streams-provider-instagram/src/test/resources/testUserInfoData.txt
----------------------------------------------------------------------
diff --git a/streams-contrib/streams-provider-instagram/src/test/resources/testUserInfoData.txt b/streams-contrib/streams-provider-instagram/src/test/resources/testUserInfoData.txt
new file mode 100644
index 0000000..679de91
--- /dev/null
+++ b/streams-contrib/streams-provider-instagram/src/test/resources/testUserInfoData.txt
@@ -0,0 +1,2 @@
+{"bio":"This is the official Instagram account for Speaker John Boehner, a regular guy with a big job.","counts":{"follows":72,"follwed_by":4844,"media":129},"first_name":null,"id":249235106,"last_name":null,"profile_picture":"http://images.ak.instagram.com/profiles/profile_249235106_75sq_1388678585.jpg","username":"speakerboehner","full_name":"Speaker John Boehner","website":"http://speaker.gov"}
+{"bio":"","counts":{"follows":37,"follwed_by":3083,"media":70},"first_name":null,"id":44979810,"last_name":null,"profile_picture":"http://images.ak.instagram.com/profiles/profile_44979810_75sq_1335477964.jpg","username":"senjohnmccain","full_name":"John McCain","website":"http://mccain.senate.gov"}
\ No newline at end of file