Last active
January 5, 2022 21:08
-
-
Save icelander/9e638f897e6df4b2b4a31b0bc7b5d38c to your computer and use it in GitHub Desktop.
Removes attachments and emoji from Mattermost bulk export file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'json' | |
## How to use | |
# | |
# 1. Generate a Mattermost bulk export file with attachments | |
# 2. Unzip the file and find the .jsonl file with the post contents and put its filename on the next line | |
filename = 'import.jsonl' | |
# 3. Run it and direct the output to a new file, e.g. | |
# | |
# ruby remove-attachments.rb > import_no_attachments.jsonl | |
posts = [] | |
emoji = [] | |
other = [] | |
File.readlines(filename).each do |line| | |
data = JSON.parse(line) | |
if data['type'] == 'post' or data['type'] == 'direct_post' | |
$stderr.puts "Found #{data['type']}" | |
type = data['type'] | |
if data[type].key?('attachments') | |
$stderr.puts "#{type} has an attachments key " | |
begin | |
if !data[type]['attachments'].nil? and data[type]['attachments'].length > 0 | |
$stderr.puts "#{type} has #{data[type]['attachments'].length} attachment(s). Resetting" | |
data[type]['attachments'] = [] | |
end | |
rescue Exception => e | |
$stderr.puts e.to_s | |
$stderr.puts data[type].to_json | |
end | |
end | |
posts << data.to_json | |
else | |
other << line | |
end | |
end | |
other.each do |line| | |
puts line | |
end | |
posts.each do |post| | |
puts post | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment