Skip to content

Commit

Permalink
less logging, mongo users fields update fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sorin-costea committed Oct 25, 2016
1 parent 974e77e commit 254893d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 24 deletions.
11 changes: 11 additions & 0 deletions .project
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,15 @@
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures>
<filteredResources>
<filter>
<id>1473288245333</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.ui.ide.multiFilter</id>
<arguments>1.0-name-matches-true-false-.vertx</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
61 changes: 38 additions & 23 deletions src/main/java/org/sorincos/mytumble/TumblrConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ private void likeLatest(Message<JsonObject> msg) {
String toLike = msg.body().getString("name");
try {
JumblrClient client = vertx.getOrCreateContext().get("jumblrclient");
if (client == null)
if (client == null) {
future.fail("Error: Jumblr not initialized");
return;
}

Map<String, Object> params = new HashMap<String, Object>();
List<Post> posts = client.blogPosts(toLike, params);
Expand All @@ -111,8 +113,15 @@ private void likeLatest(Message<JsonObject> msg) {
break;
}
}
if (!liked)
if (!liked) {
logger.info("Reblogger or quiet: " + toLike + " had nothing to like among latest " + posts.size());
for (Post post : posts) { // like the first reblog eh
if (!post.isLiked()) { // jumblr bug, urls are unusable
client.like(post.getId(), post.getReblogKey());
break;
}
}
}
future.complete(msg.body());
} catch (Exception ex) {
ex.printStackTrace();
Expand All @@ -132,8 +141,10 @@ private void followBlog(Message<String> msg) {
logger.info("Follow " + toFollow);
try {
JumblrClient client = vertx.getOrCreateContext().get("jumblrclient");
if (client == null)
if (client == null) {
future.fail("Error: Jumblr not initialized");
return;
}

client.follow(toFollow); // no return here
future.complete(toFollow);
Expand All @@ -155,9 +166,10 @@ private void unfollowBlog(Message<String> msg) {
logger.info("Unfollow " + toUnfollow);
try {
JumblrClient client = vertx.getOrCreateContext().get("jumblrclient");
if (client == null)
if (client == null) {
future.fail("Error: Jumblr not initialized");

return;
}
client.unfollow(toUnfollow); // no return here
future.complete(toUnfollow);
} catch (Exception ex) {
Expand All @@ -178,19 +190,21 @@ private void loadPosts(Message<JsonArray> msg) {
logger.info("Posts for " + blogname);
try {
JumblrClient client = vertx.getOrCreateContext().get("jumblrclient");
if (client == null)
if (client == null) {
future.fail("Error: Jumblr not initialized");

return;
}
Blog myBlog = null;
for (Blog blog : client.user().getBlogs()) {
if (blog.getName().compareTo(blogname) == 0) {
myBlog = blog;
break;
}
}
if (myBlog == null)
if (myBlog == null) {
future.fail("Error: Blog name not found - " + blogname);

return;
}
int howMany = msg.body().getInteger(0);
Map<String, Object> params = new HashMap<String, Object>();
params.put("limit", howMany);
Expand Down Expand Up @@ -237,26 +251,28 @@ private void loadUserDetails(Message<JsonArray> msg) {
vertx.<JsonArray>executeBlocking(future -> {
try {
JsonArray jsonFollowers = msg.body();
if (jsonFollowers.isEmpty())
if (jsonFollowers.isEmpty()) {
future.fail("No details to load");
return;
}
JumblrClient client = vertx.getOrCreateContext().get("jumblrclient");
if (client == null)
if (client == null) {
future.fail("Error: Jumblr not initialized");

return;
}
int count = 0;
loopUsers(jsonFollowers, client, count);
vertx.eventBus().send("mytumble.web.status", "Fetched details from Tumblr");
future.complete(jsonFollowers);
} catch (Exception ex) {
ex.printStackTrace();
vertx.eventBus().send("mytumble.web.status", "Fetching details failed: " + ex.getLocalizedMessage());
future.fail(ex.getLocalizedMessage());
}
}, result -> {
if (result.succeeded()) {
vertx.eventBus().send("mytumble.web.status", "Fetched details from Tumblr");
msg.reply(result.result());
} else {
vertx.eventBus().send("mytumble.web.status",
"Fetching details failed: " + result.cause().getLocalizedMessage());
msg.fail(1, result.cause().getLocalizedMessage());
}
});
Expand All @@ -277,8 +293,6 @@ private int loopUsers(JsonArray jsonFollowers, JumblrClient client, final int co
vertx.eventBus().send("mytumble.mongo.saveuser", jsonFollower);
loopUsers(jsonFollowers, client, count + 1);
});
} else {
vertx.eventBus().send("mytumble.web.status", "Finished details from Tumblr");
}
return count;
}
Expand All @@ -289,18 +303,21 @@ private void loadUsers(Message<JsonArray> msg) {
try {
long now = new Date().getTime();
JumblrClient client = vertx.getOrCreateContext().get("jumblrclient");
if (client == null)
if (client == null) {
future.fail("Error: Jumblr not initialized");
return;
}

for (Blog blog : client.user().getBlogs()) {
if (blog.getName().compareTo(blogname) == 0) {
myBlog = blog;
break;
}
}
if (myBlog == null)
if (myBlog == null) {
future.fail("Error: Blog name not found - " + blogname);

return;
}
Map<String, JsonObject> mapUsers = new HashMap<>();
Map<String, String> options = new HashMap<String, String>();
int offset = 0, fetched = 0;
Expand Down Expand Up @@ -353,9 +370,7 @@ private void loadUsers(Message<JsonArray> msg) {
ex.printStackTrace();
future.fail(ex.getLocalizedMessage());
}
}, result ->

{
}, result -> {
if (result.succeeded()) {
vertx.eventBus().send("mytumble.mongo.saveusers", result.result(), saved -> {
vertx.eventBus().send("mytumble.web.status", "Refreshed from Tumblr");
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/webroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<button ng-click="loadUsers('followsme,notweird,notifollow')">To
follow back</button>
<button
ng-click="loadUsers('notspecial,notfollowsme,ifollow,notweird')"
ng-click="loadUsers('notspecial,notfollowsme,ifollow')"
>Should be following me</button>
<button ng-click="unfollowAsocials()">Unfollow asocials</button>
<button ng-click="loadUsers('likers,notweird')">Likers</button>
Expand Down

0 comments on commit 254893d

Please sign in to comment.