-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
95 lines (76 loc) · 2.04 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
package main
import (
"fmt"
"github.com/charmbracelet/huh/spinner"
"github.com/onihani/go-tweet/internal/downloader"
"github.com/onihani/go-tweet/internal/form"
"github.com/onihani/go-tweet/internal/models"
"github.com/onihani/go-tweet/internal/utils"
)
func main() {
url, err := form.CollectTweetUrl()
if err != nil {
fmt.Println("Failed to get tweet url:", err)
return
}
var tweetId string
err = spinner.New().Title("Extracting tweet id from url...").Accessible(false).Action(func() {
id, err := utils.ExtractTweetID(url)
if err != nil {
panic(err)
}
tweetId = id
}).Run()
if err != nil {
fmt.Println("Failed to extract tweet id from url:", err)
return
}
var tweet *models.Tweet
err = spinner.New().Title("Fetching tweet data...").Accessible(false).Action(func() {
t, err := downloader.FetchTweet(tweetId)
if err != nil {
panic(err)
}
tweet = t
}).Run()
if err != nil {
fmt.Println("Error fetching tweet:", err)
return
}
tweetVideos := tweet.GetVideos()
var resolutions []string
for resolution := range tweetVideos {
resolutions = append(resolutions, resolution)
}
if len(resolutions) <= 0 {
fmt.Println("No videos found")
return
}
selectedResolution, err := form.SelectResolution(resolutions)
if err != nil {
fmt.Println("Error selecting resolutin:", err)
return
}
selectedVideo := tweetVideos[selectedResolution]
downloadDirectory, err := form.CollectStringInput("Enter path to the directory you want to download video to")
if err != nil {
fmt.Println("Error collecting download directory:", err)
return
}
var downloadedFilePath string
err = spinner.New().
Title(fmt.Sprintf("Downloading video at %s to %s...", selectedResolution, downloadDirectory)).
Accessible(false).
Action(func() {
dfPath, err := selectedVideo.Download(downloadDirectory, tweet.Text)
if err != nil {
panic(err)
}
downloadedFilePath = dfPath
}).Run()
if err != nil {
fmt.Println("Failed to download video", err)
return
}
fmt.Println("Video downloaded to:", downloadedFilePath)
}