парсер новый

This commit is contained in:
Artem Tsyrulnikov
2026-02-04 14:54:25 +03:00
parent 6fc6a60bce
commit d15f2d2bad
6 changed files with 187 additions and 10 deletions

View File

@@ -10,6 +10,59 @@ import (
"github.com/jackc/pgx/v5/pgtype"
)
func (d *Database) UpdateChannelIfNotAccessible(ctx context.Context, channel domain.Channel) error {
query := `UPDATE channel
SET telegram_id = $2,
username = $3,
title = $4,
access_hash = $5,
pts = 0,
is_accessible = TRUE,
invite_link = $6,
updated_at = CURRENT_TIMESTAMP
WHERE telegram_id = $1
AND is_accessible = FALSE;`
txOrPool := transaction.TryExtractTX(ctx)
username := strings.TrimSpace(channel.Username)
var usernameDTO pgtype.Text
if username != "" {
usernameDTO = pgtype.Text{String: username, Valid: true}
} else {
usernameDTO = pgtype.Text{Valid: false}
}
inviteLink := strings.TrimSpace(channel.InviteLink)
var inviteLinkDTO pgtype.Text
if inviteLink != "" {
inviteLinkDTO = pgtype.Text{String: inviteLink, Valid: true}
} else {
inviteLinkDTO = pgtype.Text{Valid: false}
}
result, err := txOrPool.Exec(ctx, query,
channel.TelegramID,
pgtype.Int8{Int64: channel.TelegramID, Valid: true},
usernameDTO,
pgtype.Text{String: channel.Title, Valid: true},
pgtype.Int8{Int64: channel.AccessHash, Valid: true},
inviteLinkDTO,
)
if err != nil {
return fmt.Errorf("txOrPool.Exec: %w", err)
}
// Log if no rows were updated (channel either doesn't exist or is already accessible)
rowsAffected := result.RowsAffected()
if rowsAffected == 0 {
// Channel doesn't exist or is already accessible - this is fine
return nil
}
return nil
}
func (d *Database) UpdateChannel(ctx context.Context, channel domain.Channel) error {
query := `UPDATE channel
SET telegram_id = $2,

View File

@@ -13,7 +13,7 @@ func (uc *UseCase) FetchChannelMeta(ctx context.Context, username string) (domai
return domain.Channel{}, fmt.Errorf("uc.telegram.ParseChannelMeta: %s", err)
}
err = uc.database.UpdateChannelIfNotAccessible(ctx, channel.TelegramID)
err = uc.database.UpdateChannelIfNotAccessible(ctx, channel)
if err != nil {
return domain.Channel{}, fmt.Errorf("uc.database.UpdateChannelIfNotAccessible: %s", err)
}

View File

@@ -13,7 +13,7 @@ func (uc *UseCase) FetchChannelMetaByInvite(ctx context.Context, inviteLink stri
return domain.Channel{}, fmt.Errorf("uc.telegram.ParseChannelMetaByInvite: %s", err)
}
err = uc.database.UpdateChannelIfNotAccessible(ctx, channel.TelegramID)
err = uc.database.UpdateChannelIfNotAccessible(ctx, channel)
if err != nil {
return domain.Channel{}, fmt.Errorf("uc.database.UpdateChannelIfNotAccessible: %s", err)
}

View File

@@ -14,6 +14,7 @@ import (
func (uc *UseCase) FetchChannels(ctx context.Context) error {
channels := uc.database.GetChannels(ctx)
log.Debug().Msg("start fetch channels")
for _, c := range channels {
if c.Pts == 0 {
@@ -115,6 +116,7 @@ func (uc *UseCase) rejoinChannel(ctx context.Context, channel domain.Channel) {
}
func (uc *UseCase) initializeChannel(ctx context.Context, channel domain.Channel) {
log.Debug().Stringer("ch", channel).Msg("init channel")
var (
updated domain.Channel
err error