File size: 3,161 Bytes
ae4627d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
var elements = [
'<input class="form-control" placeholder="ddg search for img" type="text">',
'<div id="results">'
],
q, counting, ddgResults;
$('body').append(elements);
$('input').focus();
$('input').on('keyup', function (event) {
if (event.which == 13) {
q = ($('input').val());
ddg(q);
}
});
function ddg(q) {
$.ajax({
type:'GET',
url:'https://api.duckduckgo.com/',
data: {'q': q, format:'json',skip_disambig:1},
dataType:'jsonp',
error: function(data){console.log("Femto => error: ",data);}
}).done(function (data) {
if (data) {
// $('#results').text(JSON.stringify(data));
ddgResults = data;
results = findMusicTopic(data);
scores = wordCounting( results );
relevantTopic = chooseRelevantTopic(results,scores);
imgURL = findImage(relevantTopic,data);
$('body').append('<img src="'+imgURL+'">');
console.log(scores,relevantTopic,imgURL);
} else {
alert('empty response');
}
});
}
function findMusicTopic(results){
for(var index in results.RelatedTopics){
try{
if(results.RelatedTopics[index].Name === "Music"){
console.log(results.RelatedTopics[index]);
return results.RelatedTopics[index].Topics;
}else{
return results.RelatedTopics;
}
} catch(err){
// console.log(err.message);
return false;
}
}
}
function wordCounting(topicsArray){
if(topicsArray){
kwords = {
// title:{
// kw: /(morning)|(glory)|(oasis)/gi,
// count:0
// },
songs:{
kw:/song/gi,
},
bands:{
kw:/band/gi,
},
singers:{
kw:/singer/gi,
},
songwriters:{
kw:/songwriter/gi,
},
musicians:{
kw:/musician/gi,
},
albums:{
kw:/album/gi,
}
};
var counted = [];
for(var ddg in topicsArray){
topicid = ddg;
counted[topicid] = {"topicid": topicid,score: 0};
for(var word in kwords){
try{
counted[topicid].score = counted[topicid].score + topicsArray[ddg].Text.match(kwords[word].kw).length;
// console.log(word,string.Topics[ddg].Text.match(kwords[word].kw).length);
}catch(err){
console.log(err.message);
}
}
// console.log(topicid, string.Topics[ddg].Text);
}
return counted;
}else{
console.log("Femto => wordCounting: ",topicsArray);
return false;
}
}
function chooseRelevantTopic(results,scores){
if(results && scores){
var max = 0;
var topicid = null;
for(var index in scores){
if( scores[index].score > max){
max = scores[index].score;
topicid = scores[index].topicid;
}
}
return results[topicid];
}else{
console.log(results,scores);
}
}
function findImage(topicsArray,data){
if(topicsArray){
if(topicsArray.Icon.URL){
return topicsArray.Icon.URL;
}else{
return fallBack(data);
}
}else{
return fallBack(data);
}
function fallBack(data){
if(data.Image){
return data.Image;
}else{
return false;
}
}
} |