Created
December 1, 2012 21:35
-
-
Save joshtwist/4185248 to your computer and use it in GitHub Desktop.
Azure Mobile Services script to add name and imageUrl properties on insert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var req = require('request'); | |
| function insert(item, user, request) { | |
| item.userId = user.userId; | |
| if (user.userId.indexOf('Twitter:') === 0) { | |
| var twitterId = user.userId.replace('Twitter:', ''); | |
| req.get('https://api.twitter.com/1/users/show.json?user_id=' + twitterId, | |
| function(error, result, body) { | |
| item.imageUrl = ''; | |
| if (error || result.statusCode != 200) { console.error(error || result); } | |
| var json = JSON.parse(body); | |
| if(json.profile_image_url){ | |
| var biggerImg = json.profile_image_url.replace("normal","bigger"); | |
| item.imageUrl = biggerImg; | |
| } | |
| item.name = json.name; | |
| request.execute(); | |
| }); | |
| } | |
| else { | |
| request.execute(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is for demonstration purposes, needs more robust error handling.