Skip to content

Instantly share code, notes, and snippets.

@drFabio
Created February 14, 2016 21:55
Show Gist options
  • Save drFabio/9014d11cbb69d7c4b4c8 to your computer and use it in GitHub Desktop.
Save drFabio/9014d11cbb69d7c4b4c8 to your computer and use it in GitHub Desktop.
Search for a tech on gitHub and save it somewhere
var http = require('https');
var MongoClient = require('mongodb');
var url = require('url');
function get(path,cb){
var config = url.parse(path);
config.headers = {'user-agent': 'drFabio'};
http.get(config , function(response) {
var body = '';
response.on('data', function(d) {
body += d;
});
response.on('end', function() {
var parsed = JSON.parse(body);
cb(null,parsed);
});
});
}
function queryGithub(tech,cb){
var url = 'https://api.github.com/search/repositories?q='+tech+'&sort=stars&order=desc&per_page=3'
get(url,function(err, response){
if(err){
return(cb(err));
}
var resp = getGitHubRepresentatives(tech,response);
cb(null, resp);
});
}
function getGitHubRepresentatives(tech,response){
var techName = tech.toLowerCase();
var total = response.total_count;
var size = response.items.length;
var totalStars = 0;
var repoData = [];
var item;
for(var i=0; i< size; i++){
item = response.items[i];
console.log("ITEM");
console.log(item);
var hasTech = item.description.toLowerCase().indexOf(techName)>-1
|| item.name.toLowerCase().indexOf(techName) > -1;
if(!hasTech){
continue;
}
repoData.push({
name: item.name,
full_name: item.full_name,
html_url: item.html_url,
description: item.description,
homepage: item.homepage,
stargazers_count: item.stargazers_count,
watchers_count: item.watchers_count
});
totalStars+= item.stargazers_count;
}
if(repoData.length ==0){
return null;
}
return {total:response.total_count , repos: repoData, totalStars:totalStars};
}
function saveTech(db,data,cb){
db
.collection('techs')
.insertOne( data, function(err, result) {
if(err){
return cb(err);
}
return cb(null,'Success.')
});
}
module.exports = function (ctx, done) {
MongoClient.connect(ctx.data.MONGO_URL, function (err, db) {
if(err){
return done(err);
}
queryGithub(ctx.data.tech,function(err,data){
if(err){
return done(err);
}
if(data == null){
return done(null,'Success.');
}
saveTech(db,data,done);
})
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment