Skip to content

Commit

Permalink
feat: custom fields in command
Browse files Browse the repository at this point in the history
  • Loading branch information
popstas committed Apr 17, 2020
1 parent 3330a60 commit c0c42cd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ Options:
-h, --help display help for command
```

## Кастомные поля
Можно передать дополнительные поля так:
```
sites-scraper -d 1 -u https://example -f '{ "title": "$(`title`).text()" }'
```
Надо передавать JSON.

Одинарные кавычки в команде надо менять на `


## Как посчитать контент по csv
1. Открыть в блокноте
2. Документы посчитать поиском `,0`
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ program
.option('-p, --preset <preset>', 'Table preset (minimal, seo, headers, parse)', 'seo')
.option('-d, --max-depth <depth>', 'Max scan depth', 10)
.option('-c, --concurrenty', 'Threads number', 2)
.option('-f, --fields <json>', 'JSON with custom fields', JSON.parse)
.option('--no-skip-static', `Scan static files`)
.option('--follow-xml-sitemap', `Follow sitemap.xml`)
.option('--max-requests <num>', `Limit max pages scan`, 0)
Expand Down Expand Up @@ -47,7 +48,8 @@ async function start() {
headless: program.headless, // на десктопе открывает браузер визуально
encoding: program.encoding, // для Excel
outDir: program.outDir, // папка, куда сохраняются csv
color: program.color // раскрашивать консоль
color: program.color, // раскрашивать консоль
fields: program.fields // дополнительные поля
});
}
}
Expand Down
26 changes: 23 additions & 3 deletions src/scrap-site.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ module.exports = async (baseUrl, options = {}) => {
if (!options.fields_preset || !fields_presets[options.fields_preset]){
options.fields_preset = 'default';
}
const fields = fields_presets[options.fields_preset];
let fields = fields_presets[options.fields_preset];

if(options.fields) {
//console.log('options.fields: ', options.fields);
fields = [...Object.keys(options.fields).map(f => 'result.' + f), ...fields];
}

if (options.skip_static !== undefined) {
SKIP_IMAGES = SKIP_CSS = SKIP_JS = options.skip_static;
Expand Down Expand Up @@ -127,11 +132,14 @@ module.exports = async (baseUrl, options = {}) => {

// сюда можно дописывать сборщики данных со страницы
// поля надо добавить в fields выше
evaluatePage: () => {
evaluatePage: async () => {
try {
const customFields = await window.__customFields();
// console.log('window.__customFields(): ', JSON.stringify(customFields));

let domainParts = location.host.split('.');
const domain2level = domainParts.slice(domainParts.length-2).join('.');
return {
const result = {
request_time:
window.performance.timing.responseEnd - window.performance.timing.requestStart,
title: $('title').text(),
Expand Down Expand Up @@ -165,6 +173,13 @@ module.exports = async (baseUrl, options = {}) => {
og_title: $('meta[property="og:title"]').attr('content'),
og_image: $('meta[property="og:image"]').attr('content')
};

for(let name in customFields) {
result[name] = eval(customFields[name].replace(/`/g, "'"));
// if(name == 'section') result[name] = $('.views-field.views-field-field-section a').text();
}

return result;
} catch (e) {
return {
error: JSON.stringify(e)
Expand All @@ -183,6 +198,11 @@ module.exports = async (baseUrl, options = {}) => {
await page.setRequestInterception(true);
await page.setBypassCSP(true);

//page.on('console', msg => console.log(msg.text()));
await page.exposeFunction('__customFields', () => {
return options.fields;
});

let mixedContentUrl = '';

// это событие срабатывает, когда chrome подгружает статику на странице (и саму страницу)
Expand Down

0 comments on commit c0c42cd

Please sign in to comment.