-
Notifications
You must be signed in to change notification settings - Fork 0
/
instafetch.go
182 lines (152 loc) · 4.11 KB
/
instafetch.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
// instafetch is a tool for quicky backing up an instagram account
// https://github.com/rarcega/instagram-scraper/commit/7ae2b3b2b80f7292a3a7bf036822ad6b23b7a9dd
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
"strings"
"sync"
"github.com/lepinkainen/instafetch/parser"
log "github.com/sirupsen/logrus"
"flag"
"os"
)
var (
// command line args
userName = flag.String("username", "", "Username to back up")
update = flag.Bool("update", false, "Update all existing downloads")
latest = flag.Bool("latest", false, "Only fetch the first page of each target")
debug = flag.Bool("debug", false, "Enable debug logging")
cron = flag.Bool("cron", false, "Silent run for running from cron (most useful with --latest)")
/*
rateLimitSleep = 60
downloadWorkerCount = 3
pageWorkerCount = 1 // anything above 1 here tends to trigger instagram's flood protection
*/
)
// downloadFile gets a single file defined by DownloadItem to outputFolder
func downloadFile(user parser.User, item parser.Node, outputFolder string) error {
var filename string
// create download dir for the account
err := os.MkdirAll(path.Join(outputFolder, user.Username), 0700)
if err != nil {
panic(err.Error())
}
url, err := url.Parse(item.URL)
if err != nil {
panic(err.Error())
}
tokens := strings.Split(url.Path, "/")
// Grab the actual filename from the path
filename = tokens[len(tokens)-1]
// Prepend the date the image was added to instagram and username to the file for additional metadata
// example output: 2017-11-05_alexandrabring_22860351_504365496598712_7456505757811343360_n.jpg
created := item.Timestamp.UTC().Format("2006-01-02")
filename = fmt.Sprintf("%s_%s_%s", created, user.Username, filename)
filename = path.Join(outputFolder, user.Username, filename)
// Create output file and check for its existence at the same time - no race conditions
// from: https://groups.google.com/d/msg/golang-nuts/Ayx-BMNdMFo/IVTRVqMECw8J
out, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
if os.IsExist(err) {
log.Debugf("Already downloaded: %s", filename)
return nil
}
if err != nil {
log.Errorln("Error when opening file for saving: %v", err)
return err
}
defer out.Close()
// download the file
resp, err := http.Get(item.URL)
if err != nil {
log.Errorf("Error when downloading: %v", err)
return err
}
defer resp.Body.Close()
// streams file to disk
_, err = io.Copy(out, resp.Body)
if err != nil {
log.Errorf("Could not write file to disk %v", err)
return err
}
if !*cron {
log.Printf("Downloaded: %s", filename)
}
return nil
}
func init() {
formatter := &log.TextFormatter{}
formatter.FullTimestamp = true
log.SetFormatter(formatter)
log.SetOutput(os.Stdout)
}
func main() {
flag.Parse()
if *cron {
log.SetLevel(log.WarnLevel)
} else {
log.SetLevel(log.InfoLevel)
}
// check for required variables
if *userName == "" && !*update {
fmt.Println("Usage: ")
flag.PrintDefaults()
return
}
ex, err := os.Executable()
if err != nil {
panic(err)
}
cwd := path.Dir(ex)
if *debug {
log.SetLevel(log.DebugLevel)
log.Debugf("Working directory %s", cwd)
}
outDir := path.Join(cwd, "output")
settings := parser.Settings{
Silent: *cron,
LatestOnly: *latest,
}
users := make(chan string)
var wgParsing sync.WaitGroup
wgParsing.Add(1)
go func() {
defer wgParsing.Done()
for uname := range users {
user, err := parser.ParseUser(uname, settings)
if err != nil {
log.Errorf("Error parsing user page: %v", err)
}
for _, node := range user.Nodes {
err := downloadFile(user, node, outDir)
if err != nil {
log.Errorf("Error downloading file: %v", err)
}
}
}
}()
if *update {
log.Infoln("Updating all existing sets")
// multiple accounts
// loop through directories in output and assume each is an userID
files, err := ioutil.ReadDir(outDir)
if err != nil {
panic(err.Error())
}
for _, f := range files {
if f.IsDir() {
users <- f.Name()
}
}
} else {
// Single account
users <- *userName
}
close(users)
wgParsing.Wait()
log.Info("Downloads done")
}