Skip to content
This repository was archived by the owner on Sep 24, 2024. It is now read-only.

Commit f468363

Browse files
author
Przemyslaw Mroczek
authored
Merge pull request #33 from thnukid/feature/anonymized-export
Feature/anonymized export
2 parents 1e13168 + 5ed3017 commit f468363

10 files changed

Lines changed: 1899 additions & 0 deletions

File tree

bin/anonymize_facebook_export

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require 'nokogiri'
5+
require 'fileutils'
6+
require 'micro-optparse'
7+
8+
module AnonymizedFacebookExport
9+
# Creates a collection of html/htm files
10+
# from the given directory path
11+
class FileCollection
12+
def initialize(path)
13+
@directory = File.join(path)
14+
end
15+
16+
def process
17+
all_files_from_directory
18+
end
19+
20+
private
21+
22+
def all_files_from_directory
23+
root_html_files + root_htm_files +
24+
directory_html_files + directory_htm_files
25+
end
26+
27+
def directory_html_files
28+
Dir.glob(File.join(@directory, '*', '*.html'))
29+
end
30+
31+
def directory_htm_files
32+
Dir.glob(File.join(@directory, '*', '*.htm'))
33+
end
34+
35+
def root_html_files
36+
Dir.glob(File.join(@directory, '*.html'))
37+
end
38+
39+
def root_htm_files
40+
Dir.glob(File.join(@directory, '*.htm'))
41+
end
42+
end
43+
44+
# Will load the given file with nokogiri
45+
# Replaces the content of the file with asterix
46+
class AnonFile
47+
def initialize(file)
48+
@doc = Nokogiri::HTML(File.open(file))
49+
end
50+
51+
def process
52+
remove_content_from_file
53+
export_to_html
54+
end
55+
56+
private
57+
58+
def export_to_html
59+
@doc.to_html
60+
end
61+
62+
# Searches the doc for nokogiri text objects,
63+
# replaces all non-whitespace characters
64+
def remove_content_from_file
65+
@doc.search('//text()').each do |node|
66+
next if node.path.include?('style') # keep css style
67+
node.content = node.text.gsub!(/\S/, '*')
68+
end
69+
end
70+
end
71+
72+
# Anonymizes the input directory and writes the files to
73+
# the output directory
74+
class CreateAnonExport
75+
attr_reader :anon_files
76+
77+
def initialize(input_path, output_path)
78+
@facebook_export_path = input_path
79+
@facebook_export_files = FileCollection.new(@facebook_export_path).process
80+
@anon_export_path = output_path
81+
end
82+
83+
def process
84+
anon_all_files
85+
write_files_to_export_directory
86+
end
87+
88+
private
89+
90+
def write_files_to_export_directory
91+
@anon_files.each do |export_anon_file|
92+
export_file_path = export_anon_file[:original_path].gsub!(@facebook_export_path, @anon_export_path)
93+
export_directory = File.dirname(export_file_path)
94+
95+
unless File.directory?(export_directory)
96+
FileUtils.mkdir_p(export_directory)
97+
end
98+
99+
File.open(export_file_path, 'w') do |file|
100+
file.write(export_anon_file[:html])
101+
end
102+
103+
puts "Wrote anonymized: #{export_file_path}"
104+
end
105+
end
106+
107+
def anon_all_files
108+
@anon_files = @facebook_export_files.map do |export_file|
109+
puts "Anon file #{export_file}"
110+
{ original_path: export_file, html: AnonFile.new(export_file).process }
111+
end
112+
end
113+
end
114+
end
115+
116+
options = Parser.new do |p|
117+
p.banner = ['[Usage]:', 'anonymized_facebook_export',
118+
'--catalog <facebook_export_directory>',
119+
'--export <anonymized_export_directory',
120+
'This will anonymize the facebook',
121+
'export by removing text content'].join(' ')
122+
p.option :catalog, 'set directory to facebook export',
123+
default: 'example/facebook-monaleigh'
124+
p.option :export, 'set the name of export folder',
125+
default: 'example/facebook-monaleigh-anonymized'
126+
end.process!
127+
128+
AnonymizedFacebookExport::CreateAnonExport.new(options.fetch(:catalog), options.fetch(:export)).process
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
2+
<html>
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5+
<base href="../">
6+
<style type="text/css">/* Copyright 2004-present Facebook. All Rights Reserved. */
7+
body {
8+
color: black;
9+
font-family: Arial, sans-serif;
10+
font-size: 11pt;
11+
margin: 15px auto;
12+
width: 860px;
13+
}
14+
15+
.nav {
16+
float: left;
17+
width: 200px;
18+
}
19+
20+
.nav ul {
21+
margin: 10px 0 0 0;
22+
padding: 0;
23+
}
24+
25+
.nav li {
26+
display: block;
27+
font-size: 15px;
28+
list-style: none;
29+
padding: 5px 10px;
30+
width: 160px;
31+
}
32+
33+
.nav .selected {
34+
background: #EEE;
35+
font-weight: bold;
36+
}
37+
38+
h1 {
39+
border-bottom: 1px solid #CCC;
40+
padding-bottom: 10px;
41+
}
42+
43+
.contents {
44+
padding-left: 210px;
45+
}
46+
47+
a, a:visited {
48+
color: #10208C;
49+
text-decoration: none;
50+
}
51+
52+
.meta {
53+
color: #888;
54+
font-size: 13px;
55+
}
56+
57+
.block {
58+
margin: 20px 0;
59+
}
60+
61+
.warning {
62+
background: #FEE;
63+
border: 1px solid #A00;
64+
}
65+
66+
.user {
67+
color: #10208C;
68+
margin-right: 4px;
69+
}
70+
71+
.comment {
72+
background: #EEE;
73+
margin: 5px 0;
74+
padding: 10px;
75+
}
76+
77+
.thread {
78+
border: 1px solid #CCC;
79+
margin: 0 0 20px 0;
80+
padding: 10px;
81+
}
82+
83+
.message_header {
84+
border-top: 1px solid #CCC;
85+
margin: 10px 0 0 0;
86+
padding: 10px 0 0 0;
87+
}
88+
89+
.message_header .meta {
90+
float: right;
91+
}
92+
93+
ul {
94+
list-style: none;
95+
padding: 0;
96+
}
97+
98+
th {
99+
font-weight: normal;
100+
padding: 5px 5px;
101+
text-align: left;
102+
vertical-align: top;
103+
width: 150px;
104+
}
105+
106+
td {
107+
padding: 5px 5px;
108+
}
109+
110+
.footer {
111+
clear: both;
112+
color: #888;
113+
font-size: 13px;
114+
margin-top: 10px;
115+
text-align: center;
116+
}
117+
118+
.warning {
119+
color: red;
120+
}
121+
</style>
122+
<title>**** ***** * ******* ****</title>
123+
</head>
124+
<body>
125+
<div class="nav">
126+
<img src="photos/profile.jpg"><ul>
127+
<li><a href="index.htm">*******</a></li>
128+
<li class="selected">******* ****</li>
129+
<li><a href="html/timeline.htm">********</a></li>
130+
<li><a href="html/photos.htm">******</a></li>
131+
<li><a href="html/videos.htm">******</a></li>
132+
<li><a href="html/friends.htm">*******</a></li>
133+
<li><a href="html/messages.htm">********</a></li>
134+
<li><a href="html/events.htm">******</a></li>
135+
<li><a href="html/security.htm">********</a></li>
136+
<li><a href="html/ads.htm">***</a></li>
137+
<li><a href="html/apps.htm">************</a></li>
138+
</ul>
139+
</div>
140+
<div class="contents">
141+
<h1>**** *****</h1>
142+
<table>
143+
<tr>
144+
<th style="width:200px"><b>****</b></th>
145+
<th><b>********</b></th>
146+
</tr>
147+
<tr>
148+
<td>*** *******</td>
149+
<td><span class="meta"><ul><li>******** ******************</li></ul></span></td>
150+
</tr>
151+
<tr>
152+
<td>***** ******</td>
153+
<td><span class="meta"><ul><li>******** *******************</li></ul></span></td>
154+
</tr>
155+
<tr>
156+
<td>***** ******</td>
157+
<td><span class="meta"><ul><li>******** ***************</li></ul></span></td>
158+
</tr>
159+
<tr>
160+
<td>****** *******</td>
161+
<td><span class="meta"><ul><li>******** ******************</li></ul></span></td>
162+
</tr>
163+
<tr>
164+
<td>****** *******</td>
165+
<td><span class="meta"><ul><li>******** ********************</li></ul></span></td>
166+
</tr>
167+
<tr>
168+
<td>**** *****</td>
169+
<td><span class="meta"><ul><li>******** ********************</li></ul></span></td>
170+
</tr>
171+
<tr>
172+
<td>**** ********</td>
173+
<td><span class="meta"><ul><li>******** *********************</li></ul></span></td>
174+
</tr>
175+
<tr>
176+
<td>***** *******</td>
177+
<td><span class="meta"><ul><li>******** ******************</li></ul></span></td>
178+
</tr>
179+
<tr>
180+
<td>******* ***</td>
181+
<td><span class="meta"><ul><li>******** ************</li></ul></span></td>
182+
</tr>
183+
<tr>
184+
<td>***** ***</td>
185+
<td><span class="meta"><ul><li>******** ****************</li></ul></span></td>
186+
</tr>
187+
</table>
188+
<div></div>
189+
</div>
190+
<div class="footer">********** ** **** ***** ** ******* ***** *** **** ** ******* ***</div>
191+
</body>
192+
</html>

0 commit comments

Comments
 (0)