Skip to content

Instantly share code, notes, and snippets.

@puyo
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save puyo/10229522 to your computer and use it in GitHub Desktop.
Save puyo/10229522 to your computer and use it in GitHub Desktop.
Run a Facebook based popularity competition for ALP MPs
gem 'activesupport' # {}.to_query
gem 'nokogiri'
gem 'typhoeus'
require 'active_support/core_ext/object'
require 'json'
require 'nokogiri'
require 'pp'
require 'typhoeus'
token = ENV['FB_TOKEN'] || (raise 'Need to export FB_TOKEN=...')
mps_url = 'http://www.aph.gov.au/Senators_and_Members/Parliamentarian_Search_Results?expand=1&q=&mem=1&sen=1&par=1&gen=0&ps=100&st=1'
data = Nokogiri::HTML(Typhoeus.get(mps_url).body)
names = data.css('.title a').map{|x| x.text }
names = names.map{|name| name.gsub(/the|The|Hon|Mr|Ms|Dr|Senator|\b[A-Z]+\b|,/, '').strip }
hydra = Typhoeus::Hydra.hydra
results = {}
names.each do |name|
query = { q: name, type: 'page', access_token: token }.to_query
req = Typhoeus::Request.new('https://graph.facebook.com/search?' + query)
req.on_complete do |res|
data = JSON.parse(res.body)['data'].first
if data
req2 = Typhoeus::Request.new('https://graph.facebook.com/' + data['id'])
req2.on_complete do |res2|
page_data = JSON.parse(res2.body)
results[name] = page_data['likes'].to_i
end
hydra.queue req2
end
end
hydra.queue req
end
hydra.run
results.sort_by{|name, count| -count }.each do |name, count|
printf("%10d %30s\n", count, name)
end
__END__
As of 2014-04-09:
53740 Tanya Plibersek
35552 Bill Shorten
26634 Anthony Albanese
20514 Chris Hayes
14382 Wayne Swan
14220 Doug Cameron
13376 Mark Bishop
12517 Chris Bowen
9886 Ed Husic
8031 Terri Butler
6414 Kate Lundy
5803 Penny Wong
4270 Alannah MacTiernan
4185 Tony Burke
4163 Mark Butler
4163 Julie Collins
3555 Melissa Parke
3477 Amanda Rishworth
3286 Anna Burke
2972 Julie Owens
2891 Laurie Ferguson
2624 Joel Fitzgibbon
2623 Sam Dastyari
2527 Justine Elliot
2523 Michelle Rowland
2478 Michael Danby
2351 Jason Clare
2273 Louise Pratt
2225 Kate Ellis
2181 Matt Thistlethwaite
1918 Stephen Conroy
1910 Gai Brodtmann
1888 Gary Gray
1877 Tim Watts
1798 Lisa Chesters
1782 Sharon Claydon
1695 Deborah O'Neill
1657 Nova Peris
1553 Brendan O'Connor
1526 Shayne Neumann
1274 Jill Hall
1206 Jim Chalmers
1111 Don Farrell
1064 Bernie Ripoll
946 Graham Perrett
824 Anne McEwen
822 Pat Conroy
821 Kim Carr
816 Lisa Singh
815 Joanne Ryan
757 John Faulkner
746 Clare O'Neil
664 Jan McLucas
624 Richard Marles
580 Helen Polley
576 Rob Mitchell
561 John Hogg
560 Claire Moore
519 Maria Vamvakinou
346 Carol Brown
312 Sue Lines
285 Jenny Macklin
138 Tony Zappia
96 Stephen Jones
83 Joe Ludwig
53 Jacinta Collins
35 Catryna Bilyk
34 Sharon Bird
30 Ursula Stephens
30 Alan Griffin
29 Lin Thorp
26 Mark Furner
25 David Feeney
18 Glenn Sterle
14 Gavin Marshall
13 Carine King
12 Anne Urquhart
7 Warren Snowdon
3 Alex Gallacher
1 Mehmet Tillem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment