Class: IGScrape::Post

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload) ⇒ Post

Returns a new instance of Post.



5
6
7
8
# File 'lib/ig_scrape/post.rb', line 5

def initialize payload
  @comments = []
  load_from_payload(payload)
end

Instance Attribute Details

#captionObject

Returns the value of attribute caption.



3
4
5
# File 'lib/ig_scrape/post.rb', line 3

def caption
  @caption
end

#codeObject

Returns the value of attribute code.



3
4
5
# File 'lib/ig_scrape/post.rb', line 3

def code
  @code
end

#comment_countObject

Returns the value of attribute comment_count.



3
4
5
# File 'lib/ig_scrape/post.rb', line 3

def comment_count
  @comment_count
end

#commentsObject

Returns the value of attribute comments.



3
4
5
# File 'lib/ig_scrape/post.rb', line 3

def comments
  @comments
end

#created_atObject

Returns the value of attribute created_at.



3
4
5
# File 'lib/ig_scrape/post.rb', line 3

def created_at
  @created_at
end

#display_srcObject

Returns the value of attribute display_src.



3
4
5
# File 'lib/ig_scrape/post.rb', line 3

def display_src
  @display_src
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/ig_scrape/post.rb', line 3

def id
  @id
end

#is_videoObject

Returns the value of attribute is_video.



3
4
5
# File 'lib/ig_scrape/post.rb', line 3

def is_video
  @is_video
end

#likesObject

Returns the value of attribute likes.



3
4
5
# File 'lib/ig_scrape/post.rb', line 3

def likes
  @likes
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/ig_scrape/post.rb', line 3

def type
  @type
end

Class Method Details

.edge_timeline_to_payload(node) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ig_scrape/post.rb', line 29

def self.edge_timeline_to_payload node
  {
    "id" => node["id"],
    "__typename" => node["__typename"],
    "is_video" => node["is_video"],
    "code" => node["shortcode"],
    "display_src" => node["display_url"],
    "caption" => (node["edge_media_to_caption"]["edges"].length > 0 ? node["edge_media_to_caption"]["edges"].first["node"]["text"] : ""),
    "date" => node["taken_at_timestamp"],
    "comments" => node["edge_media_to_comment"],
    "likes" => node["edge_media_preview_like"]
  }
end

.load_from_shortcode(code) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/ig_scrape/post.rb', line 10

def self.load_from_shortcode code
  url = "https://www.instagram.com/p/#{code}/?__a=1"
  resp = HTTParty.get(url)
  response = JSON.parse(resp.body)
  payload = response["graphql"]["shortcode_media"]

  post = IGScrape::Post.new(self.edge_timeline_to_payload(payload))
end

Instance Method Details

#has_more_comments?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/ig_scrape/post.rb', line 19

def has_more_comments?
  @comments.length < @comment_count
end

#load_commentsObject



23
24
25
26
27
# File 'lib/ig_scrape/post.rb', line 23

def load_comments
  while has_more_comments? do
    load_more_comments
  end
end