-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (65 loc) · 1.94 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
package main
import (
"flag"
"log"
"sync"
)
var (
username string
)
func main() {
flag.StringVar(&username, "username", "", "the Instagram username")
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")
}
log.SetFlags(log.LstdFlags | log.Lshortfile)
log.Println("checking connection to the API please wait..")
err := CheckAPIURLConnection()
if err != nil {
log.Fatal(err.Error())
}
log.Println("the connection seems ok!")
userInformation, err := GetUserInformation(username)
if err != nil {
log.Fatal(err.Error())
}
userStories, err := GetUserStories(userInformation)
if err != nil {
log.Fatal(err.Error())
}
err = CreateDir(userInformation.Result.User.Username)
if err != nil {
log.Fatal(err.Error())
}
storyFiles := ParseStory(userStories)
log.Println("=======================================")
log.Printf("Name\t\t: %s (@%s)", userInformation.Result.User.FullName, userInformation.Result.User.Username)
log.Printf("Followers\t: %d", userInformation.Result.User.FollowerCount)
log.Printf("Followings\t: %d", userInformation.Result.User.FollowingCount)
log.Printf("Public Email\t: %s", userInformation.Result.User.PublicEmail)
log.Println("=======================================")
log.Printf("Found %d stories for the user", len(*storyFiles))
var wg sync.WaitGroup
for _, f := range *storyFiles {
wg.Add(1)
go func(f File) {
defer wg.Done()
log.Printf("Downloading... %s%s", f.Name, f.Extension)
fileStream, err := GetFile(f.URL)
if err != nil {
log.Fatal(err.Error())
}
defer fileStream.Body.Close()
createdFileStream, err := CreateFile(f, *userInformation, fileStream.Body)
if err != nil {
log.Fatal(err.Error())
}
defer createdFileStream.Close()
log.Printf("Downloaded... %s%s", f.Name, f.Extension)
}(f)
}
wg.Wait()
log.Println("All stories have been downloaded!")
}