Module: Grabber

Included in:
Instabot
Defined in:
lib/instabot/grabber.rb

Instance Method Summary collapse

Instance Method Details

#handle_media_information_data(media_id) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/instabot/grabber.rb', line 26

def handle_media_information_data(media_id)
  puts '[+] '.cyan + "Trying to get media (#{media_id}) information"
  log("Trying to get media (#{media_id}) information", 'GRABBER')
  response = @agent.get("https://www.instagram.com/p/#{media_id}/?__a=1")
  data     = parse_response(response.body)

  set_media_information(data)

  unless @infinite_tags == true
    tags = @media_information[:text].encode('UTF-8', invalid: :replace, undef: :replace, replace: '?').split(/\W+/)
    id   = 0
    tags.each do |tag|
      if id < options[:add_tag_per_post]
        if tag == '_' || tag == '' || tag.nil? || tag.include?('?')
          tags.delete(tag)
        else
          id += 1
          Config.options.tags << tag
        end
      end
    end
    puts '[+] '.cyan + '[' + id.to_s.yellow + '] New tags added'
  end
end

#handle_user_information_data_by_user_id(user_id) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/instabot/grabber.rb', line 4

def handle_user_information_data_by_user_id(user_id)
  puts '[+] '.cyan + "Trying to get user (#{user_id}) information"
  log("Trying to get user (#{user_id}) information", 'GRABBER')
  user_page = "https://www.instagram.com/web/friendships/#{user_id}/follow/"
  response  = get_page(user_page)
  last_page = response.uri.to_s
  username  = last_page.split('/')[3]
  response  = @agent.get("https://instagram.com/#{username}/?__a=1")
  data      = parse_response(response.body)

  set_user_information(data)
end

#handle_user_information_data_by_user_name(username) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/instabot/grabber.rb', line 17

def handle_user_information_data_by_user_name(username)
  puts '[+] '.cyan + "Trying to get user (#{username}) information"
  log("Trying to get user (#{username}) information", 'GRABBER')
  response  = @agent.get("https://instagram.com/#{username}/?__a=1")
  data      = parse_response(response.body)

  set_user_information(data)
end

#search(tags = []) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/instabot/grabber.rb', line 85

def search(tags = [])
  used_tags = []
  tags.each do |tag|
    puts '[+] '.cyan + "Searching in hashtag [##{tag}]"
    log("Searching in hashtags [##{tag}]", 'GRABBER')
    url         = "https://www.instagram.com/explore/tags/#{tag}/?__a=1"
    response    = @agent.get(url)
    data        = parse_response(response.body)
    owners      = data.deep_find_all('owner')
    media_codes = data.deep_find_all('shortcode')
    log("didn't find anything by hashtag ##{tag}", 'GRABBER')
    next if owners.nil? || media_codes.nil?
    owners.map { |id| users << id['id'] if !id['id'].nil? }
    media_codes.map { |code| medias << code if !code.nil? }
    used_tags << tag
  end
  Config.options.tags = Config.options.tags - used_tags
  puts '[+] '.cyan + 'Total grabbed users [' + users.size.to_s.yellow + ']'
  puts '[+] '.cyan + 'Total grabbed medias [' + medias.size.to_s.yellow + ']'
  log("Total grabbed users(#{users.size}) & Total grabbed medias(#{medias.size})", 'GRABBER')
end

#set_media_information(data) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/instabot/grabber.rb', line 68

def set_media_information(data)
  @media_information = {
    id:                  data.deep_find('id'),
    full_name:           data.deep_find('full_name'),
    owner:               data.deep_find('owner')['username'],
    is_video:            data.deep_find('is_video'),
    comments_disabled:   data.deep_find('comments_disabled'),
    viewer_has_liked:    data.deep_find('viewer_has_liked'),
    has_blocked_viewer:  data.deep_find('has_blocked_viewer'),
    followed_by_viewer:  data.deep_find('followed_by_viewer'),
    is_private:          data.deep_find('is_private'),
    is_verified:         data.deep_find('is_verified'),
    requested_by_viewer: data.deep_find('requested_by_viewer'),
    text:                data.deep_find('text') 
  }.with_indifferent_access
end

#set_user_information(data) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/instabot/grabber.rb', line 51

def set_user_information(data)
  @user_information = {
    followers:   data.deep_find('followed_by')['count'],
    following:   data.deep_find('follows')['count'],
    follows_viewer: data.deep_find('follows_viewer'),
    followed_by_viewer: data.deep_find('followed_by_viewer'),
    requested_by_viewer: data.deep_find('requested_by_viewer'),
    is_private:  data.deep_find('is_private'),
    is_verified: data.deep_find('is_verified'),
    username:    data.deep_find('username'),
    media_count:       data.deep_find('media')['count'],
    medias:       data.deep_find('media')['nodes'],
    full_name:   data.deep_find('full_name'),
    id:          data.deep_find('id')
  }.with_indifferent_access
end