passing result to DB service

This commit is contained in:
Kfir Dayan 2023-04-18 13:29:39 +03:00
parent 661d5e9880
commit a5799e4b48
4 changed files with 18 additions and 17 deletions

View file

@ -1,11 +1,16 @@
import { Body, Controller, Post } from '@nestjs/common'; import { Body, Controller, Post } from '@nestjs/common';
import { CrawlerService } from '../crawler/crawler.service'; import { CrawlerService } from '../crawler/crawler.service';
import { DbService } from '../db/db.service';
@Controller('/') @Controller('/')
export class ApiController { export class ApiController {
constructor(private crawlerService: CrawlerService) {} constructor(private crawlerService: CrawlerService, private DbService: DbService) {}
@Post('crawl') @Post('crawl')
async crawl(@Body() body: { url: string }) { async crawl(@Body() body: { url: string }) {
return this.crawlerService.crawl(body.url); const results = this.crawlerService.crawl(body.url);
results.then((data) => {
console.log(data)
this.DbService.insert(data, 'crawler');
});
} }
} }

View file

@ -1,10 +1,11 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { ApiController } from './api.controller'; import { ApiController } from './api.controller';
import { CrawlerService } from '../crawler/crawler.service'; import { CrawlerService } from '../crawler/crawler.service';
import { DbService } from '../db/db.service';
@Module({ @Module({
controllers: [ApiController], controllers: [ApiController],
providers: [CrawlerService] providers: [CrawlerService, DbService]
}) })
export class ApiModule {} export class ApiModule {}

View file

@ -42,18 +42,10 @@ export class CrawlerService {
// URLS // // URLS //
const urls = await page.$$eval('a', links => links.map(link => link.href)); const urls = await page.$$eval('a', links => links.map(link => link.href));
await browser.close(); await browser.close();
return {
// // save to db cssSheetsLocation,
// this.dbService.insert({ scriptsSheetsLocation
// url, }
// domain,
// cssSheetsLocation,
// scriptsSheetsLocation,
// screenshotBuffer,
// urls
// }, 'crawler');
} }
async downloadFiles(urls: string[], path: string) { async downloadFiles(urls: string[], path: string) {

View file

@ -6,7 +6,10 @@ export class DbService {
console.log(`DbService constructor`); console.log(`DbService constructor`);
} }
insert(data, collection) { insert(data: {
console.log(`DbService insert`); cssSheetsLocation: string[];
scriptsSheetsLocation: string[];
}, collection: string) {
console.log({data, collection});
} }
} }