-
Notifications
You must be signed in to change notification settings - Fork 6
/
params.go
156 lines (138 loc) · 3.72 KB
/
params.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"flag"
"fmt"
"os"
"strconv"
"strings"
"github.com/dustin/go-humanize"
"github.com/wader/goutubedl"
"golang.org/x/exp/slices"
)
type paramsType struct {
ApiID int
ApiHash string
BotToken string
AllowedUserIDs []int64
AdminUserIDs []int64
AllowedGroupIDs []int64
MaxSize int64
}
var params paramsType
func (p *paramsType) Init() error {
// Further available environment variables:
// SESSION_FILE: path to session file
// SESSION_DIR: path to session directory, if SESSION_FILE is not set
var apiID string
flag.StringVar(&apiID, "api-id", "", "telegram api_id")
flag.StringVar(&p.ApiHash, "api-hash", "", "telegram api_hash")
flag.StringVar(&p.BotToken, "bot-token", "", "telegram bot token")
flag.StringVar(&goutubedl.Path, "yt-dlp-path", "", "yt-dlp path")
var allowedUserIDs string
flag.StringVar(&allowedUserIDs, "allowed-user-ids", "", "allowed telegram user ids")
var adminUserIDs string
flag.StringVar(&adminUserIDs, "admin-user-ids", "", "admin telegram user ids")
var allowedGroupIDs string
flag.StringVar(&allowedGroupIDs, "allowed-group-ids", "", "allowed telegram group ids")
var maxSize string
flag.StringVar(&maxSize, "max-size", "", "allowed max size of video files")
flag.Parse()
var err error
if apiID == "" {
apiID = os.Getenv("API_ID")
}
if apiID == "" {
return fmt.Errorf("api id not set")
}
p.ApiID, err = strconv.Atoi(apiID)
if err != nil {
return fmt.Errorf("invalid api_id")
}
if p.ApiHash == "" {
p.ApiHash = os.Getenv("API_HASH")
}
if p.ApiHash == "" {
return fmt.Errorf("api hash not set")
}
if p.BotToken == "" {
p.BotToken = os.Getenv("BOT_TOKEN")
}
if p.BotToken == "" {
return fmt.Errorf("bot token not set")
}
if goutubedl.Path == "" {
goutubedl.Path = os.Getenv("YTDLP_PATH")
}
if goutubedl.Path == "" {
goutubedl.Path = "yt-dlp"
}
if allowedUserIDs == "" {
allowedUserIDs = os.Getenv("ALLOWED_USERIDS")
}
sa := strings.Split(allowedUserIDs, ",")
for _, idStr := range sa {
if idStr == "" {
continue
}
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
return fmt.Errorf("allowed user ids contains invalid user ID: " + idStr)
}
p.AllowedUserIDs = append(p.AllowedUserIDs, id)
}
if adminUserIDs == "" {
adminUserIDs = os.Getenv("ADMIN_USERIDS")
}
sa = strings.Split(adminUserIDs, ",")
for _, idStr := range sa {
if idStr == "" {
continue
}
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
return fmt.Errorf("admin ids contains invalid user ID: " + idStr)
}
p.AdminUserIDs = append(p.AdminUserIDs, id)
if !slices.Contains(p.AllowedUserIDs, id) {
p.AllowedUserIDs = append(p.AllowedUserIDs, id)
}
}
if allowedGroupIDs == "" {
allowedGroupIDs = os.Getenv("ALLOWED_GROUPIDS")
}
sa = strings.Split(allowedGroupIDs, ",")
for _, idStr := range sa {
if idStr == "" {
continue
}
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
return fmt.Errorf("allowed group ids contains invalid group ID: " + idStr)
}
p.AllowedGroupIDs = append(p.AllowedGroupIDs, id)
}
if maxSize == "" {
maxSize = os.Getenv("MAX_SIZE")
}
if maxSize != "" {
b, err := humanize.ParseBigBytes(maxSize)
if err != nil {
return fmt.Errorf("invalid max size: %w", err)
}
p.MaxSize = b.Int64()
}
// Writing env. var YTDLP_COOKIES contents to a file.
// In case a docker container is used, the yt-dlp.conf points yt-dlp to this cookie file.
if cookies := os.Getenv("YTDLP_COOKIES"); cookies != "" {
f, err := os.Create("/tmp/ytdlp-cookies.txt")
if err != nil {
return fmt.Errorf("couldn't create cookies file: %w", err)
}
_, err = f.WriteString(cookies)
if err != nil {
return fmt.Errorf("couldn't write cookies file: %w", err)
}
f.Close()
}
return nil
}