Last active
December 21, 2015 05:49
-
-
Save komasaru/6260203 to your computer and use it in GitHub Desktop.
Ruby script to get a list of Facebook likes by Facebook API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
require 'net/https' | |
# 各種定数 | |
URL_BASE = "https://graph.facebook.com/{ユーザID or ユーザ名}" | |
APP_ID = "{Facebook アプリの APP-ID}" | |
APP_SECRET = "{Facebook アプリの APP-SECRET}" | |
LIMIT = 25 # 1回に取得する件数 | |
# [CLASS] Facebook API | |
class FbApiLikes | |
def initialize | |
# App Access Token 取得 | |
@token = get_token | |
end | |
# Facebook 情報取得 | |
def get_info | |
begin | |
cnt_all = 0 # 総件数 初期化 | |
# データ取得 | |
json = fetch_data | |
# データが存在しなければ終了 | |
return unless json["data"] | |
cnt_all += json["data"].size | |
# 取得データを画面表示 (名前、ID、日時(UTC)) | |
json["data"].each do |j| | |
puts "* #{j["name"]}" | |
puts "\t[ #{j["id"]}, #{j["created_time"]} ]" | |
end | |
# 次ページ URL 取得 | |
next_url = json["paging"]["next"] | |
# 次ページが存在する限り LOOP | |
while next_url | |
# データ取得 | |
json = fetch_data(next_url) | |
# データが存在しなければ終了 | |
cnt = json["data"].size | |
cnt_all += cnt | |
break if cnt == 0 | |
# 取得データを画面表示 (名前、ID、日時(UTC)) | |
json["data"].each do |j| | |
puts "* #{j["name"]}" | |
puts "\t[ #{j["id"]}, #{j["created_time"]} ]" | |
end | |
# 次ページ offset 取得 | |
next_url = json["paging"]["next"] | |
end | |
# 総件数表示 | |
puts "TOTAL: #{cnt_all} likes." | |
rescue => e | |
STDERR.puts "[ERROR][#{self.class.name}.get_info] #{e}" | |
exit 1 | |
end | |
end | |
private | |
# App Access Token 取得 | |
# ( ここでのアクセストークンは、 | |
# User に紐付いている User Access Token ではない。 | |
# Facebook アプリに紐付いていている App Access Token のこと。 ) | |
def get_token | |
begin | |
uri = URI.parse("https://graph.facebook.com/oauth/access_token") | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
res = http.request(Net::HTTP::Get.new( | |
"#{uri.request_uri}?client_id=#{APP_ID}&client_secret=#{APP_SECRET}&grant_type=client_credentials" | |
)) | |
return res.body.split("=")[1] | |
rescue => e | |
STDERR.puts "[ERROR][#{self.class.name}.get_token] #{e}" | |
exit 1 | |
end | |
end | |
# 情報取得実体 | |
def fetch_data(url = "") | |
begin | |
uri = URI.parse(URL_BASE) | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
if url == "" | |
res = http.request(Net::HTTP::Get.new( | |
"#{uri.request_uri}?access_token=#{@token}&limit=#{LIMIT}" | |
)) | |
else | |
res = http.request(Net::HTTP::Get.new(url)) | |
end | |
return JSON.parse(res.body) | |
rescue => e | |
STDERR.puts "[ERROR][#{self.class.name}.fetch_data] #{e}" | |
exit 1 | |
end | |
end | |
end | |
# 開始メッセージ | |
puts "#### Get Facebook Likes [ START ]" | |
# Facebook 「いいね」情報取得 | |
obj_main = FbApiLikes.new | |
obj_main.get_info | |
# 終了メッセージ | |
puts "#### Get Facebook Likes [ E N D ]" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment