google-news-json
Advanced tools
Comparing version 1.0.3 to 2.0.0
104
lib/index.js
@@ -8,2 +8,17 @@ const GEO = "geo"; | ||
let Topics = function() { | ||
if (this.constructor === Topics) { | ||
throw new Error("Can't instantiate abstract class"); | ||
} | ||
}; | ||
Topics.prototype.WORLD = "WORLD"; | ||
Topics.prototype.NATION = "NATION"; | ||
Topics.prototype.BUSINESS = "BUSINESS"; | ||
Topics.prototype.TECHNOLOGY = "TECHNOLOGY"; | ||
Topics.prototype.ENTERTAINMENT = "ENTERTAINMENT"; | ||
Topics.prototype.SCIENCE = "SCIENCE"; | ||
Topics.prototype.SCIENCE = "SPORTS"; | ||
Topics.prototype.SCIENCE = "HEALTH"; | ||
const _generateGoogleFeedURL = function(method, query, locale){ | ||
@@ -19,3 +34,3 @@ let feedUrl = "https://news.google.com/rss"; | ||
case "search": | ||
feedUrl += "/search?q="+query+"&"; | ||
feedUrl += encodeURI("/search?q="+query+"&"); | ||
break; | ||
@@ -33,2 +48,53 @@ default: | ||
let _parseArr = function(arr){ | ||
if(Array.isArray(arr)) { | ||
arr = _shiftArr(arr); | ||
} | ||
if(Array.isArray(arr)) { | ||
for(let i=0; i < (arr || []).length; i++){ | ||
arr[i] = _parseArr(arr[i]); | ||
} | ||
} else if(typeof arr === 'object') { | ||
if(arr['_']) arr['text'] = arr['_']; | ||
if(arr['$']) arr = Object.assign({}, arr, arr['$']); | ||
delete arr['_']; | ||
delete arr['$']; | ||
let keys = Object.keys(arr); | ||
for(let n=0; n<keys.length; n++){ | ||
if(Array.isArray(arr[keys[n]])) arr[keys[n]] = _parseArr(arr[keys[n]]); | ||
} | ||
} | ||
return arr; | ||
}; | ||
let _shiftArr = function(arr){ | ||
if(!arr || arr.length > 1) return arr; | ||
return arr.shift(); | ||
}; | ||
let request = function(url, callback){ | ||
let http = require('https'); | ||
http.get(url, function(res){ | ||
res.on('error',function(err){ | ||
callback(err); | ||
}); | ||
let data = ''; | ||
res.on('data',function(chunk){ | ||
data += chunk.toString(); | ||
}); | ||
res.on('end',function(){ | ||
// follow redirect is status code is 302 and new location exists | ||
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) { | ||
request(res.headers.location, callback); | ||
return; | ||
} | ||
callback(null, data); | ||
}); | ||
}); | ||
}; | ||
/** | ||
@@ -51,12 +117,34 @@ * Get Google's News Feed | ||
// noinspection JSUnresolvedFunction | ||
const Feed = require('rss-to-json'); | ||
// noinspection JSUnresolvedFunction | ||
Feed.load(feedUrl, function(err, rss){ | ||
if(typeof callback === "function") callback(err, rss); | ||
err ? reject(err) : resolve(rss); | ||
request(feedUrl, function(err, data){ | ||
if(err){ | ||
if(typeof callback === "function") callback(err); | ||
return reject(err); | ||
} | ||
// noinspection JSUnresolvedFunction,NodeJsCodingAssistanceForCoreModules | ||
let xml2js = require('xml2js'); | ||
xml2js.parseString(data, function (err, result) { | ||
if(err){ | ||
if(typeof callback === "function") callback(err); | ||
return reject(err); | ||
} | ||
let rss = _parseArr(result['rss']['channel']); | ||
if(typeof callback === "function") callback(err, rss); | ||
resolve(rss); | ||
}); | ||
}); | ||
// // noinspection JSUnresolvedFunction,NodeJsCodingAssistanceForCoreModules | ||
// let http = require('https'); | ||
// | ||
// // noinspection JSUnresolvedFunction | ||
// const feed2json = require('feed2json'); | ||
// feed2json.fromStream(http.get(feedUrl), feedUrl, (err, rss) => { | ||
// if(typeof callback === "function") callback(err, rss); | ||
// err ? reject(err) : resolve(rss); | ||
// }); | ||
}); | ||
}; | ||
module.exports = { HIGHLIGHTS, TOP_NEWS, LOCATION, SEARCH, TOPIC, GEO, getNews }; | ||
module.exports = { HIGHLIGHTS, TOP_NEWS, LOCATION, SEARCH, TOPIC, GEO, getNews, Topics }; |
{ | ||
"name": "google-news-json", | ||
"version": "1.0.3", | ||
"version": "2.0.0", | ||
"description": "Simple to use google news api", | ||
@@ -17,7 +17,7 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"rss-to-json": "^1.0.5" | ||
"xml2js": "^0.4.23" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^6.5.1", | ||
"mocha": "^6.2.1" | ||
"eslint": "^7.17.0", | ||
"mocha": "^8.2.1" | ||
}, | ||
@@ -24,0 +24,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8255
9
129
1
+ Addedxml2js@^0.4.23
+ Addedsax@1.4.1(transitive)
+ Addedxml2js@0.4.23(transitive)
+ Addedxmlbuilder@11.0.1(transitive)
- Removedrss-to-json@^1.0.5
- Removedaxios@0.21.4(transitive)
- Removedbindings@1.5.0(transitive)
- Removedfile-uri-to-path@1.0.0(transitive)
- Removedfollow-redirects@1.15.9(transitive)
- Removedhoek@4.3.15.0.46.1.3(transitive)
- Removedisemail@3.2.0(transitive)
- Removedjoi@13.7.0(transitive)
- Removednan@2.22.0(transitive)
- Removednode-expat@2.4.1(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedrss-to-json@1.1.3(transitive)
- Removedtopo@3.0.3(transitive)
- Removedxml2json@0.12.0(transitive)