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.



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

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

Instance Attribute Details

#captionObject

Returns the value of attribute caption.



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

def caption
  @caption
end

#codeObject

Returns the value of attribute code.



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

def code
  @code
end

#comment_countObject

Returns the value of attribute comment_count.



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

def comment_count
  @comment_count
end

#commentsObject

Returns the value of attribute comments.



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

def comments
  @comments
end

#created_atObject

Returns the value of attribute created_at.



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

def created_at
  @created_at
end

#display_srcObject

Returns the value of attribute display_src.



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

def display_src
  @display_src
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#is_videoObject

Returns the value of attribute is_video.



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

def is_video
  @is_video
end

#likesObject

Returns the value of attribute likes.



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

def likes
  @likes
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

.edge_timeline_to_payload(node) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ig_scrape/post.rb', line 36

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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ig_scrape/post.rb', line 11

def self.load_from_shortcode code
  url = "https://www.instagram.com/p/#{code}/?__a=1"
  resp = HTTParty.get(url)

  case resp.code
    when 200
      response = JSON.parse(resp.body)
      payload = response["graphql"]["shortcode_media"]

      post = IGScrape::Post.new(self.edge_timeline_to_payload(payload))
    when 404
      raise ArgumentError.new("Post with #{code} does not exist!")
  end
end

Instance Method Details

#has_more_comments?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/ig_scrape/post.rb', line 26

def has_more_comments?
  @comments.length < @comment_count
end

#load_commentsObject



30
31
32
33
34
# File 'lib/ig_scrape/post.rb', line 30

def load_comments
  while has_more_comments? do
    load_more_comments
  end
end

#to_json(*args) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ig_scrape/post.rb', line 50

def to_json(*args)
  JSON.generate({
    id: @id,
    code: @code,
    caption: @caption,
    type: @type,
    created_at: @created_at,
    comment_count: @comment_count,
    likes: @likes
  })
end