Sha256: 3464051cf59c5d33678b3ee51e9d9871923dce442d518d3ad7e2b3bc1f0daf12

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 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)

      case resp.code
        when 200
          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
        when 404
          # the client does not exist
          raise ArgumentError.new("#{@username} does not exist!")
      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.3 lib/ig_scrape/client.rb