This repository has been archived by the owner on Jul 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
instagram_user.rb
62 lines (54 loc) · 1.54 KB
/
instagram_user.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env ruby
require 'net/http'
# Track Instagram User Data
#
# This job will track public info about public instagram profiles scraping
# the public available user page.
#
# Variables:
# `instagram_user_photos` count of photos
# `instagram_user_following` users the account is following
# `instagram_user_followers` users following the user
# Config
# ------
# instagram user name
instagram_username = ENV['INSTAGRAM_USERNAME'] || 'nike'
SCHEDULER.every '10m', :first_in => 0 do |job|
http = Net::HTTP.new("instagram.com")
response = http.request(Net::HTTP::Get.new("/#{instagram_username}"))
if response.code != "200"
puts "instagram communication error (status-code: #{response.code})\n#{response.body}"
else
match = /"counts":{"media":(\d+),"followed_by":(\d+),"follows":(\d+)}/.match(response.body)
# collect all the info in a list
userInfo = [
{
label: 'Followers',
value: match[2].to_i
},
{
label: 'Following',
value: match[3].to_i
},
{
label: 'Photos',
value: match[1].to_i
}
]
# send the list
if defined?(send_event)
send_event('instagram_userinfo', {items: userInfo})
else
print userInfo
end
# send every list item as a single event
userInfo.each do |element|
varname = "instagram_user_" + element[:label].downcase
if defined?(send_event)
send_event(varname, current: element[:value])
else
print "#{varname}: #{element[:value]}\n"
end
end
end
end