Add names api

This commit is contained in:
2025-01-13 21:56:53 -07:00
parent 368f2b100f
commit 76f093eeda
4 changed files with 79 additions and 0 deletions

View File

@@ -30,6 +30,7 @@ import { JunkDrawerModule } from './junk-drawer/junk-drawer.module';
import { EmailModule } from './email/email.module'; import { EmailModule } from './email/email.module';
import { ContactModule } from './contact/contact.module'; import { ContactModule } from './contact/contact.module';
import { HoardingModule } from './hoarding/hoarding.module'; import { HoardingModule } from './hoarding/hoarding.module';
import { NamesModule } from './names/names.module';
@Module({ @Module({
imports: [ imports: [
@@ -106,6 +107,7 @@ import { HoardingModule } from './hoarding/hoarding.module';
EmailModule, EmailModule,
ContactModule, ContactModule,
HoardingModule, HoardingModule,
NamesModule,
], ],
controllers: [AppController], controllers: [AppController],
providers: [ providers: [

View File

@@ -0,0 +1,26 @@
import { Controller, Get, Param } from '@nestjs/common';
import { NamesService } from './names.service';
@Controller('names')
export class NamesController {
constructor(private readonly namesService: NamesService) {}
@Get()
async getNameList() {
return await this.namesService.getNameList();
}
@Get(':name')
async getNameInformation(@Param('name') name: string) {
return {
popularity: await this.namesService.getSsaNameData(name),
otherSites: {
behindTheName: `https://www.behindthename.com/name/${name.toLocaleLowerCase()}`,
babynames: `https://www.babynames.com/name/${name.toLocaleLowerCase()}`,
},
behindTheName: {
synonyms: await this.namesService.getBtnSynonyms(name),
},
};
}
}

10
src/names/names.module.ts Normal file
View File

@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { NamesService } from './names.service';
import { NamesController } from './names.controller';
import { MinioService } from 'src/minio/minio.service';
@Module({
providers: [MinioService, NamesService],
controllers: [NamesController],
})
export class NamesModule {}

View File

@@ -0,0 +1,41 @@
import { Injectable } from '@nestjs/common';
import { MinioService } from 'src/minio/minio.service';
@Injectable()
export class NamesService {
constructor(private readonly minioService: MinioService) {}
async getNameList(): Promise<string[]> {
return JSON.parse(
(
await this.minioService.getCachedBuffer(
'cdn-source',
'baby-name-data/list.json',
)
).toString(),
);
}
async getSsaNameData(name: string): Promise<any> {
return JSON.parse(
(
await this.minioService.getCachedBuffer(
'cdn-source',
`baby-name-data/individual/${name}.json`,
)
).toString(),
);
}
async getBtnSynonyms(name: string): Promise<string[]> {
const synonymData = JSON.parse(
(
await this.minioService.getCachedBuffer(
'cdn-source',
`baby-name-data/btn_synonyms.json`,
)
).toString(),
);
return synonymData[name]?.synonyms || [];
}
}