discourse/script/import_scripts/socialcast/export.rb
Sam Saffron 30990006a9 DEV: enable frozen string literal on all files
This reduces chances of errors where consumers of strings mutate inputs
and reduces memory usage of the app.

Test suite passes now, but there may be some stuff left, so we will run
a few sites on a branch prior to merging
2019-05-13 09:31:32 +08:00

61 lines
1.3 KiB
Ruby

# frozen_string_literal: true
require 'yaml'
require 'fileutils'
require_relative 'socialcast_api'
def load_config(file)
config = YAML::load_file(File.join(__dir__, file))
@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
def export_users(page = 1)
users = @api.list_users(page: page)
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
def export_messages(page = 1)
messages = @api.list_messages(page: page)
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