Commit e997d9de authored by Diego Iván's avatar Diego Iván
Browse files

agregando google text-to-speech

parent c72c43bb
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
import * as dotenv from 'dotenv';

dotenv.config();

export class GoogleConstants {
  static GoogleTextToSpeechKey: string = process.env.GOOGLE_TEXT_TO_SPEECH_API_KEY;
}
+7 −0
Original line number Diff line number Diff line
@@ -50,8 +50,11 @@ export class DatabaseSeederService implements OnModuleInit {
      status: UserStatus.ACTIVE,
    };
    let tokenSuper = '';
    let tokenAdmin = '';
    try {
      tokenSuper = await this.authAdminService.signUp({ ...createSuperAdmin });
      tokenAdmin = await this.authAdminService.signUp({ ...createAdmin });
      console.log({ tokenAdmin });
    } catch (error) {
      tokenSuper = (
        await this.authAdminService.signIn({
@@ -65,6 +68,10 @@ export class DatabaseSeederService implements OnModuleInit {
      `Super Admin created with email: ${createSuperAdmin.email}
      , password: ${createSuperAdmin.password}, and token: ${tokenSuper}`,
    );

    console.log(
      `Admin created with email: ${createAdmin.email}, password: ${createAdmin.password} and token: ${tokenAdmin}`,
    );
  }

  async insertTowns() {
+2 −2
Original line number Diff line number Diff line
@@ -9,11 +9,11 @@ import {
  Query,
  StreamableFile,
  UseGuards,
  Res
  Res,
} from '@nestjs/common';
import { PointOfInterestService } from './PointOfInterest.service';
import { CreatePointAndTradDto } from './dto/create-pointAndTraduction.dto';
import { ApiBearerAuth, ApiBody, ApiConsumes, ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger';
import { ApiBearerAuth, ApiConsumes, ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger';
import { Roles } from 'src/auth/role.decorator';
import { ADMIN_ROLES } from 'src/shared/enum/admin-role.enum';
import { fileInterceptor } from 'src/shared/interceptors/file-save.interceptor';
+48 −3
Original line number Diff line number Diff line
@@ -16,6 +16,10 @@ import { generateQRCode } from './utils/qrcode';
import { Place } from 'src/place/entities/place.entity';
import puppeteer from 'puppeteer';
import { printPointInfo } from './dto/printPointInfo.dto';
import * as fs from 'fs';
import axios from 'axios';
import * as uuid from 'uuid';
import { GoogleConstants } from 'src/constants/google.constants';

@Injectable()
export class PointOfInterestService {
@@ -43,11 +47,20 @@ export class PointOfInterestService {
    };
    const insertedId = (await this.pointRepository.insert(createPointDto)).raw.insertId;

    const spanishAudio = await this.convertTextToSpeech(
      `${createPointAndTradDto.contentES}\n${createPointAndTradDto.directionsES}`,
      LANGUAGES.ES,
    );
    const englishAudio = await this.convertTextToSpeech(
      `${createPointAndTradDto.contentEN}\n${createPointAndTradDto.directionsEN}`,
      LANGUAGES.EN,
    );

    const createTradEn: CreatePointTradDto = {
      idPoint: insertedId,
      language: LANGUAGES.EN,
      content: createPointAndTradDto.contentEN,
      audioName: 'default.mp3',
      audioName: englishAudio,
      directions: createPointAndTradDto.directionsEN,
    };

@@ -55,7 +68,7 @@ export class PointOfInterestService {
      idPoint: insertedId,
      language: LANGUAGES.ES,
      content: createPointAndTradDto.contentES,
      audioName: 'default.mp3',
      audioName: spanishAudio,
      directions: createPointAndTradDto.directionsES,
    };
    await this.pointTraductionRepository.insert(createTradEs);
@@ -112,7 +125,39 @@ export class PointOfInterestService {
    return new StreamableFile(fileStream);
  }

  async createAudio() {}
  private async convertTextToSpeech(text: string, lang: LANGUAGES): Promise<string> {
    let languageCode: string = '';
    if (lang == LANGUAGES.EN) {
      languageCode = 'en-gb';
    } else {
      languageCode = 'es-es';
    }
    console.log(GoogleConstants.GoogleTextToSpeechKey);
    const url = `https://texttospeech.googleapis.com/v1/text:synthesize?key=${GoogleConstants.GoogleTextToSpeechKey}`;

    // Configuración de la solicitud
    const requestBody = {
      input: { text: text },
      voice: {
        languageCode,
        ssmlGender: 'NEUTRAL',
      },
      audioConfig: {
        audioEncoding: 'MP3',
      },
    };

    try {
      const response = await axios.post(url, requestBody);
      const buffer = Buffer.from(response.data.audioContent, 'base64');
      const id = uuid.v4();
      const filename = `${id}.mp3`;
      fs.writeFileSync(`static/audios/` + filename, buffer);
      return filename;
    } catch (error) {
      console.error('Error during API request:', error.response ? error.response.data : error.message);
    }
  }

  async findOne(idPoint: number, lang: LANGUAGES): Promise<getPointDto> {
    const pointTrad = await this.dataSource