-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
174 lines (144 loc) · 4.45 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
package main
import (
"flag"
"fmt"
"log"
"sync"
)
var (
username string
option string
)
func main() {
flag.StringVar(&username, "username", "", "The Instagram username")
flag.StringVar(&option, "option", "", "The parsing option 'story' or 'highlight'")
flag.Parse()
// handle if username is not provided in the flag
if username == "" {
log.Fatal("Username not provided in the flag. use --help flag for more info")
}
// handle if options is not provided in the flag
if option == "" {
log.Fatal("Option not provided in the flag. use --help flag for more info")
}
log.SetFlags(log.LstdFlags | log.Lshortfile)
fmt.Println("Checking connection to the API please wait..")
err := CheckAPIURLConnection()
if err != nil {
log.Fatal(err.Error())
}
fmt.Println("The connection seems ok!")
userInformation, err := GetUserInformation(username)
if err != nil {
log.Fatal(err.Error())
}
switch option {
case "story":
directoryName := fmt.Sprintf("%s/%s", "stories", userInformation.Result.User.Username)
userStories, err := GetUserStories(userInformation)
if err != nil {
log.Fatal(err.Error())
}
err = CreateDir(directoryName)
if err != nil {
log.Fatal(err.Error())
}
storyFiles := ParseContent(userStories)
fmt.Println("=======================================")
fmt.Printf("= Name\t\t: %s (@%s)\n", userInformation.Result.User.FullName, userInformation.Result.User.Username)
fmt.Printf("= Followers\t: %d\n", userInformation.Result.User.FollowerCount)
fmt.Printf("= Followings\t: %d\n", userInformation.Result.User.FollowingCount)
fmt.Printf("= Public Email\t: %s\n", userInformation.Result.User.PublicEmail)
fmt.Println("=======================================")
fmt.Printf("Found %d stories for the user\n", len(*storyFiles))
var wg sync.WaitGroup
for _, f := range *storyFiles {
wg.Add(1)
go func(f File) {
defer wg.Done()
fmt.Printf("Downloading... %s%s\n", f.Name, f.Extension)
fileStream, err := GetFile(f.URL)
if err != nil {
log.Fatal(err.Error())
}
defer fileStream.Body.Close()
createdFileStream, err := CreateFile(directoryName, f, fileStream.Body)
if err != nil {
log.Fatal(err.Error())
}
defer createdFileStream.Close()
fmt.Printf("Downloaded... %s%s\n", f.Name, f.Extension)
}(f)
}
wg.Wait()
fmt.Println("All stories have been downloaded!")
case "highlight":
directoryName := fmt.Sprintf("%s/%s", "highlights", userInformation.Result.User.Username)
userHightlightList, err := GetUserStoryHighlights(userInformation)
if err != nil {
log.Fatal(err.Error())
}
var highlights []struct {
Number int
ID string
Title string
}
for i, v := range userHightlightList.Result {
highlights = append(highlights, struct {
Number int
ID string
Title string
}{
Number: i + 1,
ID: v.ID,
Title: v.Title,
})
}
fmt.Println("=======================================")
fmt.Printf("= Found the users with %d story highlights\n", len(highlights))
for _, v := range highlights {
fmt.Printf("= %d. %s\n", v.Number, v.Title)
}
fmt.Println("=======================================")
var highlightNumber int
fmt.Print("Which story highlights you want to download (in number): ")
fmt.Scan(&highlightNumber)
for _, v := range highlights {
if v.Number == highlightNumber {
userHighlightStories, err := GetUserHighlightStory(v.ID)
if err != nil {
log.Fatal(err.Error())
}
hightlightFiles := ParseContent(userHighlightStories)
highlightDirWithSelectedNumber := fmt.Sprintf("%s/%v", directoryName, v.Number)
err = CreateDir(highlightDirWithSelectedNumber)
if err != nil {
log.Fatal(err.Error())
}
var wg sync.WaitGroup
for _, f := range *hightlightFiles {
wg.Add(1)
go func(f File) {
defer wg.Done()
fmt.Printf("Downloading... %s%s\n", f.Name, f.Extension)
fileStream, err := GetFile(f.URL)
if err != nil {
log.Fatal(err.Error())
}
defer fileStream.Body.Close()
createdFileStream, err := CreateFile(highlightDirWithSelectedNumber, f, fileStream.Body)
if err != nil {
log.Fatal(err.Error())
}
defer createdFileStream.Close()
fmt.Printf("Downloaded... %s%s\n", f.Name, f.Extension)
}(f)
}
wg.Wait()
fmt.Println("All highlight story have been downloaded!")
}
}
default:
log.Fatal("Option are not available! Please check --help for more information.")
}
}