-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
45 additions
and
32 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,46 @@ | ||
var superAgent = require("superagent"), | ||
wikiParser = require("./parser/wikiParser"), | ||
wikiConstants = require("./constants/wikiConstants"); | ||
var superAgent = require("superagent"), | ||
wikiParser = require("./parser/wikiParser"), | ||
wikiConstants = require("./constants/wikiConstants"), | ||
fs = require("fs"); | ||
|
||
function searchArticle(queryAndOptions, callback) { | ||
var query = queryAndOptions.query; | ||
if (!query) { | ||
return callback(new Error("No search query was provided")); | ||
} | ||
var format = queryAndOptions.format || "json"; | ||
var summaryOnly = queryAndOptions.summaryOnly; | ||
var queryParams = {action: "query", format: format, prop: "revisions", | ||
rvprop: "content", titles: query, redirects: 1 }; | ||
if(summaryOnly){ | ||
queryParams.rvsection = 0; | ||
} | ||
var query = queryAndOptions.query; | ||
if (!query) { | ||
return callback(new Error("No search query was provided")); | ||
} | ||
|
||
superAgent.get(wikiConstants.WIKIPEDIA_EN_API_URL) | ||
.query(queryParams) | ||
.set("User-Agent", "Node.js wikipedia-js client (kenshiro@kenshiro.me)") | ||
.end(function (res) { | ||
if (res.ok) { | ||
var jsonData = JSON.parse(res.text); | ||
wikiParser.parse(jsonData, format, callback); | ||
} else { | ||
process.nextTick(function () { | ||
return callback(new Error("Unexpected HTTP status received [status=" + res.status + "]")); | ||
var format = queryAndOptions.format || "json"; | ||
var summaryOnly = queryAndOptions.summaryOnly; | ||
var queryParams = {action: "query", format: format, prop: "revisions", | ||
rvprop: "content", titles: query, redirects: 1}; | ||
|
||
if (queryAndOptions.format === "html") { | ||
queryParams.format = "json"; | ||
} | ||
|
||
if(summaryOnly){ | ||
queryParams.rvsection = 0; | ||
} | ||
|
||
superAgent.get(wikiConstants.WIKIPEDIA_EN_API_URL) | ||
.query(queryParams) | ||
.set("User-Agent", "Node.js wikipedia-js client (kenshiro@kenshiro.me)") | ||
.end(function (res) { | ||
if (res.ok) { | ||
if (format === "html") { | ||
var jsonData = JSON.parse(res.text); | ||
wikiParser.parse(jsonData, format, callback); | ||
} else if (format in {yaml: 1, php: 1, txt: 1, dbg: 1, dump: 1}) { | ||
// it does not work yet! | ||
} else { | ||
return callback(null, res.text); | ||
} | ||
} else { | ||
process.nextTick(function () { | ||
return callback(new Error("Unexpected HTTP status received [status=" + res.status + "]")); | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
module.exports.searchArticle = searchArticle; |