chimps_fb_analyzer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 39914c3c2bde7dbf0b5a4af75280feefc97a76be
4
+ data.tar.gz: 9e3dc8a87720b0413f2738c2e0734919fa1fa841
5
+ SHA512:
6
+ metadata.gz: 5e5e91ab5aaecb780765018b75a57898a9c924e7e874386b7de426f24334c4e07a4cb1e6296a1c8be5badd4709714110a70197b564c954ec9bfeaf77a2a1944a
7
+ data.tar.gz: 60adef8ec54829aecc08caf0cf2a02cd24e1c586f463dfcc3d13ee471ab06efebdb917d399f37f5483802b858fbcfe8e47118aa6db59d831d4beb3b3be536af8
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ support_files/
19
+ .ruby-*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in chimps_fb_analyzer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Michael Villena
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # ChimpsFbAnalyzer
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'chimps_fb_analyzer'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install chimps_fb_analyzer
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.test_files = FileList['test/*test.rb']
7
+ end
8
+
9
+ desc "Run tests"
10
+ task :test
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+ require 'chimps_fb_analyzer'
3
+
4
+ grab_output = false
5
+ path = ""
6
+ options = {}
7
+ ARGV.each do |arg|
8
+ if grab_output
9
+ grab_output = false
10
+ options[:output_path] = arg
11
+ else
12
+ case arg
13
+ when "-o"
14
+ grab_output = true
15
+ else
16
+ path = arg
17
+ end
18
+ end
19
+ end
20
+
21
+ fb_anaylzer = ChimpsFBAnalyzer::FBAnalyzer.new(path)
22
+ data = fb_anaylzer.analyze_wall
23
+ fb_anaylzer.write_data_to_csv(data, options[:output_path])
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'chimps_fb_analyzer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "chimps_fb_analyzer"
8
+ spec.version = ChimpsFBAnalyzer::VERSION
9
+ spec.authors = ["Michael Villena"]
10
+ spec.email = ["mvillena@cmu.edu"]
11
+ spec.description = %q{Analyze facebook pages}
12
+ spec.summary = %q{Analyze facebook pages}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_runtime_dependency "nokogiri", "~> 1.6.0"
25
+ end
@@ -0,0 +1,154 @@
1
+ require "chimps_fb_analyzer/version"
2
+
3
+ require 'nokogiri'
4
+ require 'date'
5
+ require 'pathname'
6
+ require 'csv'
7
+
8
+ module ChimpsFBAnalyzer
9
+ class FBAnalyzer
10
+ attr_reader :wall_path
11
+
12
+ FB_STATS = [
13
+ "date",
14
+ "shared",
15
+ "updated status",
16
+ "likes a page",
17
+ "is now friends",
18
+ "changed profile pic",
19
+ "creates event",
20
+ "went to a place",
21
+ "going to a place",
22
+ "adds a new photo",
23
+ "was tagged in a friend's status",
24
+ "was tagged in a photo",
25
+ "friend writes on timeline",
26
+ "joined a group",
27
+ "using Facebook in another language"
28
+ ]
29
+
30
+ ARRAY_LENGTH = FB_STATS.count
31
+
32
+ def initialize(directory_path, options={})
33
+ @directory_pathname = Pathname.new(directory_path)
34
+ @output_file = options[:output_path]
35
+ @wall_path = @directory_pathname.join("html", "wall.htm")
36
+ @data = []
37
+ @get_status_update = false
38
+ end
39
+
40
+ def analyze_wall
41
+ f = @wall_path.open
42
+ html = Nokogiri::HTML(f)
43
+ f.close
44
+ @name = html.css('html body div.contents h1').first.text
45
+
46
+ at_date = Date.new
47
+ html_data = html.css('html body div.contents').children
48
+
49
+ @data = [FB_STATS]
50
+
51
+ current_data = Array.new(ARRAY_LENGTH, 0)
52
+
53
+ while !html_data.empty?
54
+ html_node = html_data.shift
55
+
56
+ if valid_date?(html_node.text)
57
+ new_date = Date.parse(html_node.text)
58
+
59
+ if at_date != new_date
60
+
61
+ if at_date.to_s == "-4712-01-01"
62
+ at_date = new_date
63
+ else
64
+ @data.push(current_data)
65
+
66
+ at_date = new_date
67
+ current_data = Array.new(ARRAY_LENGTH, 0)
68
+ index = FB_STATS.index('date')
69
+ current_data[index] = at_date.to_s
70
+ end
71
+ end
72
+ elsif @get_status_update
73
+ @get_status_update = false
74
+ index = FB_STATS.index("updated status")
75
+
76
+ if current_data[index] == 0
77
+ current_data[index] = html_node.text
78
+ else
79
+ current_data[index] = ("|" + html_node.text)
80
+ end
81
+ else
82
+ current_data = scan_html_node(html_node, current_data, @name)
83
+ end
84
+ end
85
+
86
+ @data
87
+ end
88
+
89
+ def scan_html_node(html_node, current_data, name)
90
+ case html_node.text
91
+ when /#{name} updated (his|her) status/
92
+ @get_status_update = true
93
+ when /#{name} shared a/
94
+ index = FB_STATS.index("shared")
95
+ current_data[index] += 1
96
+ when /#{name} likes/
97
+ index = FB_STATS.index("likes a page")
98
+ current_data[index] += 1
99
+ when /#{name} is going to/
100
+ index = FB_STATS.index("going to a place")
101
+ current_data[index] += 1
102
+ when /#{name} went to/
103
+ index = FB_STATS.index("went to a place")
104
+ current_data[index] += 1
105
+ when /#{name} joined/
106
+ index = FB_STATS.index("joined a group")
107
+ current_data[index] += 1
108
+ when /#{name} was tagged in .+ photo/ # TODO Finish
109
+ index = FB_STATS.index("was tagged in a photo")
110
+ current_data[index] += 1
111
+ when /#{name} and .+ are now friends/
112
+ index = FB_STATS.index("is now friends")
113
+ current_data[index] += 1
114
+ when /#{name} changed (his|her) profile picture/
115
+ index = FB_STATS.index("changed profile pic")
116
+ current_data[index] += 1
117
+ when /#{name} created/
118
+ index = FB_STATS.index("creates event")
119
+ current_data[index] += 1
120
+ when /#{name} added a new photo to the album/
121
+ index = FB_STATS.index("adds a new photo")
122
+ current_data[index] += 1
123
+ when /wrote on your timeline/
124
+ index = FB_STATS.index("friend writes on timeline")
125
+ current_data[index] += 1
126
+ when /#{name} is now using Facebook in/
127
+ index = FB_STATS.index("using Facebook in another language")
128
+ current_data[index] += 1
129
+ end
130
+ current_data
131
+ end
132
+
133
+ def write_data_to_csv(data_array, output_path=nil)
134
+ filepath = ""
135
+
136
+ if output_path
137
+ filepath = Pathname.new(output_path)
138
+ else
139
+ filepath = File.join(Dir.pwd, "#{@name.split.join("_")}_stats.csv")
140
+ end
141
+
142
+ puts "Writing to #{filepath}"
143
+ CSV.open(filepath, 'w') do |csv|
144
+ data_array.each do |set|
145
+ csv << set
146
+ end
147
+ end
148
+ end
149
+
150
+ def valid_date?(date)
151
+ Date.parse(date) rescue false
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,3 @@
1
+ module ChimpsFBAnalyzer
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,174 @@
1
+ require 'test/unit'
2
+ require 'chimps_fb_analyzer'
3
+ require 'pathname'
4
+
5
+ class PlayScrapeTest < Test::Unit::TestCase
6
+ Node = Struct.new(:text)
7
+
8
+ def setup
9
+ file = File.join File.dirname(__FILE__), 'support_files', 'facebook-samschock-end'
10
+ @fb_analyzer = ChimpsFBAnalyzer::FBAnalyzer.new(file)
11
+ @name = "John Doe"
12
+ @array_length = ChimpsFBAnalyzer::FBAnalyzer::FB_STATS.count
13
+ end
14
+
15
+ def test_analyze_wall
16
+ @fb_analyzer.analyze_wall.inspect
17
+ end
18
+
19
+ def test_scan_html_node_for_sharing_a_link
20
+ text = "#{@name} shared a link"
21
+ node = Node.new(text)
22
+
23
+ current_data = Array.new(@array_length, 0)
24
+ 10.times do |n|
25
+ current_data = @fb_analyzer.scan_html_node(node, current_data, @name)
26
+ index = ChimpsFBAnalyzer::FBAnalyzer::FB_STATS.index("shared")
27
+
28
+ assert_equal(n + 1, current_data[index])
29
+ end
30
+ end
31
+
32
+ def test_scan_html_node_for_liking_a_page
33
+ text = "#{@name} likes Zelda"
34
+ node = Node.new(text)
35
+
36
+ current_data = Array.new(@array_length, 0)
37
+ 10.times do |n|
38
+ current_data = @fb_analyzer.scan_html_node(node, current_data, @name)
39
+ index = ChimpsFBAnalyzer::FBAnalyzer::FB_STATS.index("likes a page")
40
+
41
+ assert_equal(n + 1, current_data[index])
42
+ end
43
+ end
44
+
45
+ def test_scan_html_node_for_going_to_a_place
46
+ text = "#{@name} is going to Waffle House"
47
+ node = Node.new(text)
48
+
49
+ current_data = Array.new(@array_length, 0)
50
+ 10.times do |n|
51
+ current_data = @fb_analyzer.scan_html_node(node, current_data, @name)
52
+ index = ChimpsFBAnalyzer::FBAnalyzer::FB_STATS.index("going to a place")
53
+
54
+ assert_equal(n + 1, current_data[index])
55
+ end
56
+ end
57
+
58
+ def test_scan_html_node_for_went_to_a_place
59
+ text = "#{@name} went to to Waffle House"
60
+ node = Node.new(text)
61
+
62
+ current_data = Array.new(@array_length, 0)
63
+ 10.times do |n|
64
+ current_data = @fb_analyzer.scan_html_node(node, current_data, @name)
65
+ index = ChimpsFBAnalyzer::FBAnalyzer::FB_STATS.index("went to a place")
66
+
67
+ assert_equal(n + 1, current_data[index])
68
+ end
69
+ end
70
+
71
+ def test_scan_html_node_joined_a_group
72
+ text = "#{@name} joined Myspace"
73
+ node = Node.new(text)
74
+
75
+ current_data = Array.new(@array_length, 0)
76
+ 10.times do |n|
77
+ current_data = @fb_analyzer.scan_html_node(node, current_data, @name)
78
+ index = ChimpsFBAnalyzer::FBAnalyzer::FB_STATS.index("joined a group")
79
+
80
+ assert_equal(n + 1, current_data[index])
81
+ end
82
+ end
83
+
84
+ def test_scan_html_node_for_tagged_in_a_photo
85
+ text = "#{@name} was tagged in Jane Doe's photo"
86
+ node = Node.new(text)
87
+
88
+ current_data = Array.new(@array_length, 0)
89
+ 10.times do |n|
90
+ current_data = @fb_analyzer.scan_html_node(node, current_data, @name)
91
+ index = ChimpsFBAnalyzer::FBAnalyzer::FB_STATS.index("was tagged in a photo")
92
+
93
+ assert_equal(n + 1, current_data[index])
94
+ end
95
+ end
96
+
97
+ def test_scan_html_node_for_new_friends
98
+ text = "#{@name} and Jane Doe are now friends"
99
+ node = Node.new(text)
100
+
101
+ current_data = Array.new(@array_length, 0)
102
+ 10.times do |n|
103
+ current_data = @fb_analyzer.scan_html_node(node, current_data, @name)
104
+ index = ChimpsFBAnalyzer::FBAnalyzer::FB_STATS.index("is now friends")
105
+
106
+ assert_equal(n + 1, current_data[index])
107
+ end
108
+ end
109
+
110
+ def test_scan_html_node_for_change_profile_pic
111
+ array = []
112
+ array.push "#{@name} changed his profile picture"
113
+ array.push "#{@name} changed her profile picture"
114
+ node = Node.new(array.sample)
115
+
116
+ current_data = Array.new(@array_length, 0)
117
+ 10.times do |n|
118
+ node.text = array.sample
119
+ current_data = @fb_analyzer.scan_html_node(node, current_data, @name)
120
+ index = ChimpsFBAnalyzer::FBAnalyzer::FB_STATS.index("changed profile pic")
121
+
122
+ assert_equal(n + 1, current_data[index])
123
+ end
124
+ end
125
+
126
+ def test_scan_html_node_for_creating_an_event
127
+ text = "#{@name} created Facebook"
128
+ node = Node.new(text)
129
+
130
+ current_data = Array.new(@array_length, 0)
131
+ 10.times do |n|
132
+ current_data = @fb_analyzer.scan_html_node(node, current_data, @name)
133
+ index = ChimpsFBAnalyzer::FBAnalyzer::FB_STATS.index("creates event")
134
+
135
+ assert_equal(n + 1, current_data[index])
136
+ end
137
+ end
138
+
139
+ def test_scan_html_node_for_adding_a_new_photo
140
+ text = "#{@name} added a new photo to the album John"
141
+ node = Node.new(text)
142
+
143
+ current_data = Array.new(@array_length, 0)
144
+ 10.times do |n|
145
+ current_data = @fb_analyzer.scan_html_node(node, current_data, @name)
146
+ index = ChimpsFBAnalyzer::FBAnalyzer::FB_STATS.index("adds a new photo")
147
+
148
+ assert_equal(n + 1, current_data[index])
149
+ end
150
+ end
151
+
152
+ def test_scan_html_node_for_friend_writing_on_your_timeline
153
+ text = "#{@name} wrote on your timeline"
154
+ node = Node.new(text)
155
+
156
+ current_data = Array.new(@array_length, 0)
157
+ 10.times do |n|
158
+ current_data = @fb_analyzer.scan_html_node(node, current_data, @name)
159
+ index = ChimpsFBAnalyzer::FBAnalyzer::FB_STATS.index("friend writes on timeline")
160
+
161
+ assert_equal(n + 1, current_data[index])
162
+ end
163
+ end
164
+
165
+ def test_write_data_to_csv
166
+ data = @fb_analyzer.analyze_wall
167
+ path = File.join File.expand_path(File.dirname(__FILE__)), "support_files", "test.csv"
168
+ @fb_analyzer.write_data_to_csv(data, path)
169
+ file = Pathname.new path
170
+ assert(file.exist?)
171
+ FileUtils.rm path
172
+ end
173
+
174
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chimps_fb_analyzer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Villena
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: nokogiri
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 1.6.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.6.0
55
+ description: Analyze facebook pages
56
+ email:
57
+ - mvillena@cmu.edu
58
+ executables:
59
+ - chimps_fb_analyzer
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/chimps_fb_analyzer
69
+ - chimps_fb_analyzer.gemspec
70
+ - lib/chimps_fb_analyzer.rb
71
+ - lib/chimps_fb_analyzer/version.rb
72
+ - test/chimps_fb_analyzer_test.rb
73
+ homepage: ''
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.0.3
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Analyze facebook pages
97
+ test_files:
98
+ - test/chimps_fb_analyzer_test.rb