Created
April 20, 2015 12:11
-
-
Save zetavg/15cefe2637a0eff8a3f8 to your computer and use it in GitHub Desktop.
從 FB 留言取得抽獎名單給 https://github.com/Neson/RaffleDraw 用
This file contains hidden or 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
| # Get all commenters from a Facebook post and save the list into a format that | |
| # can be used by https://github.com/Neson/RaffleDraw | |
| # | |
| # Usage: | |
| # $ ruby get_draw_list.rb <access_token> <post_id> [<end_time>] | |
| # | |
| # The result will be write into data.js | |
| # | |
| # Args: | |
| # access_token: (required) A valid access token used to get the infomation | |
| # post_id: (required) The post's ID | |
| # end_time: (optional) Set an end time to eliminate commenters that are out | |
| # over time, format as: '2015/3/12 19:30' | |
| # | |
| require 'net/http' | |
| require 'json' | |
| require 'time' | |
| access_token = ARGV[0] | |
| post_id = ARGV[1] | |
| end_time = ARGV[2] | |
| uri = URI("https://graph.facebook.com/v2.2/#{post_id}/comments?limit=500000&access_token=#{access_token}") | |
| body = Net::HTTP.get(uri) | |
| data = JSON.parse(body) | |
| end_time = (end_time && Time.parse(end_time)) || Time.now | |
| processed_data = data['data'] | |
| processed_data.reject! { |c| t = Time.parse(c['created_time']); t > end_time } | |
| commenters = processed_data.map { |d| d['from'] } | |
| commenters.reject! { |c| !c['category'].nil? } | |
| commenters.map! { |c| { fbid: c['id'], name: c['name'] } } | |
| commenters.uniq! | |
| target = File.new('data.js', 'w') | |
| target.write("var data = #{commenters.to_json} ;") | |
| target.close | |
| p 'ok' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment