alis-request
Advanced tools
Comparing version 1.0.1 to 1.0.2
122
index.js
@@ -1,85 +0,75 @@ | ||
const cheerio = require('cheerio'); | ||
const request = require('request').defaults({ jar: true }); | ||
import cheerio from 'cheerio'; | ||
import request from 'request'; | ||
import Stream from 'stream'; | ||
function getPageUrl(body) { | ||
const $ = cheerio.load(body); | ||
const pageLink = $('a[id^=\'Agt\']'); | ||
const pageUrl = `${$(pageLink).attr('href')}`; | ||
return pageUrl; | ||
export const ReadableStreamBooks = new Stream.Readable(); | ||
ReadableStreamBooks._read = () => {}; | ||
export function sendInitialQuery(query, callback) { | ||
if (!query.year) { | ||
return process.nextTick(callback, new Error('query.year is not provided')); | ||
} | ||
const j = request.jar(); | ||
const INITIAL_URL = `http://${query.ip}/alis/EK/do_searh.php?radiodate=simple&valueINP=${query.year}&tema=1&tag=6`; | ||
request({ url: INITIAL_URL, jar: j }, (err, response, body) => { | ||
if (err) { | ||
callback(err); | ||
return; | ||
} | ||
callback(null, { page: body, jar: j }); | ||
}); | ||
} | ||
function run(urlsQueue, callback) { | ||
const books = []; | ||
function fn(q) { | ||
if (q[0] === 'undefined') { | ||
callback(null, books); | ||
export function getPage(url, jug, callback) { | ||
request({ url, jar: jug }, (err, response, body) => { | ||
if (err) { | ||
callback(err); | ||
return; | ||
} | ||
request(q[0], (err, res, body) => { | ||
if (err) { | ||
callback(err); | ||
} | ||
const $ = cheerio.load(body); | ||
const nextPageUrl = getPageUrl(body); | ||
const remainingQueue = q.slice(1); | ||
console.log(books.length); | ||
callback(null, body); | ||
}); | ||
} | ||
$('.article').each(() => { | ||
books.push($(this).text()); | ||
}); | ||
export function getNextPageUrl($) { | ||
const pageLink = $('#Agt'); | ||
const pageUrl = (`${$(pageLink).attr('href')}`); | ||
return pageUrl; | ||
} | ||
if (nextPageUrl === 'undefined') { | ||
callback(null, books); | ||
return; | ||
} | ||
if (q.length === 1) { | ||
remainingQueue.push(`http://86.57.174.45/alis/EK/${nextPageUrl}`); | ||
} | ||
fn(remainingQueue); | ||
}); | ||
} | ||
fn(urlsQueue); | ||
export function getBooks($) { | ||
$('.article').each(function () { | ||
ReadableStreamBooks.push($(this).text()); | ||
}); | ||
} | ||
function getFirstTenPageUrls(html) { | ||
const $ = cheerio.load(html); | ||
export function getNumberedPageUrls(page, ip) { | ||
const $ = cheerio.load(page); | ||
const firstTenPageLinks = $('a[href^=\'do_other\']'); | ||
const firstTenPageUrls = $(firstTenPageLinks).map((i, link) => `http://86.57.174.45/alis/EK/${$(link).attr('href')}`).toArray(); | ||
const firstTenPageUrls = $(firstTenPageLinks).map((i, link) => `http://${ip}/alis/EK/${$(link).attr('href')}`).toArray(); | ||
return firstTenPageUrls; | ||
} | ||
function sendInitialQuery(query, callback) { | ||
const url = `http://86.57.174.45/alis/EK/do_searh.php?radiodate=simple&valueINP=${query.year}&tema=1&tag=6`; | ||
request(url, (err, response, html) => { | ||
export function run(fn, q, ip, jar) { | ||
if (q.length === 0) { | ||
ReadableStreamBooks.push(null); | ||
return; | ||
} | ||
fn(q[0], jar, (err, page) => { | ||
if (err) { | ||
console.log(err); | ||
callback(err); | ||
return; | ||
} | ||
callback(null, html); | ||
}); | ||
} | ||
function getAllBooksByYear(query, callback) { | ||
sendInitialQuery(query, (err, html) => { | ||
if (err) { | ||
callback(err); | ||
const $ = cheerio.load(page); | ||
getBooks($); | ||
const nextPageUrl = getNextPageUrl($); | ||
if (nextPageUrl === 'undefined') { | ||
ReadableStreamBooks.push(null); | ||
return; | ||
} | ||
const firstTenPageUrls = getFirstTenPageUrls(html); | ||
run(firstTenPageUrls, (error, books) => { | ||
if (error) { | ||
callback(error); | ||
} | ||
callback(null, books); | ||
}); | ||
const remainingQueue = q.slice(1); | ||
if (q.length === 1) { | ||
remainingQueue.push(`http://${ip}/alis/EK/${nextPageUrl}`); | ||
} | ||
run(fn, remainingQueue, ip, jar); | ||
}); | ||
} | ||
module.exports = { | ||
getAllBooksByYear, | ||
sendInitialQuery, | ||
getPageUrl, | ||
getFirstTenPageUrls, | ||
run, | ||
}; |
{ | ||
"name": "alis-request", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Get books by year in alis library", | ||
@@ -10,13 +10,19 @@ "main": "index.js", | ||
"scripts": { | ||
"alis": "nodemon --exec babel-node -- ./index.js", | ||
"callback": "nodemon --exec babel-node -- ./callback.js" | ||
"start": "nodemon --exec babel-node -- ./consumer.js", | ||
"test": "mocha --compilers js:babel-core/register", | ||
"lint": "eslint index.js" | ||
}, | ||
"dependencies": { | ||
"cheerio": "^0.22.0", | ||
"request": "^2.81.0" | ||
"cheerio": "0.22.0", | ||
"request": "2.81.0" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.24.0", | ||
"babel-preset-es2015": "^6.24.0" | ||
"babel-cli": "6.24.0", | ||
"babel-preset-es2015": "6.24.0", | ||
"chai": "3.5.0", | ||
"eslint": "3.19.0", | ||
"mocha": "3.2.0", | ||
"nock": "^9.0.11", | ||
"nodemon": "1.11.0" | ||
} | ||
} |
@@ -16,22 +16,45 @@ # alis-request | ||
### API | ||
## API | ||
* getAllBooksByYear - get all book by year | ||
* ReadableStreamBooks - read all book by year in stream | ||
* sendInitialQuery - make initial http query | ||
* getPageUrl - get page url | ||
* getFirstTenPageUrls - get ten page urls | ||
* getPage - get page body | ||
* getNumberedPageUrls - get ten page urls | ||
* run - recursive runner | ||
### Examples | ||
## Examples | ||
You can do what you want with books, for example save in database. | ||
Next example return all books 2016 year. | ||
``` | ||
const AlisRequest = require('alis-request'); | ||
Next example return all books 2015 year in stream. | ||
```js | ||
import Stream from 'stream'; | ||
import { sendInitialQuery, getPage, getNumberedPageUrls, run, ReadableStreamBooks } from './index'; | ||
AlisRequest.getAllBooksByYear({ year: 2016 }, (err, books) => { | ||
console.log(books.length); | ||
const WritableStreamBooks = new Stream.Writable(); | ||
ReadableStreamBooks.pipe(WritableStreamBooks); | ||
const books = []; | ||
const initParams = { | ||
year: 2015, | ||
ip: '86.57.174.45', | ||
}; | ||
sendInitialQuery(initParams, (err, res) => { | ||
if (err) { | ||
console.log(err); | ||
return; | ||
} | ||
const firstTenPageUrls = getNumberedPageUrls(res.page, initParams.ip); | ||
run(getPage, firstTenPageUrls, initParams.ip, res.jar); | ||
}); | ||
WritableStreamBooks._write = (book, encoding, done) => { | ||
books.push(book); | ||
console.log(`STREAM: ${books.length}`); | ||
// ready to process the next chunk | ||
done(); | ||
}; | ||
``` | ||
@@ -38,0 +61,0 @@ |
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
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
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
101856
72
7
68
+ Addedajv@4.11.8(transitive)
+ Addedassert-plus@0.2.0(transitive)
+ Addedaws-sign2@0.6.0(transitive)
+ Addedboom@2.10.1(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addedco@4.6.0(transitive)
+ Addedcryptiles@2.0.5(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedform-data@2.1.4(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhar-schema@1.0.5(transitive)
+ Addedhar-validator@4.2.1(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhawk@3.1.3(transitive)
+ Addedhoek@2.16.3(transitive)
+ Addedhttp-signature@1.1.1(transitive)
+ Addedisarray@2.0.5(transitive)
+ Addedjson-stable-stringify@1.1.1(transitive)
+ Addedjsonify@0.0.1(transitive)
+ Addedoauth-sign@0.8.2(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedperformance-now@0.2.0(transitive)
+ Addedpunycode@1.4.1(transitive)
+ Addedqs@6.4.1(transitive)
+ Addedrequest@2.81.0(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedsntp@1.0.9(transitive)
+ Addedstringstream@0.0.6(transitive)
+ Addedtough-cookie@2.3.4(transitive)
- Removedajv@6.12.6(transitive)
- Removedaws-sign2@0.7.0(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedform-data@2.3.3(transitive)
- Removedhar-schema@2.0.0(transitive)
- Removedhar-validator@5.1.5(transitive)
- Removedhttp-signature@1.2.0(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedoauth-sign@0.9.0(transitive)
- Removedperformance-now@2.1.0(transitive)
- Removedpsl@1.10.0(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedqs@6.5.3(transitive)
- Removedrequest@2.88.2(transitive)
- Removedtough-cookie@2.5.0(transitive)
- Removeduri-js@4.4.1(transitive)
Updatedcheerio@0.22.0
Updatedrequest@2.81.0