Method: FbRuby::Comments#initialize

Defined in:
lib/FbRuby/comments.rb

#initialize(nokogiriObj:, request_session:) ⇒ Comments

Inisialisasi Object Comments

Parameters:

  • nokogiriObj (Object)

    Object dari tag Nokogiri

  • request_session (Session)

    Object Session



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/FbRuby/comments.rb', line 16

def initialize(nokogiriObj:, request_session:)
  @html = nokogiriObj
  @url = URI("https://mbasic.facebook.com/")
  @sessions = request_session
  @author = @html.at_css("h3 a[href*='profile.php?id='], h3 a")
  @username = nil
  @comment_text = @html.at_css("h3")
  @user_tag = []
  @media = {}
  @time = @html.at_css("abbr")
  @time = @time.text unless @time.nil?

  unless @author.nil?
    usr = @author['href'].match(/^\/(([a-zA-Z0-9_.-]+)\?eav|profile\.php\?id=(\d+))/)
    @username = (@author['href'].include?('profile.php') ? usr[3] : usr[2]) unless usr.nil?
    @author = @author.text
  end

  unless @comment_text.nil?
    @comment_text = @comment_text.next_element
    for com in @comment_text.css('a')
      next unless com['class'].nil?
      u = com['href'].match(/^\/(([a-zA-Z0-9_.-]+)\?eav|profile\.php\?id=(\d+))/)
      u = com['href'].include?('profile.php') ? u[3] : u[2]
      @user_tag << {"name"=>com.text,"username"=>u}
    end
    @comment_text.css("br").each{|i| i.replace("\n")}
    @comment_text = @comment_text.text
  end

  @video = @html.at_css("a[href^='/video_redirect']")
  @image = @html.at_css("a[href^='/photo.php']")

  unless @video.nil?
    vidData = {"link"=>nil,"id"=>nil,"preview"=>nil,"content-type"=>"video"}
    vidUrl = URI.decode_www_form_component(@video['href']).match(/src=(.*)/)
    preview = @video.at_css('img')

    unless vidUrl.nil?
      vidData['link'] = vidUrl[1]
      vidData['id'] = vidUrl[1].match(/&id=(\d+)/)[1]
    end
    
    vidData['preview'] = preview['src'] unless preview.nil?
    @media.update(vidData)
  end

  unless @image.nil?
    imgData = {"link"=>nil,"id"=>nil,"preview"=>nil,"content-type"=>"image"}
    imgUrl = URI.join(@url, @image['href'])
    thubmnail = @image.at_css('img')
    fullPhoto = @sessions.get(imgUrl).parse_html.at_css("img[src^='https://scontent'], img[src^='https://z-m-scontent']")
    imgData["link"] = fullPhoto['src'] unless fullPhoto.nil?
    imgData["id"] = fullPhoto['src'].match(/(\d+_\d+_\d+)/)[1] unless fullPhoto.nil?
    imgData["preview"] = thubmnail['src'] unless thubmnail.nil?
    @media.update(imgData)
  end
end