Sha256: 30aaa857084e12bdad674916d72a522bfd0c9fc7eaf56ce8069c02e8198f29de

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

require 'httparty'

class IGScrape::Client

  attr_accessor :username, :full_name, :follower_count, :follows_count, :id ,:post_count, :profile_pic_url, :posts

  def initialize(username)
    @username = username
    @posts = []
    load_profile
  end

  def load
    while has_more_posts? do
      load_more_posts
    end
  end

  def has_more_posts?
    @posts.length < @post_count
  end

  def loaded_post_count
    @posts.length
  end

  private

    def load_profile
      url = "https://www.instagram.com/#{@username}/?__a=1"
      resp = HTTParty.get(url)

      response = JSON.parse(resp.body)
      user = response["user"]
      @full_name = user["full_name"]
      @follower_count = user["followed_by"]["count"]
      @follows_count = user["follows"]["count"]
      @id = user["id"]
      @post_count = user["media"]["count"]
      @page_info = user["media"]["page_info"]
      @profile_pic_url = user["profile_pic_url"]

      media = user["media"]["nodes"]
      if media
        @posts = media.collect do |node|
          IGScrape::Post.new(node)
        end
      end
    end

    def load_more_posts
      cursor = @page_info["end_cursor"]

      variables = URI.encode_www_form_component("{\"id\":\"#{@id}\",\"first\":12,\"after\":\"#{cursor}\"}")
      url = "https://www.instagram.com/graphql/query/?query_id=17888483320059182&variables=#{variables}"

      resp = HTTParty.get(url)
      response = JSON.parse(resp.body)
      timeline = response["data"]["user"]["edge_owner_to_timeline_media"]
      @page_info = timeline["page_info"]
      new_posts = timeline["edges"].collect do |edge|
        IGScrape::Post.new(IGScrape::Post.edge_timeline_to_payload(edge["node"]))
      end

      @posts = @posts.concat(new_posts)
    end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ig_scrape-0.0.2 lib/ig_scrape/client.rb