Skip to content

Commit

Permalink
it can accept more formats
Browse files Browse the repository at this point in the history
  • Loading branch information
torokmark committed Jun 15, 2013
1 parent a3046e9 commit 263b23c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 32 deletions.
12 changes: 6 additions & 6 deletions lib/parser/wikiParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ function parseJson(wiki, callback) {


module.exports.parse = function (wiki, format, callback) {
if (format === "json") {
//if (format === "json") {
parseJson(wiki, callback);
} else {
process.nextTick(function () {
callback(new Error("Unrecognized format [format=" + format + "]"));
});
}
//} else {
// process.nextTick(function () {
// callback(new Error("Unrecognized format [format=" + format + "]"));
// });
//}
};
65 changes: 39 additions & 26 deletions lib/wikiClient.js
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;

0 comments on commit 263b23c

Please sign in to comment.