Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make declarative like with scrappers #9

Open
github-actions bot opened this issue Sep 8, 2023 · 0 comments
Open

make declarative like with scrappers #9

github-actions bot opened this issue Sep 8, 2023 · 0 comments
Labels

Comments

@github-actions
Copy link

github-actions bot commented Sep 8, 2023

https://api.github.com/vorant94/sofash/blob/fcb3ff714b19baf963f745027eebdd11e63af32c/apps/server/src/parse/parsers/telegram.parser.ts#L56

import { type Parser } from './parser.js';
import { type TelegramRawEventJob } from 'mq';
import { type EventEntity } from 'db';
import { type Logger } from 'logger';
import {
  type message,
  type MessageContent,
  type messagePhoto,
  type messageText,
} from 'tdlib-types';
import { type Llm } from 'llm';
import { type DeepPartial } from 'typeorm';

export class TelegramParser implements Parser<TelegramRawEventJob> {
  readonly #logger: Logger;
  readonly #llm: Llm;

  constructor(logger: Logger, llm: Llm) {
    this.#logger = logger.clone(TelegramParser.name);
    this.#llm = llm;
  }

  async parseRawEventJob(
    rawEvent: TelegramRawEventJob,
  ): Promise<DeepPartial<EventEntity>> {
    this.#logger.debug(
      `parseRawEventJob started: ${JSON.stringify(rawEvent, null, 2)}`,
    );

    const message = this.#extractMessage(rawEvent.content);
    const eventData = await this.#llm.extractEventData(message);

    const event: DeepPartial<EventEntity> = {
      title: eventData.title,
      description: eventData.description,
      price: eventData.price,
      detailsUrl: 'test',
      sourceId: rawEvent.eventSource.id,
      sourceMessageId: rawEvent.content.id.toString(),
    };

    if (eventData.startingAt != null) {
      event.startingAt = new Date(eventData.startingAt);
    }

    if (eventData.endingAt != null) {
      event.endingAt = new Date(eventData.endingAt);
    }

    this.#logger.debug(
      `parseRawEventJob finished: ${JSON.stringify(event, null, 2)}`,
    );
    return event;
  }

  // TODO: make declarative like with scrappers
  #extractMessage({ content }: message): string {
    if (this.#isTextContent(content)) {
      return content.text.text;
    } else if (this.#isImageContent(content)) {
      return content.caption.text;
    } else {
      throw new Error(
        `Can't extract message from with content of type [${content._}]`,
      );
    }
  }

  #isTextContent(content: MessageContent): content is messageText {
    return content._ === 'messageText';
  }

  #isImageContent(content: MessageContent): content is messagePhoto {
    return content._ === 'messagePhoto';
  }
}
@github-actions github-actions bot added the todo label Sep 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants