Skip to content

Commit

Permalink
feat(dl): file name template
Browse files Browse the repository at this point in the history
  • Loading branch information
iyear committed Oct 1, 2022
1 parent c2cf9f4 commit 07d6ab4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 17 deletions.
8 changes: 6 additions & 2 deletions app/dl/dl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/spf13/viper"
)

func Run(ctx context.Context, urls, files []string) error {
func Run(ctx context.Context, template string, urls, files []string) error {
c, _, err := tgc.NoLogin()
if err != nil {
return err
Expand All @@ -25,7 +25,11 @@ func Run(ctx context.Context, urls, files []string) error {
return err
}

return downloader.New(c.API(), viper.GetInt(consts.FlagPartSize), viper.GetInt(consts.FlagThreads), newIter(c.API(), umsgs, fmsgs)).
it, err := newIter(c.API(), template, umsgs, fmsgs)
if err != nil {
return err
}
return downloader.New(c.API(), viper.GetInt(consts.FlagPartSize), viper.GetInt(consts.FlagThreads), it).
Download(ctx, viper.GetInt(consts.FlagLimit))
})
}
57 changes: 44 additions & 13 deletions app/dl/iter.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,61 @@
package dl

import (
"bytes"
"context"
"fmt"
"github.com/gotd/td/telegram/peers"
"github.com/gotd/td/telegram/query"
"github.com/gotd/td/tg"
"github.com/iyear/tdl/pkg/downloader"
"github.com/iyear/tdl/pkg/utils"
"text/template"
"time"
)

type iter struct {
client *tg.Client
dialogs []*dialog
curi int
curj int
manager *peers.Manager
client *tg.Client
dialogs []*dialog
curi int
curj int
template *template.Template
manager *peers.Manager
}

type dialog struct {
peer tg.InputPeerClass
msgs []int
}

func newIter(client *tg.Client, items ...[]*dialog) *iter {
type fileTemplate struct {
DialogID int64
MessageID int
MessageDate int64
FileName string
FileSize string
DownloadDate int64
}

func newIter(client *tg.Client, tmpl string, items ...[]*dialog) (*iter, error) {
t, err := template.New("dl").Parse(tmpl)
if err != nil {
return nil, err
}

mm := make([]*dialog, 0)

for _, m := range items {
mm = append(mm, m...)
}

return &iter{
client: client,
dialogs: mm,
curi: 0,
curj: -1,
manager: peers.Options{}.Build(client), // TODO(iyear): use local cache to speed up
}
client: client,
dialogs: mm,
curi: 0,
curj: -1,
template: t,
manager: peers.Options{}.Build(client), // TODO(iyear): use local cache to speed up
}, nil
}

func (i *iter) Next(ctx context.Context) bool {
Expand Down Expand Up @@ -91,7 +110,19 @@ func (i *iter) item(ctx context.Context, peer tg.InputPeerClass, msg int) (*down
id, message.ID)
}

media.Name = fmt.Sprintf("%d_%d_%s", id, message.ID, media.Name)
buf := bytes.Buffer{}
err := i.template.Execute(&buf, &fileTemplate{
DialogID: id,
MessageID: message.ID,
MessageDate: int64(message.Date),
FileName: media.Name,
FileSize: utils.Byte.FormatBinaryBytes(media.Size),
DownloadDate: time.Now().Unix(),
})
if err != nil {
return nil, err
}
media.Name = buf.String()

return media, nil
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/dl/dl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ import (

var (
urls, files []string
template string
)

var Cmd = &cobra.Command{
Use: "dl",
Aliases: []string{"download"},
Short: "Download anything from Telegram (protected) chat",
RunE: func(cmd *cobra.Command, args []string) error {
return dl.Run(cmd.Context(), urls, files)
return dl.Run(cmd.Context(), template, urls, files)
},
}

func init() {
Cmd.Flags().StringSliceVarP(&urls, consts.FlagDlUrl, "u", []string{}, "telegram message links")
Cmd.Flags().StringSliceVarP(&files, consts.FlagDlFile, "f", []string{}, "official client export files")
Cmd.Flags().StringSliceVarP(&files, consts.FlagDlFile, "f", []string{}, "official client exported files")
Cmd.Flags().StringVar(&template, consts.FlagDlTemplate, "{{ .DialogID }}_{{ .MessageID }}_{{ .FileName }}", "download file name template")
}
1 change: 1 addition & 0 deletions pkg/consts/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ const (
FlagLoginDesktop = "desktop"
FlagDlUrl = "url"
FlagDlFile = "file"
FlagDlTemplate = "template"
)

0 comments on commit 07d6ab4

Please sign in to comment.