discourse/script/import_scripts/socialcast/export.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

61 lines
1.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-05-26 14:54:38 +08:00
require 'yaml'
require 'fileutils'
require_relative 'socialcast_api'
2017-07-28 09:20:09 +08:00
def load_config(file)
config = YAML::load_file(File.join(__dir__, file))
2016-05-26 14:54:38 +08:00
@domain = config['domain']
@username = config['username']
@password = config['password']
end
def export
@api = SocialcastApi.new @domain, @username, @password
create_dir("output/users")
create_dir("output/messages")
export_users
export_messages
end
2017-07-28 09:20:09 +08:00
def export_users(page = 1)
users = @api.list_users(page: page)
2016-05-26 14:54:38 +08:00
return if users.empty?
users.each do |user|
File.open("output/users/#{user['id']}.json", 'w') do |f|
puts user['contact_info']['email']
f.write user.to_json
f.close
end
end
export_users page + 1
end
2017-07-28 09:20:09 +08:00
def export_messages(page = 1)
messages = @api.list_messages(page: page)
2016-05-26 14:54:38 +08:00
return if messages.empty?
messages.each do |message|
File.open("output/messages/#{message['id']}.json", 'w') do |f|
title = message['title']
title = message['body'] if title.empty?
title = title.split('\n')[0][0..50] unless title.empty?
puts "#{message['id']}: #{title}"
f.write message.to_json
f.close
end
end
export_messages page + 1
end
def create_dir(path)
path = File.join(__dir__, path)
unless File.directory?(path)
FileUtils.mkdir_p(path)
end
end
load_config ARGV.shift
export