Skip to content

Instantly share code, notes, and snippets.

@nicolas-oliveira
Created June 22, 2024 17:49
Show Gist options
  • Save nicolas-oliveira/52ec210fd5c49881d311cfc6cd03d9c5 to your computer and use it in GitHub Desktop.
Save nicolas-oliveira/52ec210fd5c49881d311cfc6cd03d9c5 to your computer and use it in GitHub Desktop.
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import CrawlerService from '../domain/entities/crawler/crawler.service';
import PeopleInfo from './dtos/PeopleInfo.dto';
import formatDate from './utils/formatDate';
import { TimeoutError } from 'puppeteer';
interface RequestProps {
checkin: string;
checkout: string;
}
@Injectable()
export class AppService {
constructor(private readonly crawlerService: CrawlerService) {}
async searchForOffers({checkin, checkout}: RequestProps): Promise<PeopleInfo[]> {
try {
const domain = "https://www.linkedin.com/company/itau/people/";
const URLParams = ``;
const OthersURLParams = "";
const URL = domain + URLParams + OthersURLParams;
const page = await this.crawlerService.SetupPuppeteer(URL);
await page.waitForSelector("artdeco-card org-people-profile-card__card-spacing org-people__card-margin-bottom", {
timeout: 1000,
});
const roomObject = await page.evaluate(() => {
const peopleInfo = document.querySelectorAll(
"grid grid__col--lg-8 block org-people-profile-card__profile-card-spacing"
);
peopleInfo.forEach((room) => {
const name = room.querySelector("org-people-profile-card__profile-title").textContent;
roomObject.push({ name });
});
return roomObject;
})
return roomObject;
} catch (error) {
if (error instanceof TimeoutError) {
// lidar com o erro de tempo limite
throw new HttpException("Could not find the elements on the page", HttpStatus.REQUEST_TIMEOUT);
} else {
console.log("Error: app.service.ts");
console.log(error);
throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment