Add Open Graph scraper module

This commit is contained in:
2023-10-09 16:08:39 -06:00
parent 1a6a4cf1de
commit eb7d0dc1f8
6 changed files with 203 additions and 0 deletions

View File

@@ -12,6 +12,7 @@ import { CacheModule } from '@nestjs/cache-manager';
import { IrcbotModule } from './ircbot/ircbot.module';
import { IrcbotService } from './ircbot/ircbot.service';
import { DinosaurwetModule } from './dinosaurwet/dinosaurwet.module';
import { OgScraperModule } from './ogscraper/ogscraper.module';
@Module({
imports: [
@@ -27,6 +28,7 @@ import { DinosaurwetModule } from './dinosaurwet/dinosaurwet.module';
DomainrproxyModule,
IrcbotModule,
DinosaurwetModule,
OgScraperModule,
],
controllers: [AppController],
providers: [AppService],

View File

@@ -0,0 +1,25 @@
import { Body, Controller, Post } from '@nestjs/common';
import { ApiProperty, ApiTags } from '@nestjs/swagger';
const ogs = require('open-graph-scraper');
import { SuccessResult } from 'open-graph-scraper';
import { OgScraperService } from './ogscraper.service';
class ScrapeOgDto {
@ApiProperty({
description: 'URL of the page to fetch Open Graph metadata of',
example:
'https://qz.com/1903322/why-pivot-tables-are-the-spreadsheets-most-powerful-tool',
})
url: string;
}
@Controller('ogscraper')
@ApiTags('open-graph-scraper')
export class OgScraperController {
constructor(private readonly ogScraperService: OgScraperService) {}
@Post('')
async scrapeOg(@Body() body: ScrapeOgDto): Promise<SuccessResult> {
return this.ogScraperService.getOg(body.url);
}
}

View File

@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { OgScraperController } from './ogscraper.controller';
import { OgScraperService } from './ogscraper.service';
@Module({
controllers: [OgScraperController],
providers: [OgScraperService],
exports: [OgScraperService],
})
export class OgScraperModule {}

View File

@@ -0,0 +1,20 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
const ogs = require('open-graph-scraper');
import { SuccessResult } from 'open-graph-scraper';
@Injectable()
export class OgScraperService {
constructor(public readonly configService: ConfigService) {}
async getOg(url: string): Promise<SuccessResult> {
return ogs({
url,
fetchOptions: {
headers: {
'user-agent': this.configService.get<string>('userAgent') || '',
},
},
});
}
}