-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtumblr_export.rb
More file actions
25 lines (21 loc) · 843 Bytes
/
tumblr_export.rb
File metadata and controls
25 lines (21 loc) · 843 Bytes
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
require "json"
require "tumblr_client"
Tumblr.configure do |config|
config.consumer_key = ENV.fetch("consumer_key")
config.consumer_secret = ENV.fetch("consumer_secret")
config.oauth_token = ENV.fetch("oauth_token")
config.oauth_token_secret = ENV.fetch("oauth_token_secret")
end
class TumblrClient
def self.client
@@client ||= Tumblr::Client.new
end
end
blog_name = ENV.fetch("blog_name")
number_of_posts = TumblrClient.client.info["user"]["blogs"].find { |blog| blog["name"] == blog_name }["posts"]
all_posts = 0.step(by: 21, to: number_of_posts).flat_map do |offset|
puts "Fetching posts #{offset}..#{offset + 21}"
posts = TumblrClient.client.posts(blog_name, offset: offset)
posts["posts"]
end ; nil
File.open("#{blog_name}_posts.json", "w") { |file| file.write(JSON.pretty_generate(all_posts)) }