-
Notifications
You must be signed in to change notification settings - Fork 25
/
export.rb
179 lines (120 loc) · 3.68 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
require 'rubygems'
require 'httparty'
# Configuration
api_key = ENV["TUMBLR_API_KEY"]
username = ARGV[0] || ENV["TUMBLR_USERNAME"]
image_dir = ARGV.count == 2 ? ARGV[1] : (ARGV[1] || "images")
limit = 20 # number of posts requested each time
class TumblrPhotoExport
attr_accessor :username, :api_key, :image_dir, :limit, :url
def initialize(username, api_key, image_dir, limit)
@username = username
@api_key = api_key
@image_dir = image_dir
@limit = limit
@download_num = nil
@before = nil
@url = "http://api.tumblr.com/v2/blog/#{@username}.tumblr.com/likes?api_key=#{@api_key}"
puts "\033[32mURL\033[0m"
puts @url
puts "\n\033[32mUSERNAME\033[0m"
puts @username
puts "\n\033[32mDIR\033[0m"
puts @image_dir
puts "\n"
create_download_dir
end
def create_download_dir
Dir.mkdir("./#{@image_dir}") unless File.directory?("./#{@image_dir}")
end
def get_liked_count
response = HTTParty.get(@url + "&limit=1")
parsed_response = JSON.parse(response.body)
if parsed_response['meta']['status'] === 403
puts "\033[31m#{"ERROR"}\033[0m"
puts "Forbidden. Please, enable the 'Share posts you like' option in your tumblr settings"
return -1
elsif parsed_response['meta']['status'] === 401
puts "\033[31m#{"ERROR"}\033[0m"
puts "Unauthorized. Please, check your username and API_KEY"
return -1
end
return parsed_response['response']['liked_count']
end
def get_photos(limit = 0)
if @before
response = HTTParty.get(@url + "&limit=#{limit}&before=#{@before}")
else
response = HTTParty.get(@url + "&limit=#{limit}")
end
parsed_response = JSON.parse(response.body)
begin
before = parsed_response['response']['_links']['next']['query_params']['before']
rescue Exception => e
puts ":( #{e}"
end
# 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
download_likes(parsed_response['response']['liked_posts'])
@before = before
return true
end
def download_likes(likes)
likes.each do |like|
photos = like['photos']
puts "\033[37m#{like['blog_name']}\033[0m" if photos and photos.length > 0
photos.each do |photo|
begin
uri = photo['original_size']['url']
file = File.basename(uri)
File.open("./#{@image_dir}/" + file, "wb") do |f|
puts " #{uri}"
f.write HTTParty.get(uri).parsed_response
end
rescue => e
puts ":( #{e}"
end
end if photos
end
end
def start
begin
@download_num = get_liked_count
if @download_num > 0
download
end
rescue Exception => e
puts "\033[31m#{"Error: #{e} "}\033[0m"
puts "\033[31m#{"Error: #{e.backtrace.inspect} "}\033[0m"
end
end
def download
parsed = 0
rest = @download_num % @limit
if rest > 1
rest = 1
end
batchs = (@download_num / @limit) + rest
if (@download_num < @limit)
batchs = 1
@limit = @download_num
end
puts "Downloading \033[32m#{@download_num}\033[0m posts\n"
batchs.times do |i|
if parsed + @limit > @download_num
@limit = @download_num - parsed
end
result = get_photos(@limit)
parsed += @limit
break if !result
end
puts "\033[32m#{"Aaaaand we're done, parsed #{parsed} "}\033[0m"
end
end
tumblr = TumblrPhotoExport.new(username, api_key, image_dir, limit)
tumblr.start