Skip to content

Instantly share code, notes, and snippets.

@flakd
Last active December 19, 2015 11:49
Show Gist options
  • Select an option

  • Save flakd/5950366 to your computer and use it in GitHub Desktop.

Select an option

Save flakd/5950366 to your computer and use it in GitHub Desktop.
Handling large requests/responses with Grackle
require 'grackle'
client = Grackle::Client.new(:auth=>{
:type =>:oauth,
:consumer_key =>'xxx',
:consumer_secret =>'xxx',
:token =>'xxx',
:token_secret =>'xxx'
})
# Input slicing #
# Twitter API only accepts up to 100 screen names for a single request.
# Input lists must be sliced to length 100, and the outputs combined.
screen_names = ['_UoW', 'aberdeenuni', 'AbertayUni', 'AberUni', 'AdmissionsUoM', 'angliaruskin', 'ArtsHotlist', 'astonuniversity', 'AUCBPress', 'BangorUni', 'BathSpaUni', 'BirkbeckNews', 'BoltonUni', 'bournemouthuni', 'BradfordUni', 'bristoluni', 'bruneluni', 'bucksnewuni', 'CaledonianNews', 'Cambridge_Uni', 'canterburyccuni', 'cardiffmet', 'cardiffuni', 'chiuni', 'CityUniLondon', 'conservatoiredd', 'covcampus', 'CSSDLondon', 'CumbriaUni', 'derbyuni', 'dmuleicester', 'durham_uni', 'edgehill', 'EdinburghNapier', 'GlasgowUni', 'glyndwruni', 'GoldsmithsUOL', 'gsofa', 'guildhallschool', 'HarperAdamsUC', 'HeriotWattUni', 'HeythropCollege', 'HuddersfieldUni', 'icrnews', 'imperialcollege', 'IOE_London', 'KeeleUniversity', 'kingscollegelon', 'LancasterUni', 'lborouniversity', 'leedsmet', 'LeedsMusic', 'LeedsTrinity', 'LIPALiverpool', 'LiverpoolHopeUK', 'livuninews', 'LJMU', 'LondonBSchool', 'londonmetuni', 'lsbu', 'lsenews', 'LSHTMpress', 'manmetuni', 'MyBCU', 'mysgul', 'newman_uni', 'newportuni', 'NewUniPress', 'openuniversity', 'oxford_brookes', 'plymuni', 'portsmouthuni', 'QMUL', 'QMUniversity', 'queensubelfast', 'ravensbourneuk', 'RCAevents', 'rcmlatest', 'rcstweets', 'rhbncalumni', 'RobertGordonUni', 'rosebruford', 'RoyalAcadMusic', 'RoyalAgCollege', 'RoyalVetCollege', 'SalfordUni', 'School_Pharmacy', 'sheffhallamuni', 'sheffielduni', 'SOASnewsroom', 'solentofficial', 'StaffsUni', 'StirUni', 'SussexUni', 'swanseamet', 'SwanseaUni', 'TeessideUni', 'ThinkUHI', 'trentuni', 'TrinityLaban', 'trinitystdavid', 'UCA_creative', 'ucfalmouth', 'uclan', 'uclnews', 'ucpmarjon', 'UCS_NEWS', 'uel_news', 'ulsteruni', 'Uni_of_Essex', 'unibirmingham', 'uniglam', 'unigreenwich', 'unikent', 'unilincoln', 'UniNorthants', 'UniOfBath', 'uniofbeds', 'uniofbrighton', 'UniOfBuckingham', 'uniofeastanglia', 'UniofEdinburgh', 'uniofexeter', 'uniofglos', 'UniofHerts', 'UniOfHull', 'uniofleicester', 'UniofNottingham', 'uniofoxford', 'uniofsurrey', 'uniofyork', 'unirdg_news', 'unisouthampton', 'unistrathclyde', 'universityleeds', 'univofstandrews', 'UniWestminster', 'UniWestScotland', 'uochester', 'uwebristol', 'warwickuni', 'westlondonguru', 'wlv_uni', 'worcester_uni', 'writtlecollege', 'Yorkstjohn', 'YourStMarys']
screen_names.each_slice(100) do |slice|
client.users.lookup?(:screen_name => slice.join(',')).each do |user|
puts user.marshal_dump.inspect
end
end
# Cursor chunking #
# Twitter API only returns maximum 5000 followers.
# Use cursors to return response sections
ids = []
response = Grackle::TwitterStruct.new
response.next_cursor = '-1'
begin
response = client.followers.ids? :screen_name => 'uniofoxford', :cursor => response.next_cursor
ids << response.ids
end while response.next_cursor > 0
puts ids.flatten.inspect
# Show remaining requests #
puts "Requests remaining: #{client.response.headers['x-ratelimit-remaining']}"
puts "Request reset at: #{Time.at client.response.headers['x-ratelimit-reset'].to_i}"
@flakd
Copy link
Author

flakd commented Jul 8, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment