Created
August 17, 2018 09:42
-
-
Save dispeakble/4c10bb0eb5c01a8e51ce51ba51fea165 to your computer and use it in GitHub Desktop.
ai net.js
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 net = { | |
objects:{ | |
request:require('request'), | |
cheerio:require('cheerio') | |
}, | |
actions:{ | |
search:function(params){ | |
return new Promise(function(resolve_search, reject_search){ | |
var url = 'http://api.pearson.com/v2/dictionaries/entries?headword=' + params.data; | |
net.objects.request(url, function(error, response, html){ | |
if(!error){ | |
var response = { | |
hash:params.hash, | |
data:JSON.parse(html), | |
what:'string' | |
}; | |
var definitions = []; | |
for(var i = 0, t=response.data.results.length; i < t; i++){ | |
if (response.data.results[i].senses && response.data.results[i].senses.length) { | |
if(!!response.data.results[i].senses[0].definition){ | |
if(typeof response.data.results[i].senses[0].definition == 'object'){ | |
definitions.push(response.data.results[i].senses[0].definition[0]); | |
} else { | |
definitions.push(response.data.results[i].senses[0].definition); | |
} | |
} | |
} | |
} | |
response.data = definitions; | |
resolve_search(response); | |
/*var $ = net.objects.cheerio.load(html); | |
$('#mw-content-text ol').filter(function(){ | |
var data = $(this); | |
var values = []; | |
data.children().each(function(i,e){ | |
values.push($(this).text()); | |
}); | |
var result = data.children().first(); | |
result.find('ul').remove(); | |
result.find('.HQToggle').remove(); | |
var response = { | |
hash:params.hash, | |
data:html, | |
what:'string' | |
}; | |
resolve_search(response); | |
})*/ | |
} | |
}) | |
}); | |
} | |
} | |
}; | |
module.exports = net; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment