Add Open Graph scraper module
This commit is contained in:
@@ -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],
|
||||
|
25
src/ogscraper/ogscraper.controller.ts
Normal file
25
src/ogscraper/ogscraper.controller.ts
Normal 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);
|
||||
}
|
||||
}
|
10
src/ogscraper/ogscraper.module.ts
Normal file
10
src/ogscraper/ogscraper.module.ts
Normal 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 {}
|
20
src/ogscraper/ogscraper.service.ts
Normal file
20
src/ogscraper/ogscraper.service.ts
Normal 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') || '',
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user