This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
tumblr-downloader.go
343 lines (274 loc) · 8.56 KB
/
tumblr-downloader.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Tumblr Downloader is an application that authenticates to your Tumblr
// account, and downloads all the liked photos to your computer.
//
// This file is part of Tumblr Downloader
//
// Tumblr Downloader is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Tumblr Downloader is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Tumblr Downloader. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"path"
"regexp"
"strconv"
"strings"
"time"
"github.com/dghubble/oauth1"
"github.com/elgs/gojq"
"github.com/pkg/browser"
)
const limit = 50
var (
blogIdentifier string
consumerKey string
consumerSecret string
callbackURL string
config oauth1.Config
outputFolderName string
startOffset string
debug bool
downloadNoDirs bool
reMediaURL = regexp.MustCompile("src=\"([^\"]+)\"")
)
var lock chan string
var httpClient *http.Client
func checkError(err error) bool {
if err != nil {
fmt.Println("There was an error:")
fmt.Println(err)
time.Sleep(500 * time.Millisecond)
os.Exit(1)
}
return err != nil
}
func initOauthConfig() {
config = oauth1.Config{
ConsumerKey: consumerKey,
ConsumerSecret: consumerSecret,
CallbackURL: callbackURL,
Endpoint: oauth1.Endpoint{
RequestTokenURL: "https://www.tumblr.com/oauth/request_token",
AuthorizeURL: "https://www.tumblr.com/oauth/authorize",
AccessTokenURL: "https://www.tumblr.com/oauth/access_token",
},
}
}
func getConfigValue(key string) string {
value := os.Getenv(key)
if value == "" {
checkError(errors.New(key + " is missing"))
}
return value
}
func loadConfig() {
blogIdentifier = getConfigValue("BLOG_IDENTIFIER")
consumerKey = getConfigValue("CONSUMER_KEY")
consumerSecret = getConfigValue("CONSUMER_SECRET")
callbackURL = getConfigValue("CALLBACK_URL")
blogIdentifier = strings.Replace(blogIdentifier, "http://", "", -1)
blogIdentifier = strings.Replace(blogIdentifier, "https://", "", -1)
blogIdentifier = strings.Replace(blogIdentifier, ".tumblr.com", "", -1)
blogIdentifier = blogIdentifier + ".tumblr.com"
outputFolderName = getConfigValue("TARGET_LOCATION") + string(os.PathSeparator) + blogIdentifier
os.Mkdir(outputFolderName, os.ModePerm)
startOffset = getConfigValue("START_OFFSET")
debug, _ = strconv.ParseBool(getConfigValue("DEBUG_MODE"))
downloadNoDirs, _ = strconv.ParseBool(getConfigValue("DOWNLOAD_NO_DIRS"))
}
func authTumblr() {
requestToken, requestSecret, _ := config.RequestToken()
authorizationURL, _ := config.AuthorizationURL(requestToken)
fmt.Println("Authorization URL: " + authorizationURL.String())
fmt.Println("Please check your browser, and log into Tumblr as usual, then come back to this application.")
browser.OpenURL(authorizationURL.String())
listen, err := net.Listen("tcp", ":8080")
checkError(err)
http.HandleFunc("/tumblr/callback", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Success! Now go check the app!")
verifier := r.URL.Query().Get("oauth_verifier")
token2 := r.URL.Query().Get("oauth_token")
fmt.Println(verifier, token2)
listen.Close()
accessToken, accessSecret, err := config.AccessToken(token2, requestSecret, verifier)
checkError(err)
token := oauth1.NewToken(accessToken, accessSecret)
ctx := context.Background()
httpClient = config.Client(ctx, token)
fmt.Println("Releasing now")
time.Sleep(1000 * time.Millisecond)
lock <- "Go"
})
http.Serve(listen, nil)
}
func tumblrGetLikes(blogIdentifier string, timestamp int) *gojq.JQ {
urlPath := "http://api.tumblr.com/v2/blog/" + blogIdentifier + "/likes?&api_key=" + consumerKey + "&limit=" + strconv.Itoa(limit)
if timestamp == 0 {
urlPath += "&offset=" + startOffset
}
if timestamp != 0 {
urlPath += "&before=" + strconv.Itoa(timestamp)
}
if debug {
fmt.Println("[DEBUG] - " + urlPath)
}
resp, err := httpClient.Get(urlPath)
checkError(err)
body, err := ioutil.ReadAll(resp.Body)
checkError(err)
parser, err := gojq.NewStringQuery(string(body))
checkError(err)
if debug {
mapRes, _ := parser.QueryToMap(".")
res, _ := json.MarshalIndent(mapRes, "", " ")
fmt.Println("[DEBUG] - " + string(res))
}
return parser
}
// Check whether the given URL belongs to tumblr.com
func isTumblr(URL string) bool {
parsedURL, err := url.Parse(URL)
checkError(err)
return strings.Contains(parsedURL.Hostname(), "tumblr.com")
}
func downloadURL(URL string, SubDir string) {
fileName := ""
if isTumblr(URL) {
fmt.Println("Downloading", URL)
fileName = path.Base(URL)
}
if fileName != "" {
var outputFilePath string
if downloadNoDirs {
outputFilePath = outputFolderName + string(os.PathSeparator) + fileName
} else {
outputFilePath = outputFolderName + string(os.PathSeparator) + SubDir + fileName
os.Mkdir(outputFolderName + string(os.PathSeparator)+SubDir, os.ModePerm)
}
_, err := os.Stat(outputFilePath)
if os.IsNotExist(err) {
resp, err := http.Get(URL)
outFile, err := os.Create(outputFilePath)
checkError(err)
defer outFile.Close()
_, err = io.Copy(outFile, resp.Body)
} else {
fmt.Println("File already exists, skipping")
}
} else {
fmt.Println("No tumblr link, skipping")
}
}
func run() {
lastTimestamp := 0
counter := 1
counterStartOffset, _ := strconv.ParseInt(startOffset, 10, 32)
counter = counter + int(counterStartOffset)
for {
fmt.Printf("Ask for liked posts %d - %d... ", counter, counter+limit-1)
parser := tumblrGetLikes(blogIdentifier, lastTimestamp)
likedPosts, err := parser.QueryToArray("response.liked_posts")
checkError(err)
if len(likedPosts) == 0 {
fmt.Println("0 rows fetched")
os.Exit(1)
} else {
fmt.Println("done")
}
for _, v := range likedPosts {
postType, err := gojq.NewQuery(v).Query("type")
checkError(err)
postBlogName, err := gojq.NewQuery(v).Query("blog_name")
checkError(err)
lastTimestampString, err := gojq.NewQuery(v).Query("liked_timestamp")
checkError(err)
lastTimestamp = int(lastTimestampString.(float64))
if postType == "photo" {
postPhotos, err := gojq.NewQuery(v).QueryToArray("photos")
checkError(err)
for i, v2 := range postPhotos {
url, err := gojq.NewQuery(v2).Query("original_size.url")
checkError(err)
switch url.(type) {
case string:
fmt.Print("[" + strconv.Itoa(counter) + "/" + strconv.Itoa(i) + "] ")
downloadURL(url.(string), postBlogName.(string)+string(os.PathSeparator))
}
}
}
if postType == "video" {
videoUrl, err := gojq.NewQuery(v).Query("video_url")
if err == nil && videoUrl != nil {
switch videoUrl.(type) {
case string:
fmt.Print("[" + strconv.Itoa(counter) + "] ")
downloadURL(videoUrl.(string), postBlogName.(string)+string(os.PathSeparator))
}
} else {
postPlayer, err := gojq.NewQuery(v).QueryToArray("player")
checkError(err)
for _, v2 := range postPlayer {
url, err := gojq.NewQuery(v2).Query("embed_code")
checkError(err)
switch url.(type) {
case string:
matches := reMediaURL.FindAllStringSubmatch(url.(string), -1)
for _, match := range matches {
urlVideo := match[1]
fmt.Println("Downloading object from multi-media bundle")
downloadURL(urlVideo, postBlogName.(string)+string(os.PathSeparator))
}
}
}
}
}
if postType == "text" {
postBody, err := gojq.NewQuery(v).Query("body")
checkError(err)
if debug {
fmt.Println("Downloading media from text post")
}
matches := reMediaURL.FindAllStringSubmatch(postBody.(string), -1)
for _, match := range matches {
urlVideo := match[1]
downloadURL(urlVideo, postBlogName.(string)+string(os.PathSeparator))
}
}
counter += 1
}
}
}
func main() {
fmt.Println("Initializing environment... ")
loadConfig()
initOauthConfig()
lock = make(chan string, 1)
fmt.Println("Authorizing on tumblr... ")
go func() {
fmt.Println("Waiting until authorization flow is completed")
<-lock
fmt.Println("Authorization complete")
run()
lock <- "Done"
}()
authTumblr()
<-lock
}