class InstaScraper::JSON::MediaCommentStream
Attributes
comments[R]
error[R]
last_comment_id[R]
max_comments[R]
per_page[R]
responses[R]
shortcode[R]
Public Class Methods
new(shortcode, options = {})
click to toggle source
# File lib/insta_scraper/json/media_comment_stream.rb, line 12 def initialize(shortcode, options = {})#, last_comment_id = nil, max_comments = nil, per_page = 20) @shortcode = shortcode @last_comment_id = options[:last_comment_id] || nil @per_page = options[:per_page] || 20 @max_comments = options[:max_comments] || nil @responses = {} @comments = {} end
Public Instance Methods
data()
click to toggle source
# File lib/insta_scraper/json/media_comment_stream.rb, line 21 def data Hashie::Mash.new({ comments: _data }) .extend(Hashie::Extensions::DeepFetch) .extend(Hashie::Extensions::DeepFind) end
Private Instance Methods
_data(current_last_comment_id = nil)
click to toggle source
# File lib/insta_scraper/json/media_comment_stream.rb, line 29 def _data(current_last_comment_id = nil) current_last_comment_id ||= (last_comment_id || default_last_comment_id) response = connection.post do |req| req.url '/query/' #req.headers['Content-Type'] = 'application/json' req.headers['content-type'] = 'application/x-www-form-urlencoded' req.headers['X-CSRFToken'] = csrf_token req.headers['Referer'] = "https://www.instagram.com/#{shortcode}/" req.headers['x-instagram-ajax'] = '1' req.headers['x-requested-with'] = 'XMLHttpRequest' req.body = "q=ig_shortcode(#{shortcode}){comments.before(#{current_last_comment_id},#{per_page}){count,nodes{id,created_at,text,user{id,profile_pic_url,username,follows{count},followed_by{count},biography,full_name,media{count},is_private,external_url,is_verified}},page_info}}" end current_comments = ::JSON.parse(response.body)['comments']['nodes'] @responses[current_last_comment_id] = response @comments[current_last_comment_id] = current_comments if current_comments.any? && !reached_max_comments? _data(current_comments.first['id']) end all_comments rescue => e warn e.inspect @error = e all_comments end
all_comments()
click to toggle source
# File lib/insta_scraper/json/media_comment_stream.rb, line 59 def all_comments comments.values.flatten end
connection()
click to toggle source
# File lib/insta_scraper/json/media_comment_stream.rb, line 69 def connection @connection ||= Faraday.new(:url => "https://www.instagram.com/") do |builder| builder.use :cookie_jar builder.adapter Faraday.default_adapter end end
connection_response()
click to toggle source
# File lib/insta_scraper/json/media_comment_stream.rb, line 76 def connection_response @connection_response ||= connection.get "/p/#{shortcode}/?__a=1" end
csrf_token()
click to toggle source
# File lib/insta_scraper/json/media_comment_stream.rb, line 80 def csrf_token connection_response.headers["set-cookie"].match(%r{csrftoken=(?<token>\w+);})['token'] end
default_last_comment_id()
click to toggle source
# File lib/insta_scraper/json/media_comment_stream.rb, line 84 def default_last_comment_id media = InstaScraper::JSON::Media.new(shortcode) media.response = connection_response media.data["graphql"]["shortcode_media"]["edge_media_to_comment"]["edges"].last["node"]["id"] end
reached_max_comments?()
click to toggle source
# File lib/insta_scraper/json/media_comment_stream.rb, line 63 def reached_max_comments? return false unless max_comments all_comments.count > max_comments end