Class: FacebookDumper::FacebookFriendsDumper

Inherits:
Object
  • Object
show all
Defined in:
lib/facebook_dumper.rb

Instance Method Summary collapse

Constructor Details

#initializeFacebookFriendsDumper

Returns a new instance of FacebookFriendsDumper.



41
42
43
# File 'lib/facebook_dumper.rb', line 41

def initialize
  @friends = []
end

Instance Method Details

#extract_from_file(file) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/facebook_dumper.rb', line 183

def extract_from_file(file)
  charset = nil
  html = ""
  open(file) {|f|
    html = f.read
  }
  #open("t1.html", "w") {|f| f.print html }

  doc = Nokogiri::HTML.parse(html, nil, charset)
  #open("t2.html", "w") {|f| f.print doc.to_s }

  doc.search(:meta).map &:remove
  doc.search(:link).map &:remove
  doc.search(:style).map &:remove
  doc.search(:script).map &:remove
  doc.search(:svg).map &:remove
  doc.search(:div).each {|node|
    node.remove_attribute("class")
  }
  doc.search(:span).each {|node|
    node.remove_attribute("class")
  }
  doc.search(:i).each {|node|
    node.remove_attribute("class")
  }
  doc.search(:a).each {|node|
    node.remove_attribute("class")
  }
  doc.search(:label).each {|node|
    node.remove_attribute("class")
  }
  doc.search(:img).each {|node|
    node.remove_attribute("class")
  }
  doc.search(:input).each {|node|
    node.remove_attribute("class")
  }
  doc.search(:ul).each {|node|
    node.remove_attribute("class")
  }
  doc.search(:li).each {|node|
    node.remove_attribute("class")
  }
  #open("t3.html", "w") {|f| f.print doc.to_s }

  #$f = open("t4.html", "w")

  num = 0
  @friends = []
  doc.xpath('//div[@aria-label="友達"]').each {|node|
    gpa = node.parent.parent	# get grand parent
    person = parse_a_person(gpa)
    #$f.puts person.inspect
    #$f.puts "----------------------------------------------------------------------"
    @friends << person
    num += 1
  }
  #$f.puts num	# 4993
  #$f.close

  return @friends
end

#friends_listObject



255
256
257
258
259
260
261
# File 'lib/facebook_dumper.rb', line 255

def friends_list
  out = ""
  @friends.sort.each {|p|
    out << p.inspect + "\n"
  }
  return out
end

#friends_list_take1Object



246
247
248
249
250
251
252
253
# File 'lib/facebook_dumper.rb', line 246

def friends_list_take1
    ar << [p.url, p.name, p.num]
    if num < 0
      out << "#{p.url}	#{p.name}	-1\n"
    else
      out << "#{p.url}	#{p.name}\n"
    end
end

#parse_a_person(gpa) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/facebook_dumper.rb', line 148

def parse_a_person(gpa)
  person = Person.new
  gpa.search(:img).each {|img|
    person.imgsrc = img.attribute('src').value
    pa = img.parent
    if pa.name != "a"
      gpa.search(:span).each {|span|
        text = span.inner_text
        unless text =~ /^友達$/
          person.name = text
          person.url = "no_longer_on_Facebook"
        end
      }
      return person	# return here, since the user is no longer on Facebook.
    end
  }
  gpa.search(:a).each {|a|
    if a.attribute('tabindex').value == "0"
      url = a.attribute('href').value
      text = a.inner_text
      if url =~ /friends_mutual/
        if text =~ /共通の友達(.+)人/
          person.friends_mutual = $1.gsub(",", "").to_i
        end
      else
        person.url = url
        person.name = text
      end
      #f.puts [person.url, person.name]
    end
    #f.puts a.to_html
  }
  return person
end

#run(argv) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/facebook_dumper.rb', line 45

def run(argv)
  file = argv[0]
  p file
  extract_from_file file
  out = friends_list
  open("facebook-friends.txt", "w") {|f| f.print out}
end

#take1Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/facebook_dumper.rb', line 53

def take1
  doc.search(:img).each {|node|
    node.remove_attribute("alt")
    node.remove_attribute("role")
  }
  doc.search(:div).each {|node|
    node.remove_attribute("data-ft")
    node.remove_attribute("data-testid")
    node.remove_attribute("style")
  }
  doc.search(:noscript).map &:remove
  doc.search(:a).each {|node|
=begin
    node.remove_attribute("data-gt")
    node.remove_attribute("ajaxify")
    node.remove_attribute("rel")
    node.remove_attribute("role")
    node.remove_attribute("data-hover")
    node.remove_attribute("data-tooltip-uri")
    node.remove_attribute("tabindex")
    node.remove_attribute("aria-hidden")
    node.remove_attribute("data-profileid")
    node.remove_attribute("data-flloc")
    node.remove_attribute("data-unref")
    node.remove_attribute("data-floc")
=end
    node.remove_attribute("data-hovercard")
    node.remove_attribute("data-hovercard-prefer-more-content-show")
    node.remove_attribute("style")
    node.remove_attribute("aria-haspopup")
  }
  doc.search(:li).each {|node|
    node.remove_attribute("data-ft")
    node.remove_attribute("data-gt")
    node.remove_attribute("data-alert-id")
  }
  doc.search(:button).each {|node|
    node.remove_attribute("class")
    node.remove_attribute("data-flloc")
    node.remove_attribute("data-profileid")
    node.remove_attribute("type")
    node.remove_attribute("data-cancelref")
    node.remove_attribute("data-floc")
  }
  #open("t3.html", "w") {|f| f.print doc.to_s }
  open("t4.html", "w") {|f| f.print doc.to_xml(:indent => 2) }

  num = 0
  @friends = []
  doc.xpath('//li[@class="_698"]').each {|node|	#<li class="_698">
    node.search(:button).map &:remove
    node.search(:span).each {|n|
      n.remove_attribute("class")
      n.remove_attribute("aria-hidden")
    }

    person = Person.new
    node.search(:div).each {|n|
      if n.attribute('class').value == "uiProfileBlockContent"
        n.search(:a).each {|a|
          text = a.inner_text
          if text =~ /共通の友達(.+)人/
            person.mutual_friends_num = $1.gsub(",", "").to_i
          elsif text =~ /友達(.+)人/
            person.friends_num = $1.gsub(",", "").to_i
          elsif a.attribute('ajaxify') && a.attribute('ajaxify').value =~ /\/ajax\/friends\/inactive\/dialog\?id=(.+)/
            #<a href="https://www.facebook.com/etocom/friends#" rel="dialog" ajaxify="/ajax/friends/inactive/dialog?id=100008321654013" role="button">干場 隆志
            #ajaxify="/ajax/browser/dialog/mutual_friends/?uid=1036721103"
            uid = $1.to_i
            person.uid = uid
            person.url = "https://www.facebook.com/profile.php?id=#{uid}"
            person.name = a.inner_text.chomp
            person.friends_num = -1
          elsif a.attribute('data-gt')
            person.name = text.chomp
            url = a.attribute('href').value
            url.gsub!("?fref=profile_friend_list&hc_location=friends_tab", "")
            url.gsub!("&fref=profile_friend_list&hc_location=friends_tab", "")
            url.gsub!("?fref=pb&hc_location=friends_tab", "")
            url.gsub!("&fref=pb&hc_location=friends_tab", "")
            person.url = url
          else
            # ignore
          end
        }
        #friends << [friend_url, friend_name, friend_num, friend_mutual]
        @friends << person
      end
    }
    num += 1
  }
  #p num	#4995
  return @friends
end