Skip to content

Commit

Permalink
Merge pull request duckduckgo#1959 from altern8tif/spice_news
Browse files Browse the repository at this point in the history
News - Update to latest Spice API
  • Loading branch information
moollaza committed Jul 2, 2015
2 parents dba9f3d + 0cfee12 commit 140ffeb
Showing 1 changed file with 66 additions and 68 deletions.
134 changes: 66 additions & 68 deletions share/spice/news/news.js
Original file line number Diff line number Diff line change
@@ -1,100 +1,98 @@
function ddg_spice_news(apiResult) {
(function (env) {
"use strict";
env.ddg_spice_news = function (api_result) {

// Words that we have to skip in DDG.isRelevant.
var skip = [
"news",
"headline",
"headlines",
"latest",
"breaking",
"update",
"s:d",
"sort:date"
];
// Words that we have to skip in DDG.isRelevant.
var skip = [
"news",
"headline",
"headlines",
"latest",
"breaking",
"update",
"s:d",
"sort:date"
];

// Some sources need to be set by us.
var setSourceOnStory = function(story) {
switch(story.syndicate) {
case "Topsy":
story.source = story.author || "Topsy";
break;
case "NewsCred":
if(story.source) {
if(story.author) {
story.source = story.source + " by " + story.author;
// Some sources need to be set by us.
var setSourceOnStory = function(story) {
switch(story.syndicate) {
case "Topsy":
story.source = story.author || "Topsy";
break;
case "NewsCred":
if(story.source) {
if(story.author) {
story.source = story.source + " by " + story.author;
}
} else {
story.source = "NewsCred";
}
} else {
story.source = "NewsCred";
break;
}
break;
}
};
};

var entityWords = [];
if (Spice.news && Spice.news.entities && Spice.news.entities.length) {
for (var j = 0, entity; entity = Spice.news.entities[j]; j++) {
var tmpEntityWords = entity.split(" ");
for (var k = 0, entityWord; entityWord = tmpEntityWords[k]; k++) {
if (entityWord.length > 3) {
entityWords.push(entityWord);
var entityWords = [];
if (Spice.news && Spice.news.entities && Spice.news.entities.length) {
for (var j = 0, entity; entity = Spice.news.entities[j]; j++) {
var tmpEntityWords = entity.split(" ");
for (var k = 0, entityWord; entityWord = tmpEntityWords[k]; k++) {
if (entityWord.length > 3) {
entityWords.push(entityWord);
}
}
}
}
}

// Check if the title is relevant to the query.
var goodStories = [];
for(var i = 0, story; story = apiResult[i]; i++) {
// strip bold from story titles.
story.title = story.title.replace(/<b>|<\/b>|:/g, "");
if (DDG.isRelevant(story.title, skip)) {
setSourceOnStory(story);
goodStories.push(story);
}
// Check if the title is relevant to the query.
var goodStories = [];
for(var i = 0, story; story = api_result[i]; i++) {
// strip bold from story titles.
story.title = story.title.replace(/<b>|<\/b>|:/g, "");
if (DDG.isRelevant(story.title, skip)) {
setSourceOnStory(story);
goodStories.push(story);
}

// additional news relevancy for entities. story need only
// contain one word from one entity to be good. strict indexof
// check though.
else if (entityWords.length > 0) {
var storyOk = 0;
var tmpStoryTitle = story.title.toLowerCase();
// additional news relevancy for entities. story need only
// contain one word from one entity to be good. strict indexof
// check though.
else if (entityWords.length > 0) {
var storyOk = 0;
var tmpStoryTitle = story.title.toLowerCase();

for (var k = 0, entityWord; entityWord = entityWords[k]; k++) {
if (tmpStoryTitle.indexOf(entityWord) !== -1) {
storyOk = 1;
break;
for (var k = 0, entityWord; entityWord = entityWords[k]; k++) {
if (tmpStoryTitle.indexOf(entityWord) !== -1) {
storyOk = 1;
break;
}
}
}

if (storyOk) {
setSourceOnStory(story);
goodStories.push(story);
if (storyOk) {
setSourceOnStory(story);
goodStories.push(story);
}
}
}
}

var searchTerm = DDG.get_query().replace(/(?: news|news ?)/i, '').trim();
var searchTerm = DDG.get_query().replace(/(?: news|news ?)/i, '').trim();

if (api_result.error || goodStories >= 3) {
Spice.failed('news');
}

// If we found a few good stories, display them.
if(goodStories.length >= 3) {
Spice.add({
id: 'news',
name: 'News',

data: goodStories,

meta: {
count: goodStories.length,
searchTerm: searchTerm,
itemType: 'News articles'
},

templates: {
item: 'news_item'
}
});
} else {
Spice.failed('news');
}
}
}(this));

0 comments on commit 140ffeb

Please sign in to comment.