Class: InstagramUser::Client
- Inherits:
-
Object
- Object
- InstagramUser::Client
- Defined in:
- lib/instagram_user/client.rb
Constant Summary collapse
- BASE_URL =
'https://www.instagram.com/graphql/query/?query_hash=%s&variables=%s'.freeze
- LOGIN_URL =
'https://www.instagram.com/accounts/login/ajax/'.freeze
- USER_INFO_URL =
'https://www.instagram.com/%s/?__a=1'.freeze
- MEDIA_JSON_BY_TAG_URL =
'https://www.instagram.com/explore/tags/%s/?__a=1&max_id=%s'.freeze
- DEFAULT_USER_AGENT =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36'.freeze
- DEFAULT_REFERER =
'https://www.instagram.com/'.freeze
- USER_MAP =
{ follow: { query_hash: '58712303d941c6855d4e888c5f0cd22f', edge: 'edge_follow' }, follower: { query_hash: '37479f2b8209594dde7facb0d904896a', edge: 'edge_followed_by' } }.freeze
Instance Method Summary collapse
- #create_follow(user_name) ⇒ Object
- #delete_follow(user_name) ⇒ Object
- #get_followers(user_name, num_users = 3000) ⇒ Object
- #get_follows(user_name, num_users = 3000) ⇒ Object
- #get_medias_by_tag(tag_name, req_num = 1) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/instagram_user/client.rb', line 27 def initialize( = {}) @user_name = (ENV['INSTAGRAM_USER_NAME'] || [:user_name]) @password = (ENV['INSTAGRAM_PASSWORD'] || [:password]) @user_agent = (ENV['INSTAGRAM_USER_AGENT'] || [:user_agent] || DEFAULT_USER_AGENT) @referer = (ENV['INSTAGRAM_REFERER'] || [:referer] || DEFAULT_REFERER) @session = Mechanize.new @user_ids = {} return if @user_name.nil? || @password.nil? mechanize_login_setting selenium_login_setting unless [:selenium] == false end |
Instance Method Details
#create_follow(user_name) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/instagram_user/client.rb', line 50 def create_follow(user_name) color = get_follow_btn_color(user_name) return false if color == false || color != "rgba(255, 255, 255, 1)" @driver.find_element(:xpath, '//article//button').click sleep(2) color = get_follow_btn_color(user_name) (color == false || color == "rgba(255, 255, 255, 1)") ? false : true end |
#delete_follow(user_name) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/instagram_user/client.rb', line 62 def delete_follow(user_name) color = get_follow_btn_color(user_name) return false if color == false || color == "rgba(255, 255, 255, 1)" @driver.find_element(:xpath, '//article//button').click sleep(2) color = get_follow_btn_color(user_name) (color == false || color != "rgba(255, 255, 255, 1)") ? false : true end |
#get_followers(user_name, num_users = 3000) ⇒ Object
45 46 47 48 |
# File 'lib/instagram_user/client.rb', line 45 def get_followers(user_name, num_users = 3000) user_id = @user_ids[user_name].nil? ? get_user_id(user_name) : @user_ids[user_name] fetch_all_user_names(user_id, USER_MAP[:follower], num_users) end |
#get_follows(user_name, num_users = 3000) ⇒ Object
40 41 42 43 |
# File 'lib/instagram_user/client.rb', line 40 def get_follows(user_name, num_users = 3000) user_id = @user_ids[user_name].nil? ? get_user_id(user_name) : @user_ids[user_name] fetch_all_user_names(user_id, USER_MAP[:follow], num_users) end |
#get_medias_by_tag(tag_name, req_num = 1) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/instagram_user/client.rb', line 74 def get_medias_by_tag(tag_name, req_num = 1) max_id = nil = {"recent" => [], "popularity" => []} req_num.times do url = format MEDIA_JSON_BY_TAG_URL, tag_name, max_id page = @session.get(url) json = JSON.parse(page.body) = json["graphql"]["hashtag"] ["recent"] += ["edge_hashtag_to_media"]["edges"] ["popularity"] = ["edge_hashtag_to_top_posts"]["edges"] if max_id.nil? break unless ["edge_hashtag_to_media"]["page_info"]["has_next_page"] max_id = ["edge_hashtag_to_media"]["page_info"]["end_cursor"] end end |