Compare commits

..

No commits in common. "master" and "v1.1.0" have entirely different histories.

17 changed files with 332 additions and 4141 deletions

View file

@ -1,18 +0,0 @@
steps:
dependencies:
image: node:16
pull: true
commands:
- npm i
osv-detector:
image: gitnet.fr/deblan/osv-detector:v0.10
commands:
- osv-detector package-lock.json
failure: ignore
tests:
image: node:16
pull: true
commands:
- npm run test

View file

@ -1,8 +1,6 @@
Scraper
=======
[![Build Status](https://ci.gitnet.fr/api/badges/deblan/scraper/status.svg)](https://ci.gitnet.fr/deblan/scraper)
This project is a basic tool to scrap a data from a website
using a CSS selector.
@ -33,33 +31,30 @@ With code
const scraper = require('deblan-scraper')
const options = {
url: 'https://github.com/foo/bar',
acceptAllStatus: false, // Optional, default is `false`
method: 'GET', // Optional, default is `GET`
url: 'https://github.com/foo/bar',
acceptAllStatus: false, // Optional
method: 'GET', // Optional
}
const isMultiple = false // get the first result, `true` to get an array of results
const selector = '.repository-content .numbers-summary li:nth-child(4) a'
const filters = {
tags: null, // Removes tags. You can specify the tags to remove (separated by comma)
breaks: null, // Removes breaks (\n, \r)
spaces: null, // Replaces 2 successive spaces by 1, except breaks
trim: null, // Strips whitespaces from the beginning and end of the value
tags: null,
breaks: null,
spaces: null,
trim: null,
}
scraper(
options,
selector,
filters,
function(value) {
console.log(value)
},
function(error) {
console.log(error)
},
isMultiple
options,
selector,
filters,
function(value) {
console.log(value)
},
function(error) {
console.log(error)
}
)
```

2921
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,22 +1,20 @@
{
"name": "deblan-scraper",
"description": "Web scraper using CSS selector",
"version": "1.3.0",
"version": "1.1.0",
"main": "src/index.js",
"devDependencies": {
"mocha": "^10.2.0"
},
"devDependencies": {},
"dependencies": {
"cheerio": "^1.0.0-rc.3",
"extends-classes": "^1.0.5",
"minimist": "^1.2.5",
"request": "^2.88.2",
"request-promise": "^4.2.6",
"request-promise": "^4.2.5",
"striptags": "^3.1.1",
"trim": "^1.0.1"
"trim": "^0.0.1"
},
"scripts": {
"test": "mocha"
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",

View file

@ -34,8 +34,7 @@ Optional parameters
--method [METHOD] HTTP Method
--accept-http-error Accepts all status codes (like 404)
--verbose, -v Show message of error
--multiple, -m The output must contain all the selector targets
--verbose, -v Show message of error
`
if (input.has('help')) {
@ -63,7 +62,6 @@ const selector = input.get('selector')
const method = input.has('method') ? input.get('method') : 'GET'
const acceptAllStatus = input.has('accept-http-error')
const verbose = input.has('verbose') || input.has('v')
const isMultiple = input.has('multiple') || input.has('m')
let filtersToApply = {}
@ -88,13 +86,7 @@ const options = {
}
const onSuccess = function(value) {
if (isMultiple && value instanceof Array) {
for (let item of value) {
output.write(item)
}
} else {
output.write(value)
}
output.write(value)
}
const onError = function(error) {
@ -107,4 +99,4 @@ const onError = function(error) {
process.exit(1)
}
scraper(options, selector, filtersToApply, onSuccess, onError, isMultiple)
scraper(options, selector, filtersToApply, onSuccess, onError)

View file

@ -3,56 +3,56 @@
var Minimist = require('minimist');
class Input {
/**
* Constructor.
*
* @param object process
*/
constructor(process) {
this.args = Minimist(process.argv.slice(2))
this.node = process.argv[0]
this.script = process.argv[1]
}
/**
* Return the value of the given name argument.
*
* @param string name
* @param mixed default
*
* @return mixed
*/
get(name, defaultValue) {
if (this.has(name)) {
return this.args[name]
/**
* Constructor.
*
* @param object process
*/
constructor(process) {
this.args = Minimist(process.argv.slice(2))
this.node = process.argv[0]
this.script = process.argv[1]
}
if (defaultValue !== undefined) {
return defaultValue
/**
* Return the value of the given name argument.
*
* @param string name
* @param mixed default
*
* @return mixed
*/
get(name, defaultValue) {
if (this.has(name)) {
return this.args[name]
}
if (defaultValue !== undefined) {
return defaultValue
}
return null;
}
return null;
}
/**
* Check the given argument name exists.
*
* @param string name
*
* @return boolean
*/
has(name) {
return this.args.hasOwnProperty(name)
}
/**
* Check the given argument name exists.
*
* @param string name
*
* @return boolean
*/
has(name) {
return this.args.hasOwnProperty(name)
}
/**
* Return if args is empty.
*
* @return boolean
*/
empty() {
return Object.keys(this.args).length === 1
}
/**
* Return if args is empty.
*
* @return boolean
*/
empty() {
return Object.keys(this.args).length === 1
}
}
module.exports = Input

View file

@ -1,37 +1,37 @@
"use strict";
class Output {
/**
* Convert and print data to json.
*
* @param mixed data
*/
json(data, pretty) {
data = JSON.stringify(
data,
function(key, value) {
if (value === undefined) {
return null
}
/**
* Convert and print data to json.
*
* @param mixed data
*/
json(data, pretty) {
data = JSON.stringify(
data,
function(key, value) {
if (value === undefined) {
return null
}
return value
},
pretty ? 2 : null
);
return value
},
pretty ? 2 : null
);
return this.write(data)
}
return this.write(data)
}
/**
* Print data.
*
* @param mixed data
*/
write(data, level) {
level = level || 'log'
/**
* Print data.
*
* @param mixed data
*/
write(data, level) {
level = level || 'log'
console[level](data)
}
console[level](data)
}
}
module.exports = Output

View file

@ -1,5 +1,5 @@
const filter = function(value) {
return value.replace(/(\n|\r)/g, '')
return value.replace(/(\n|\r)/g, '')
}
module.exports = filter

View file

@ -1,5 +1,5 @@
const filter = function(value) {
return value.replace(/\s{2,}/g, ' ')
return value.replace(/\s{2,}/g, ' ')
}
module.exports = filter

View file

@ -1,7 +1,7 @@
const striptags = require('striptags')
const filter = function(value, tags) {
return striptags(value, tags)
return striptags(value, tags)
}
module.exports = filter

View file

@ -1,7 +1,7 @@
const trim = require('trim')
const filter = function(value) {
return trim(value)
return trim(value)
}
module.exports = filter

View file

@ -7,7 +7,7 @@ const filters = {
trim: require('./filter/trim'),
}
const scraper = function(options, selector, filtersToApply, callbackSuccess, callbackError, isMultiple) {
const scraper = function(options, selector, filtersToApply, callbackSuccess, callbackError) {
filtersToApply = filtersToApply || {}
rq({
@ -17,26 +17,10 @@ const scraper = function(options, selector, filtersToApply, callbackSuccess, cal
})
.then(function(body) {
const $ = cheerio.load(body)
let value = []
if (isMultiple) {
let nodes = $(selector)
nodes.each(function(i, node) {
value.push($(node).html())
})
} else {
value = $(selector).html()
}
let value = $(selector).html()
for (let filter in filtersToApply) {
if (isMultiple) {
for (let i in value) {
value[i] = filters[filter](value[i], filtersToApply[filter])
}
} else {
value = filters[filter](value, filtersToApply[filter])
}
value = filters[filter](value, filtersToApply[filter])
}
if (callbackSuccess) {

View file

@ -1,20 +0,0 @@
const filter = require('../src/filter/breaks')
const assert = require('assert')
describe('Check "breaks"', () => {
it('Should return empty string', () => {
assert.equal(filter("\n"), "")
})
it('Should return empty string', () => {
assert.equal(filter("\r"), "")
})
it('Should return "foo"', () => {
assert.equal(filter("foo"), "foo")
})
it('Should return "foobar"', () => {
assert.equal(filter("\r\nfoo\nbar\r\n"), "foobar")
})
});

View file

@ -1,16 +0,0 @@
const filter = require('../src/filter/spaces')
const assert = require('assert')
describe('Check "spaces"', () => {
it('Should return ""', () => {
assert.equal(filter(""), "")
})
it('Should return "foo bar"', () => {
assert.equal(filter("foo bar"), "foo bar")
})
it('Should return "foo bar"', () => {
assert.equal(filter("foo \nbar"), "foo bar")
})
});

View file

@ -1,5 +0,0 @@
const filter = require('../src/filter/tags')
const assert = require('assert')
describe('Check "tags"', () => {
});

View file

@ -1,20 +0,0 @@
const filter = require('../src/filter/trim')
const assert = require('assert')
describe('Check "trim"', () => {
it('Should return ""', () => {
assert.equal(filter(""), "")
})
it('Should return ""', () => {
assert.equal(filter(" "), "")
})
it('Should return "foo"', () => {
assert.equal(filter(" foo "), "foo")
})
it('Should return "foo bar"', () => {
assert.equal(filter(" foo bar \n"), "foo bar")
})
});

1236
yarn.lock

File diff suppressed because it is too large Load diff