@grodno-city/alis-web-request
Advanced tools
Comparing version 0.0.2 to 1.0.0
106
lib/index.js
@@ -6,3 +6,2 @@ 'use strict'; | ||
}); | ||
exports.ReadableStreamItems = undefined; | ||
exports.sendInitialQuery = sendInitialQuery; | ||
@@ -13,3 +12,2 @@ exports.getPage = getPage; | ||
exports.getItems = getItems; | ||
exports.pushItemsToStream = pushItemsToStream; | ||
exports.getNumberedPageUrls = getNumberedPageUrls; | ||
@@ -19,3 +17,8 @@ exports.parsePage = parsePage; | ||
exports.run = run; | ||
exports.getRecordsByQuery = getRecordsByQuery; | ||
var _querystring = require('querystring'); | ||
var _querystring2 = _interopRequireDefault(_querystring); | ||
var _request = require('request'); | ||
@@ -29,20 +32,35 @@ | ||
var _stream = require('stream'); | ||
var _queryMap = require('./queryMap.json'); | ||
var _stream2 = _interopRequireDefault(_stream); | ||
var _queryMap2 = _interopRequireDefault(_queryMap); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function sendInitialQuery(query, callback) { | ||
if (!query.year) { | ||
return process.nextTick(callback, new Error('query.year is not provided')); | ||
function sendInitialQuery(params, callback) { | ||
if (!params.alisEndpoint) { | ||
return process.nextTick(callback, new Error('alisEndpoint is not provided')); | ||
} | ||
if (!params.query) { | ||
return process.nextTick(callback, new Error('query is not provided')); | ||
} | ||
if (!params.queryType) { | ||
return process.nextTick(callback, new Error('queryType is not provided')); | ||
} | ||
if (!params.recordType) { | ||
return process.nextTick(callback, new Error('recordType is not provided')); | ||
} | ||
var j = _request2.default.jar(); | ||
var INITIAL_URL = query.alisEndpoint + '/alis/EK/do_searh.php?radiodate=simple&valueINP=' + query.year + '&tema=1&tag=6'; | ||
var alisEndpoint = '' + params.alisEndpoint; | ||
var qs = _querystring2.default.stringify({ | ||
radiodate: 'simple', | ||
valueINP: encodeURIComponent(params.query), | ||
tema: _queryMap2.default.recordType[params.recordType], | ||
tag: _queryMap2.default.queryType[params.queryType] | ||
}); | ||
var firstPageUrl = '/alis/EK/do_searh.php?' + qs; | ||
var INITIAL_URL = '' + alisEndpoint + firstPageUrl; | ||
(0, _request2.default)({ url: INITIAL_URL, jar: j }, function (err, response, body) { | ||
if (err) { | ||
callback(err); | ||
return; | ||
return callback(err); | ||
} | ||
@@ -83,15 +101,2 @@ callback(null, { page: body, jar: j }); | ||
var ReadableStreamItems = exports.ReadableStreamItems = new _stream2.default.Readable({ objectMode: true }); | ||
/* This is a temporary solution _read = () => {}, that will be changed */ | ||
ReadableStreamItems._read = function () {}; | ||
function pushItemsToStream(items) { | ||
return items.map(function (item) { | ||
return ReadableStreamItems.push({ | ||
id: item.id, | ||
title: item.title | ||
}); | ||
}); | ||
} | ||
function getNumberedPageUrls($) { | ||
@@ -110,3 +115,3 @@ var pageLinks = $('a[href^=\'do_other\']'); | ||
function processItems(options, callback) { | ||
function processItems(memo, q, options, callback) { | ||
if (!options) { | ||
@@ -117,11 +122,20 @@ return process.nextTick(callback, new Error('options is not provided')); | ||
getPage({ url: options.url, jar: options.jar }, function (err, body) { | ||
if (err) return callback(err); | ||
var $ = parsePage(body); | ||
pushItemsToStream(getItems($)); | ||
var items = getItems($); | ||
var nextPageUrl = getNextPageUrl($); | ||
callback(null, nextPageUrl); | ||
var remainingQueue = q.slice(1); | ||
if (q.length === 1) { | ||
remainingQueue.push('' + nextPageUrl); | ||
} | ||
items.forEach(function (item) { | ||
memo.push(item); | ||
}); | ||
callback(null, memo, remainingQueue); | ||
}); | ||
} | ||
function run(fn, q, options) { | ||
function run(fn, q, memo, options, callback) { | ||
if (!q) { | ||
@@ -132,17 +146,35 @@ return new Error('q is not provided'); | ||
if (q[0] === 'undefined') { | ||
return; | ||
return callback(null, memo); | ||
} | ||
fn({ url: options.alisEndpoint + '/alis/EK/' + q[0], jar: options.jar }, function (err, nextPageUrl) { | ||
fn(memo, q, { url: options.alisEndpoint + '/alis/EK/' + q[0], jar: options.jar }, function (err, nextMemo, nextQ) { | ||
if (err) { | ||
return err; | ||
return callback(err); | ||
} | ||
run(fn, nextQ, memo, options, callback); | ||
}); | ||
} | ||
var remainingQueue = q.slice(1); | ||
if (q.length === 1) { | ||
remainingQueue.push('' + nextPageUrl); | ||
function getRecordsByQuery(initParams, callback) { | ||
sendInitialQuery(initParams, function (err, res) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
run(fn, remainingQueue, options); | ||
var options = { | ||
alisEndpoint: initParams.alisEndpoint, | ||
jar: res.jar | ||
}; | ||
if (res.page.match('Не результативный поиск')) { | ||
return callback(new Error('no match')); | ||
} | ||
var $ = parsePage(res.page); | ||
var firstNumberedPageUrls = getNumberedPageUrls($); | ||
var remainingQueue = firstNumberedPageUrls; | ||
run(processItems, remainingQueue, [], options, function (runErr, memo) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
callback(null, memo); | ||
}); | ||
}); | ||
} |
{ | ||
"name": "@grodno-city/alis-web-request", | ||
"version": "0.0.2", | ||
"version": "1.0.0", | ||
"main": "./lib/index.js", | ||
@@ -13,3 +13,4 @@ "repository": "https://github.com/grodno-city/alis-web-request.git", | ||
"lint": "eslint index.js", | ||
"prepublish": "babel index.js --presets babel-preset-es2015 --out-dir lib" | ||
"prepublish": "babel index.js --presets babel-preset-es2015 --out-dir lib", | ||
"update-query-mapping": "babel-node bin/update-query-mapping.js > queryMap.json" | ||
}, | ||
@@ -24,3 +25,5 @@ "dependencies": { | ||
"chai": "3.5.0", | ||
"eslint": "3.19.0", | ||
"eslint": "^3.19.0", | ||
"eslint-config-airbnb-base": "^11.2.0", | ||
"eslint-plugin-import": "^2.3.0", | ||
"mocha": "3.2.0", | ||
@@ -27,0 +30,0 @@ "nock": "9.0.11", |
@@ -0,0 +0,0 @@ # alis-web-request |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
103895
8
203
0
9
1