Skip to content

Commit

Permalink
use user_name not but user_id
Browse files Browse the repository at this point in the history
  • Loading branch information
YuzuruS committed Feb 1, 2018
1 parent 749e1d9 commit 2b29f2b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 29 deletions.
24 changes: 2 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,13 @@ Or install it yourself as:

## Usage

1. Prepare your user_name and password for Instagram.
2. Prepare instagram user id to check list.

### How to find instagram user id

There are some methods finding instagram user id

- Use this site https://smashballoon.com/instagram-feed/find-instagram-user-id/
- execute Javascript on Profile page to check on Chrome dev console

```apple js
window._sharedData.entry_data.ProfilePage[0].user.id
```

![2018-01-30 23 12 23](https://user-images.githubusercontent.com/1485195/35570636-1f79042e-0613-11e8-92f5-a19412bf38c4.png)

#### code

```ruby
cli = InstagramUser.new(user_name: 'yuzuru_dev', password: 'PASSWORD')

# input instagram's user id
follows = cli.get_follows(7007201232)
follows = cli.get_follows('yuzuru_dev')
# => ["yudsuzuk", "instagram"]

# input instagram's user id
followers = cli.get_followers(7007201232)
followers = cli.get_followers('yuzuru_dev')
# => ["yudsuzuk"]
```

Expand Down
18 changes: 15 additions & 3 deletions lib/instagram_user/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Client

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
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_MAX_NUM_USERS = 3000
DEFAULT_REFERER = 'https://www.instagram.com/'.freeze
Expand All @@ -29,19 +30,30 @@ def initialize(options = {})
@referer = (ENV['INSTAGRAM_REFERER'] || options[:referer] || DEFAULT_REFERER)
@num_users = (ENV['INSTAGRAM_NUM_USERS'] || options[:num_users] || DEFAULT_MAX_NUM_USERS)
@session = Mechanize.new
@user_ids = {}
logined_session
end

def get_follows(user_id)
def get_follows(user_name)
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])
end

def get_followers(user_id)
def get_followers(user_name)
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])
end

private

def get_user_id(user_name)
url = USER_INFO_URL % [user_name]
page = @session.get(url)
json = JSON.parse(page.body)
@user_ids[user_name] = json["user"]["id"]
@user_ids[user_name]
end

def logined_session
@session.request_headers = login_http_headers
@session.post(LOGIN_URL, user_info)
Expand Down Expand Up @@ -89,7 +101,7 @@ def fetch_user_names(user_id, request_params, after = nil)
first: @num_users
}
variables[:after] = after unless after.nil?
url = format(BASE_URL, request_params[:query_hash], JSON.generate(variables))
url = BASE_URL % [request_params[:query_hash], JSON.generate(variables)]
@session.request_headers = username_http_headers
page = @session.get(url)
json = JSON.parse(page.body)
Expand Down
7 changes: 3 additions & 4 deletions spec/instagram_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
before do
@user_name = 'yuzuru_dev'
@password = 'gkK9.izwZGBysV82uowpE+JqYzztgVA9'
@user_id = '7007201232'
@cli = InstagramUser.new(
user_name: @user_name,
password: @password
Expand All @@ -17,17 +16,17 @@
end

it "get follows" do
follows = @cli.get_follows(@user_id)
follows = @cli.get_follows(@user_name)
expect(follows.include?('instagram')).to eq true
end

it "get followers 1" do
follows = @cli.get_followers(@user_id)
follows = @cli.get_followers(@user_name)
expect(follows.include?('instagram')).to eq false
end

it "get followers 2" do
follows = @cli.get_followers(@user_id)
follows = @cli.get_followers(@user_name)
expect(follows.include?('yudsuzuk')).to eq true
end
end
Expand Down

0 comments on commit 2b29f2b

Please sign in to comment.