Files
tg_exporter/internal/cmd_export.go
2024-09-02 18:01:38 +08:00

66 lines
1.3 KiB
Go

package internal
import (
"context"
"github.com/gotd/td/tg"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
channelID int64
modeHistory bool
channelAlias string
)
func ExportCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "export",
Short: "export channels",
RunE: wrapE(exportCmd),
}
cmd.Flags().Int64Var(&channelID, "channel", 0, "channel id")
cmd.Flags().BoolVar(&modeHistory, "history", false, "history mode")
cmd.Flags().StringVar(&channelAlias, "alias", "", "channel alias")
return cmd
}
func exportCmd(ctx context.Context) error {
if channelID == 0 && channelAlias == "" {
return errors.New("channel id or alias is required")
}
var channel *tg.Channel
var err error
if channelAlias != "" {
channel, err = client.ChannelInfoByAlias(ctx, channelAlias)
if err != nil {
return err
}
} else {
channel, err = client.ChannelInfoByID(ctx, channelID)
if err != nil {
return err
}
}
if channel.GetID() == 0 {
return errors.New("channel not found")
}
cfg := NewDBChannel(channel.GetID(), channel.Username, channel.Title)
if err := cfg.Get(ctx); err != nil {
return err
}
// https://t.me/yunpanshare/37426
for {
if err := client.Channel(ctx, channel, cfg, modeHistory); err != nil {
return err
}
}
}