-
-
Notifications
You must be signed in to change notification settings - Fork 108
Support Facebook API in Statistics::Aggregation #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@@ -1,4 +1,4 @@ | |||
class DojoEventService < ApplicationRecord | |||
belongs_to :dojo | |||
enum name: %i( connpass doorkeeper ) | |||
enum name: %i( connpass doorkeeper facebook ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
facebookサポートにより追加
👀 ✨ |
@@ -0,0 +1,9 @@ | |||
class IntegerToStringOnGroupIdInDojoEventServices < ActiveRecord::Migration[5.0] | |||
def up | |||
change_column :dojo_event_services, :group_id, :string, null: false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
facebookのgroup idが15桁なのでintegerからstringに変更した。
bigintにしておくという手もあるが、別のイベントサイトをサポートする際に数値である保証もないので今のうち(?)にstringに変更した。
class IntegerToStringOnServiceGroupIdAndEventIdInEventHistories < ActiveRecord::Migration[5.0] | ||
def up | ||
change_column :event_histories, :service_group_id, :string, null: false | ||
change_column :event_histories, :event_id, :string, null: false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
@@ -20,7 +22,7 @@ def run(dojos, date) | |||
|
|||
dojos.each do |dojo| | |||
cnps.fetch_events(params.merge(series_id: dojo.dojo_event_service.group_id)).each do |e| | |||
next unless e.dig('series', 'id') == dojo.dojo_event_service.group_id | |||
next unless e.dig('series', 'id').to_s == dojo.dojo_event_service.group_id |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
カラムの型変更に伴いto_s
するようにした
@@ -47,7 +49,7 @@ def run(dojos, date) | |||
|
|||
dojos.each do |dojo| | |||
drkp.fetch_events(params.merge(group_id: dojo.dojo_event_service.group_id)).each do |e| | |||
next unless e['group'] == dojo.dojo_event_service.group_id | |||
next unless e['group'].to_s == dojo.dojo_event_service.group_id |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
lib/statistics/client.rb
Outdated
|
||
class Facebook | ||
def initialize | ||
@client = Koala::Facebook::API.new(ENV.fetch('FACEBOOK_ACCESS_TOKEN')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Facebook Graph APIを使うためのaccess tokenは環境変数経由で取得する
namespace :oauth do | ||
desc 'Facebookのaccess tokenを取得します' | ||
task :facebook_access_token, [:app_id, :app_secret] => :environment do |_tasks, args| | ||
puts 'Access Token: ' + Koala::Facebook::OAuth.new(args[:app_id], args[:app_secret]).get_app_access_token |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Facebookのapp_idとapp_secretでaccess tokenを取得するRake task
Resolved conflicts 4f1b20d |
@himajin315 @nanophate お手隙の際にこちらのPRをレビューしてもらえると嬉しいです...!! 🙏 |
class Facebook | ||
class << self | ||
def run(dojos, date) | ||
fsbk = Client::Facebook.new |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fsbk
ってfacebookを省略した言葉なんですね🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ローカル変数なので雑に省略しています😅
lib/statistics/client.rb
Outdated
@@ -113,7 +113,7 @@ def fetch_events(group_id:, since_at: nil, until_at: nil) | |||
limit: 100, | |||
since: since_at, | |||
until: until_at | |||
}.compact! | |||
}.compact |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
valueがnilのものがない場合はparamsがnilになってしまうのでbangは不要
lib/statistics/client.rb
Outdated
@@ -119,7 +119,7 @@ def fetch_events(group_id:, since_at: nil, until_at: nil) | |||
|
|||
collection = @client.get_object("#{group_id}/events", params) | |||
events.push(*collection.to_a) | |||
while collection.paging['next'] do | |||
while !collection.empty? && collection.paging['next'] do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
collection.empty? == true
の場合、collection.paging
がnilなのでcollectionがemptyかどうかチェックしておく
@nalabjp PRありがとうございます! 😸 ちょっと細かな点ですが、Facebook API の Access Token Key は Travis CI や Heroku にも渡す必要があるかなという認識ですが、既に各環境の環境変数で渡している感じですかね? 🤔 (テストが通っているところを見ると、大丈夫そう?) あと Facebook の Access Token は確か短期と長期のトークンがあって、60日ごとに更新する必要があったのかなと思うのですが、トークンが Expire された場合って何かしら Rake タスクを手動で走らせる必要があるって認識で大丈夫ですかね? 💭 cf. アクセストークンの有効期限と延長 - Facebook for Developers |
はい、CIは設定済みですね。
はい、今のところその通りです。 有効期限が切れたケースにまだ遭遇できていないので未実装なのですが、 |
了解です! 🙆 補足ありがとうございます 😸
確かに。現在の利用方法で API の利用上限には到達しないと思うので、常に毎回取得・更新する方法が楽で良いかもしれないですね🤔 Document を見ても定期的に (1日1回ぐらい) 更新するような使い方が推奨されていそうです 👀
トークンを更新するRakeタスクについては、Heroku Scheduler で1日1回実行する形でも良いかなと考えているのですが、どうですかね? 🤔 (もし問題がありそうであれば指摘してもらえると嬉しいです 🙏 💦 ) |
そうしましょうか。 |
de9841c |
MTG MEMO w/ @nalabjp
これが終わったら Review & Merge ✅ |
@nalabjp Facebook の長期トークンが60日間なので、毎月1回でも大丈夫です! 🙆 ただせっかく Heroku の Hobby プランを使っていて、一日中 Dyno が動いているので、アクセスの少ない夜中とかに (APIの利用制限に引っかからない範囲で) 毎日タスクを流してイベント数が徐々に上がっている感じが見えるとインパクトあって良いかなと思った次第です 🤔💭 とはいえ CoderDojo は週末に行われることが多いので、毎週月曜日に実行する形でも、実質的にはほぼ同じ結果が得られるかなとは考えています ;) |
なるほど、毎週月曜は良さそうですね。 |
@nalabjp 他に特に懸念点などなければマージしようと思いますが、いかがでしょう...!! 🤔
あ、念のため確認ですが、とりあえずは「毎月1回 (各月の初日) 実行する」という認識で大丈夫ですかね? ✅ |
.oO(明日 DojoCon Japan 2017 があるので、もし集計が間に合えばそこに滑り込ませたいな的なことを考えています) 😅💭 |
はい、月一でも週一でもどちらでも大丈夫です! |
なるほど、ひとまずコンフリクトだけ直してしまうので少々お待ち下さい。 |
CI通ったのでマージしてしまいます! 💪 |
ありがとうございます! 😸 |
#12
Statisctics::AggregationにおいてFacebook APIをサポートしました。
これにより、Facebookイベントの開催履歴を集計することができます。
FacebookのGraph APIはkoala.gem経由で扱います。
dojo_event_servicesテーブルのgroup_idカラムで使用するFacebookのgroup idはlookup-id.comで検索します。
Facebookグループのtopページurlである https://www.facebook.com/[group_name] をlookup-idに入れるとgroup idが返却されるので、db/dojo_event_services.yaml経由で登録しておきます。
(Graph APIで提供されているのかもしれないけど、ちょっと見つけられなかった😢)
TODO
dojo_event_services
table #140 を最新に更新してから先にマージした方が良さそう(それまでWIP)