-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
230 lines (216 loc) · 6.78 KB
/
main.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"bufio"
"encoding/json"
"fmt"
"github.com/mehanon/tikmeh/tikwm"
"github.com/urfave/cli/v2"
"log"
"os"
"strings"
"time"
)
const (
PackageName = "Tikmeh"
VersionInfo = "0.2.3 (Jan 8, 2023)"
GithubLink = "https://github.com/mehanon/tikmeh"
DefaultWorkingDirectory = "."
)
func main() {
defer func() {
if err := recover(); err != nil {
log.Printf("%v", err)
println("\nPress any button to exit...")
_, _ = bufio.NewReader(os.Stdin).ReadByte()
}
}()
if len(os.Args) > 1 { // with console args
Cli := NewTikmehCli()
if err := Cli.Run(os.Args); err != nil {
log.Fatal(err)
}
} else { // interactive mode
fmt.Printf("%s (%s) [sources and up-to-date executables: %s]\n"+
"Enter 'help' to get help message.\n", PackageName, VersionInfo, GithubLink)
reader := bufio.NewReader(os.Stdin)
for {
print(">>> ")
input, err := reader.ReadString('\n')
if err != nil {
log.Fatalf(err.Error())
}
input = strings.Trim(input, " \n\t")
if input == "" {
println("see you next time (exiting in 5 sec)")
time.Sleep(time.Second * 5)
os.Exit(0)
}
Cli := NewTikmehCli()
if err := Cli.Run(append([]string{PackageName}, strings.Split(input, " ")...)); err != nil {
log.Fatal(err)
}
}
}
}
type jsonOrOutput struct {
Files []string `json:"files"`
Error string `json:"error"`
}
type output struct {
IsJson bool
Json jsonOrOutput
}
func (out *output) file(file, message string) {
if out.IsJson {
out.Json.Files = append(out.Json.Files, file)
} else {
log.Println(message)
}
}
func (out *output) err(err, message string) {
if out.IsJson {
out.Json.Error = err
} else {
log.Println(message)
}
}
func NewTikmehCli() *cli.App {
return &cli.App{
Name: PackageName,
Usage: "TikTok downloader",
Version: fmt.Sprintf("%s [source code: %s]", VersionInfo, GithubLink),
Description: "Download TikTok videos in the best quality.\n" +
fmt.Sprintf("Examples:\n"+
" %s start in interactive mode\n"+
" %s tiktok.com/@shrimpydimpy/video/7133412834960018730 simply download a tiktok to the current directory\n"+
" %s --convert --directory goddess 7133412834960018730 download to ./goddess and convert to h.264\n"+
" %s --convert profile shrimpydimpy download shrimpydimpy videos and convert them to h.264\n"+
"(more help on downloading profiles -- %s profile help)",
os.Args[0], os.Args[0], os.Args[0], os.Args[0], os.Args[0]),
Action: func(ctx *cli.Context) error {
out := &output{IsJson: ctx.Bool("json"), Json: jsonOrOutput{Files: []string{}}}
defer func() {
if out.IsJson {
buffer, err := json.MarshalIndent(out.Json, "", " ")
if err != nil {
log.Fatalln(err.Error())
}
println(string(buffer))
}
}()
if ctx.String("directory") == "" {
err := ctx.Set("directory", DefaultWorkingDirectory)
if err != nil {
out.err(err.Error(), err.Error())
return nil
}
}
if _, err := os.Stat(ctx.String("directory")); os.IsNotExist(err) {
err := os.Mkdir(ctx.String("directory"), 0777)
if err != nil {
output := fmt.Sprintf("while creating directory %s, an error occured: %s", ctx.String("directory"), err.Error())
out.err(output, output)
return nil
}
}
for _, video := range ctx.Args().Slice() {
filename, err := tikwm.DownloadTiktok(video, ctx.String("directory"))
if err != nil {
output := fmt.Sprintf("while downloading %s, an error occured: %s", video, err.Error())
out.err(output, output)
return nil
}
if ctx.Bool("convert") {
h264, err := tikwm.ConvertToH264(filename, ctx.String("ffmpeg"))
if err != nil {
output := fmt.Sprintf("while converting %s, an error occured: %s", filename, err.Error())
out.err(output, output)
return nil
}
err = os.Rename(h264, filename)
if err != nil {
output := fmt.Sprintf("while converting %s, an error occured: %s", filename, err.Error())
out.err(output, output)
return nil
}
}
out.file(filename, fmt.Sprintf("downloaded %s", filename))
}
return nil
},
Commands: []*cli.Command{{
Name: "profile",
Usage: fmt.Sprintf("Downloads all videos of a TikTok user ('%s profile help' - more info)", os.Args[0]),
Aliases: []string{"p"},
Description: "Download all videos of a TikTok user, until already downloaded is met\n" +
fmt.Sprintf("Examples:\n"+
" %s profile shrimpydimpy losertron download all their videos to ./shrimpydimpy & ./losertron accordinaly\n"+
" %s -d . -c profile --all losertron download all shrimpydimpy videos to current directory, convert to h.264", os.Args[0], os.Args[0]),
Flags: []cli.Flag{&cli.BoolFlag{
Name: "all",
Aliases: []string{"a"},
Value: false,
Usage: "don't stop when an already downloaded video is met, to ensure everything is downloaded",
Category: "profile",
}},
Action: func(ctx *cli.Context) error {
out := output{IsJson: ctx.Bool("json"), Json: jsonOrOutput{Files: []string{}}}
defer func() {
if out.IsJson {
buffer, err := json.MarshalIndent(out.Json, "", " ")
if err != nil {
log.Fatalln(err.Error())
}
println(string(buffer))
}
}()
for _, username := range ctx.Args().Slice() {
downloader := tikwm.ProfileDownloader{
Username: username,
Directory: ctx.String("directory"),
FfmpegPath: ctx.String("ffmpeg"),
CheckAll: ctx.Bool("all"),
Convert: ctx.Bool("convert"),
}
if downloader.Directory == "" {
downloader.Directory = strings.ToLower(strings.Trim(username, "@ "))
}
for resp := range downloader.DownloadIteratively() {
switch resp.(type) {
case string:
out.file(resp.(string), fmt.Sprintf("downloaded %s", resp.(string)))
case error:
out.err(resp.(error).Error(), fmt.Sprintf("while downloading %s, an error occuer %s", username, resp.(error).Error()))
return nil
}
}
}
return nil
},
}},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "directory",
Aliases: []string{"d"},
Value: "",
Usage: "target directory (created if not found)",
DefaultText: "<dir>=username of the profile, or current for videos",
}, &cli.BoolFlag{
Name: "convert",
Aliases: []string{"c"},
Value: false,
Usage: "convert uploaded files to h264 with ffmpeg",
}, &cli.BoolFlag{
Name: "json",
Aliases: []string{"j"},
Value: false,
Usage: "print output as json",
}, &cli.StringFlag{
Name: "ffmpeg",
Aliases: []string{"f"},
Value: "ffmpeg",
Usage: "path to ffmpeg, (ffmpeg isn't required, unless you use 'convert')",
},
},
}
}