Last active
December 11, 2015 04:39
-
-
Save wemakeweb/4546645 to your computer and use it in GitHub Desktop.
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 twitter = require('twit'); | |
var http = require('http'); | |
var https = require('https'); | |
var feed = new twitter({ | |
consumer_key : '...', | |
consumer_secret : '...', | |
access_token : '...', | |
access_token_secret : '...' | |
}); | |
var itunes_options = { | |
host: 'itunes.apple.com', | |
port: 443, | |
method: 'GET', | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
}; | |
var app = { | |
start : function(){ | |
http.createServer(function (request, response) { | |
app.getFeed(response); | |
}).listen(1138); | |
console.log("Server running"): | |
}, | |
getFeed : function(res){ | |
feed.get('statuses/user_timeline', function (err, twitterData) { | |
if(err){ | |
return res.end("Error"); | |
} | |
app.processFedd(twitterData, res); | |
}); | |
}, | |
processFedd : function(twitterData, res){ | |
var tweets2WaitFor = 0, | |
output = []; | |
for (var i = 0, m = twitterData.length; i < m; i += 1) { | |
// Shows the twenty latest tweets of @cratemag | |
// console.log(twitterData[i].text); | |
if (twitterData[i].entities.hashtags.length >= 1) { | |
for (var j = 0, mm = twitterData[i].entities.hashtags.length; j < mm; j += 1) { | |
tweets2WaitFor++; | |
(function(tweet, hashtag){ | |
app.getGames(hashtag , function(game){ | |
output.push({ | |
"tweet" : { | |
"text" : tweet.text, | |
"screen_name" : '', | |
"user" : { | |
"profile_image_url" : '' | |
} | |
}, | |
game : game | |
}]; | |
check(); | |
}); | |
})(twitterData[i], twitterData[i].entities.hashtags[j]); | |
} | |
} | |
} | |
function check(){ | |
if( (tweets2WaitFor -1) === 0 ){ | |
res.end(JSON.stringify(output)); | |
} else { | |
tweets2WaitFor--; | |
} | |
}; | |
}, | |
getGames : function(twitterData, callback){ | |
var itunesOutput = ""; | |
itunes_options.path = '/search?term=' + twitterData.text + '&country=DE&entity=software'; | |
https.get(itunes_options, function (res) { | |
res.on('data', function (itunesData) { | |
itunesOutput += itunesData; | |
}); | |
res.on('end', function () { | |
itunesOutput = JSON.parse(itunesOutput); | |
var game = app.filterGame( itunesOutput ); | |
callback( game ); | |
}); | |
res.on('error', function(){ | |
res.end("error"); | |
}); | |
}); | |
}, | |
filterGame : function(data){ | |
if (data.resultCount >= 1) { | |
for (var k = 0, mmm = data.resultCount; k < mmm; k += 1) { | |
if (data.results[k].primaryGenreName == 'Games') { | |
return { | |
"name" : data.results[k].trackName, | |
"app_icon_100px" : data.results[k].artworkUrl100, | |
"formattedPrice" : data.results[k].formattedPrice | |
}; | |
} | |
} | |
} | |
}, | |
}; | |
app.start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment