-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexport.rb
348 lines (279 loc) · 9.01 KB
/
export.rb
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
344
345
346
347
348
# USE AT YOUR VERY OWN RISK
# Heavily inspired by, mmmmm, ripped from https://github.com/javierarce/tumblr-photo-export/
require 'rubygems'
require 'httparty'
require 'reverse_markdown'
# Configuration
what = ARGV[0] #posts, likes
username = ARGV[1] || ENV["TUMBLR_USERNAME"]
api_key = ARGV[2] || ENV["TUMBLR_CONSUMER_KEY"]
public_dir = "public"
liked_dir = "liked"
where = public_dir
image_subdir = "images"
download_num = -1 # number of posts to download, use -1 for ALL
limit = 3 # number of posts requested each time
class TumblrPhotoExport
attr_accessor :username, :api_key, :what, :where, :public_dir, :liked_dir, :image_subdir, :limit, :download_num, :url
def initialize(username, api_key, what, where, public_dir, liked_dir, image_subdir, limit, download_num)
@username = username
@api_key = api_key
@what = what
if @what == "likes"
@where = liked_dir
@whatkey = "liked_posts"
@whatcount= "liked_count"
else
@where = public_dir
@whatkey = "posts"
@whatcount= "total_posts"
end
# URL to get Posts
@url = "http://api.tumblr.com/v2/blog/#{@username}.tumblr.com/#{@what}?api_key=#{@api_key}"
@public_dir = public_dir
@liked_dir = liked_dir
@image_subdir = image_subdir
@limit = limit
@download_num = download_num
if @download_num == -1
@download_num = get_results_count
end
@index_arr = []
@indexstart = "---\nARCHIVE: "
@indexend = "\n---"
create_dirs
end
def create_dirs
Dir.mkdir("./#{@where}") unless File.directory?("./#{@where}")
Dir.mkdir("./#{@where}/#{@image_subdir}") unless File.directory?("./#{@where}/#{@image_subdir}")
end
def get_results_count
response = HTTParty.get(@url)
parsed_response = JSON.parse(response.body)
total_count = parsed_response['response'][@whatcount].to_i
puts "total count: #{total_count}"
return total_count
end
def get_results(limit = 0, offset = 0)
response = HTTParty.get(@url + "&limit=#{limit}&offset=#{offset}")
parsed_response = JSON.parse(response.body)
# Status of the request
status_code = parsed_response['meta']['status']
status_msg = parsed_response['meta']['msg']
if status_code != 200
puts "\033[91m#{status_msg}\033[0m"
return
end
if parsed_response['response'][@whatkey].length > 0
parse_posts(parsed_response['response'][@whatkey])
return true
else
return
end
end
def parse_posts(posts)
# for each item in posts
$i = 0
$LEN = posts.length
until $i == $LEN
$post = posts[$i]
parse_post_common($post)
send("parse_post_#{$type}".to_sym, $post)
# push basic data to index array so we can build the archive
if $title == nil
$title = $slug
end
@index_arr.push Hash[
"STATE" => $state,
"TYPE" => $type,
"DATE" => $date,
"SLUG" => $slug,
"TITLE" => $title
]
$i += 1
end
end
def parse_post_common(post)
# common post data:
$id = post['id']
$slug = post['slug']
if $slug == ""
$slug = $id
end
$type = post['type']
$date = post['date']
$timestamp = post['timestamp']
$format = post['format']
$source_url = post['source_url']
$source_title = post['source_title']
$short_url = post['short_url']
$blog_name = post['blog_name'] # useful to keep track in liked posts
$post_url = post['post_url']
$tags = post['tags']
$state = post['state']
# common md output content
$headerstart = "---\nlayout: post\ntype: #$type"
$headerend = "path: #$slug\npost_url: #$post_url\ntags: #$tags\ncreated: #$date\n---\n\n"
end
def parse_post_text(post)
puts "parse_post_text"
$title = post['title']
$body = post['body']
$headercustom = "title: #$title"
if $format == "html"
$body = get_md($body)
end
# write post to disk
write_post("./#{@where}/", "#$slug.md", "\n#$headerstart\n#$headercustom\n#$headerend\n\n#$body")
end
def parse_post_quote(post)
puts "parse_post_quote"
$text = post['text']
$source = post['source']
$headercustom = "title: no title"
if $format == "html"
$text = get_md($text)
$source = get_md($source)
end
# write post to disk
write_post("./#{@where}/", "#$slug.md", "\n#$headerstart\n#$headercustom\n#$headerend\n\n#$text\n\n#$source")
end
def parse_post_link(post)
puts "parse_post_link"
$title = post['title']
$url = post['url']
$url = "[#$title](#$url)"
$description = post['description']
$headercustom = "title: #$title"
if $format == "html"
$description = get_md($description)
end
# write post to disk
write_post("./#{@where}/", "#$slug.md", "\n#$headerstart\n#$headercustom\n#$headerend\n\n#$url\n#$description")
end
def parse_post_video(post)
puts "parse_post_video"
$caption = $post['caption']
$player = $post['player']
$headercustom = "title: no title"
if $format == "html"
$caption = get_md($caption)
end
# write post to disk
write_post("./#{@where}/", "#$slug.md", "\n#$headerstart\n#$headercustom\n#$headerend\n\n#$caption")
end
def parse_post_photo(post)
puts "parse_post_photo"
$caption = post['caption']
$images_arr = []
$num_photos = post['photos'].length
$headercustom = "num_photos: #$num_photos\ntitle: no title"
$folder = "./#{@where}/"
$subfolder = "#{@image_subdir}/"
# write all source images to disk
$num_photos.times do |i|
$image = post['photos'][i]
$image_url = $image['original_size']['url']
$image_cap = $image['caption']
$image_str = ""
$extension = $image_url.split('.').last
$filename = "#$slug"+"_"+i.to_s+"."+$extension
if $format == "html"
$image_cap = get_md($image_cap)
end
if $image_cap == ""
$image_str = "[#$slug](#$folder#$subfolder#$filename)"
else
$image_str = "[#$image_cap](#$folder#$subfolder#$filename)"
end
$images_arr.push $image_str
write_file("#$folder#$subfolder", $image_url, $filename)
end
if $format == "html"
$caption = get_md($caption)
end
# inject images array into $headercustom
$headercustom += "\nimages: #$images_arr"
# write post to disk
write_post("./#{@where}/", "#$slug.md", "\n#$headerstart\n#$headercustom\n#$headerend\n\n#$caption")
end
def parse_post_chat(post)
puts "parse_post_chat"
# same format as text
parse_post_text(post)
end
def parse_post_audio(post)
puts "parse_post_audio"
$caption = $post['caption']
$url = $post['audio_url']
$url = "[#$url](#$url)"
$headercustom = "title: no title"
if $format == "html"
$caption = get_md($caption)
end
# write post to disk
write_post("./#{@where}/", "#$slug.md", "\n#$headerstart\n#$headercustom\n#$headerend\n\n#$url\n#$caption")
end
def parse_post_answer(post)
# nothing to see here, old post type
end
def get_md(html)
return ReverseMarkdown.convert html
end
def write_file(folder, fileuri, filename)
filename = File.basename(filename)
begin
File.open(folder + filename, "wb") do |f|
f.write HTTParty.get(fileuri).parsed_response
end
rescue => e
puts ":( #{e}"
end
end
def write_post(folder, filename, content)
begin
File.open(folder + filename, "wb") do |f|
f.write content
end
rescue => e
puts ":( #{e}"
end
end
def write_index(folder, content)
begin
File.open(folder + "______index.md", "wb") do |f|
f.write content
end
rescue => e
puts ":( #{e}"
end
end
def start
parsed = 0
rest = @download_num % @limit
if rest > 1
rest = 1
end
batchs = (@download_num / @limit) + rest
puts "batchs download_num #{download_num} limit #{limit} batchs #{batchs}"
if (@download_num < @limit)
batchs = 1
@limit = @download_num
end
puts ":::::::::::::::::::::::::: Downloading \033[32m#{@download_num}\033[0m posts"
batchs.times do |i|
offset = i*@limit
if parsed + @limit > @download_num
@limit = @download_num - parsed
end
puts "::::::::::::: step #{i} parsed #{parsed} limit #{@limit} offset #{offset}"
result = get_results(@limit, offset)
parsed += @limit
break if !result
end
puts "\033[32m#{"Aaaaand we're done, parsed #{parsed} "}\033[0m"
write_index("./#{@where}/", "\n#@indexstart #@index_arr #@indexend")
end
end
tumblr = TumblrPhotoExport.new(username, api_key, what, where, public_dir, liked_dir, image_subdir, limit, download_num)
tumblr.start