Skip to content

Instantly share code, notes, and snippets.

@timgshi
Created November 22, 2012 00:42
Show Gist options
  • Select an option

  • Save timgshi/4128732 to your computer and use it in GitHub Desktop.

Select an option

Save timgshi/4128732 to your computer and use it in GitHub Desktop.
This uses Parse Cloud Code to attempt to save instagram relationships from an instagram json object.
// Use Parse.Cloud.define to define as many cloud functions as you want.
// For example:
Parse.Cloud.define("hello", function(request, response) {
response.success("Hello world!");
});
Parse.Cloud.define("receiveinstagram", function(request, response) {
var photo = request.params.data;
var query = new Parse.Query("instagram_moment");
query.equalTo("instagram_id", photo.id);
query.find({
success: function(results) {
console.log("QUERY RESULTS:" + results);
var obj;
if (results.length > 0) {
obj = results[0];
} else {
var Moment = Parse.Object.extend("instagram_moment");
obj = new Moment();
}
console.log("object: " + obj);
obj.set("instagram_id", photo.id);
obj.set("instagram_url", photo.images.standard_resolution.url);
obj.set("instagram_url_thumb", photo.images.thumbnail.url);
obj.set("instagram_timestamp", photo.created_time);
var caption = photo.caption.text;
if (caption != null) {
obj.set("instagram_caption", photo.caption.text);
}
obj.set("instagram_filter", photo.filter);
obj.set("coordinates", new Parse.GeoPoint({latitude: photo.location.latitude, longitude: photo.location.longitude}));
obj.set("instagram_link", photo.link);
var tags = [];
var likes = [];
obj.set("instagram_tags", tags);
obj.set("instagram_likes", likes);
obj.save(null, {
success: function(moment) {
console.log("moment saved correctly: " + moment.id);
var IGLocation = Parse.Object.extend("instagram_location");
var locationObj = new IGLocation();
locationObj.set("obj", obj);
locationObj.set("instagram_id", photo.location.id);
locationObj.set("instagram_name", photo.location.name);
locationObj.set("coordinates", new Parse.GeoPoint({latitude: photo.location.latitude, longitude: photo.location.longitude}));
locationObj.save();
var User = Parse.Object.extend("instagram_user");
var userObj = new User();
userObj.set("instagram_username", photo.user.username);
userObj.set("instagram_full_name", photo.user.full_name);
userObj.set("instagram_id", photo.user.id);
userObj.set("instagram_profile_picture", photo.user.profile_picture);
userObj.set("obj", obj);
userObj.set("user", true);
userObj.save();
saveLikes(photo.likes.data, obj, photo, response);
// saveComments(photo.comments.data, obj, photo, response);
},
error: function(moment, error) {
response.error("moment save error: " + error.message);
}
});
},
error: function(error) {
response.error("moment lookup failed");
}
});
// response.error("never entered loop");
});
function saveLikes(likes, obj, photo, response) {
if (likes.length == 0) {
saveTags(photo.tags, obj, response);
// saveComments(photo.comments.data, obj, photo, response);
} else {
console.log("in likes function: " + likes);
var like = likes.pop();
var User = Parse.Object.extend("instagram_user");
var userObj = new User();
userObj.set("instagram_username", like.username);
userObj.set("instagram_full_name", like.full_name);
userObj.set("instagram_id", like.id);
userObj.set("instagram_profile_picture", like.profile_picture);
userObj.set("obj", obj);
userObj.save(null, {
success: function(newTag) {
console.log("user save success before recursive call");
saveLikes(likes, obj, photo, response);
},
error: function(newTag, error) {
console.log("user save error before recursive call");
saveLikes(likes, obj, photo, response);
}
});
}
}
function saveTags(tags, obj, response) {
if (tags.length == 0) {
response.success("saved after running tags");
}
console.log("in tags function: " + tags);
var tag = tags.pop();
var Tag = Parse.Object.extend("instagram_tag");
var tagObj = new Tag();
tagObj.set("text", tag);
tagObj.set("obj", obj);
tagObj.save(null, {
success: function(newTag) {
console.log("tag save success before recursive call");
saveTags(tags, obj, response);
},
error: function(newTag, error) {
console.log("tag save error before recursive call");
saveTags(tags, obj, response);
}
});
}
function saveComments(comments, obj, photo, response) {
console.log("in comments function");
if (comments.length == 0) {
saveTags(photo.tags, obj, response);
} else {
var comment = comments.pop();
var Comment = Parse.Object.extend("instagram_comment");
var commentObj = new Comment();
commentObj.set("text", comment.text);
commentObj.set("instagram_id", comment.id);
commentObj.set("instagram_timestamp", comment.created_time);
commentObj.set("obj", obj);
commentObj.save(null, {
success: function(newComment) {
var User = Parse.Object.extend("instagram_user");
var userObj = new User();
userObj.set("instagram_username", comment.from.username);
userObj.set("instagram_full_name", comment.from.full_name);
userObj.set("instagram_id", comment.from.id);
userObj.set("instagram_profile_picture", comment.from.profile_picture);
userObj.set("obj", commentObj);
userObj.set("user", true);
userObj.save();
console.log("comment save success before recursive call");
saveComments(comments, obj, photo, response);
},
error: function(newComment, error) {
var User = Parse.Object.extend("instagram_user");
var userObj = new User();
userObj.set("instagram_username", comment.from.username);
userObj.set("instagram_full_name", comment.from.full_name);
userObj.set("instagram_id", comment.from.id);
userObj.set("instagram_profile_picture", comment.from.profile_picture);
userObj.set("obj", commentObj);
userObj.set("user", true);
userObj.save();
console.log("comment save error before recursive call");
saveComments(comments, obj, photo, response);
}
});
}
}
function beforeSaveRelationship(request, response, queryParam, className, targetParam, relationName) {
var param = request.object.get(queryParam);
var photoObj = request.object.get(targetParam);
var tagQuery = new Parse.Query(className);
tagQuery.equalTo(queryParam, param);
console.log("searching for " + className);
tagQuery.find({
success: function(results) {
var obj;
if (results != null) {
if (results.length > 0) {
obj = results[0];
console.log(className + " object already found id: " + obj.id);
if (photoObj != null) {
photoObj.fetch({
success: function(fetchedObj) {
// request.object.unset(targetParam);
console.log("fetched photoObj: " + fetchedObj);
var relation = fetchedObj.relation(relationName);
relation.add(obj);
console.log("set relation");
fetchedObj.save(null, {
success: function(){
response.error(obj.id);
},
error: function (error) {
response.error(error.message);
}
});
},
error: function(photoObj, error) {
response.error("couldn't fetch photo: " + error.message);
}
});
} else {
response.error(obj.id);
}
} else {
console.log("succeeding on save");
response.success();
}
} else {
console.log("succeeding on save");
response.success();
}
},
error: function(error) {
console.log("tag query error: " + error);
response.error(error);
}
});
}
function afterSaveRelationship(request, relationName, targetParam) {
var photoObj = request.object.get(targetParam);
if (photoObj != null) {
photoObj.fetch({
success: function(fetchedObj) {
console.log("fetched photoObj: " + fetchedObj);
var relation = fetchedObj.relation(relationName);
relation.add(request.object);
console.log("set relation");
fetchedObj.save(null, {
success: function(){
request.object.unset("obj");
request.object.save();
},
error: function (error) {
request.object.unset("obj");
request.object.save();
throw "couldn't save relation: " + error.message;
}
});
},
error: function(photoObj, error) {
throw "couldn't fetch photo: " + error.message;
}
});
}
}
Parse.Cloud.beforeSave("instagram_tag", function(request, response) {
beforeSaveRelationship(request, response, "text", "instagram_tag", "obj", "tags");
});
Parse.Cloud.afterSave("instagram_tag", function(request) {
afterSaveRelationship(request, "tags", "obj");
});
Parse.Cloud.beforeSave("instagram_comment", function(request, response) {
beforeSaveRelationship(request, response, "instagram_id", "instagram_comment", "obj", "comments");
});
Parse.Cloud.afterSave("instagram_comment", function(request) {
afterSaveRelationship(request, "comments", "obj");
});
Parse.Cloud.beforeSave("instagram_location", function(request, response) {
beforeSaveRelationship(request, response, "instagram_id", "instagram_location", "obj", "location");
});
Parse.Cloud.afterSave("instagram_location", function(request) {
afterSaveRelationship(request, "location", "obj");
});
Parse.Cloud.beforeSave("instagram_user", function(request, response) {
if (request.object.get("obj") != null) {
if (request.object.get("user") != null) {
console.log("creating user relationship");
beforeSaveRelationship(request, response, "instagram_id", "instagram_user", "obj", "instagram_user");
} else {
console.log("creating like relationship");
beforeSaveRelationship(request, response, "instagram_id", "instagram_user", "obj", "likes");
}
} else {
response.success();
}
});
Parse.Cloud.afterSave("instagram_user", function(request) {
if (request.object.get("obj") != null) {
if (request.object.get("user") != null) {
console.log("creating user relationship after save");
afterSaveRelationship(request, "instagram_user", "obj");
} else {
console.log("creating like relationship after save");
afterSaveRelationship(request, "likes", "obj");
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment