Last active
December 4, 2019 10:15
-
-
Save pocke/27155765598bc912059f5b629f25664d to your computer and use it in GitHub Desktop.
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 'net/http' | |
require 'json' | |
class RequestError < StandardError | |
def initialize(errors) | |
@errors = errors | |
end | |
def message | |
@errors.map{|e| e[:message]}.join("\n") | |
end | |
end | |
def token | |
ENV['GITHUB_ACCESS_TOKEN'] | |
end | |
def req(query) | |
http = Net::HTTP.new('api.github.com', 443) | |
http.use_ssl = true | |
header = { | |
"Authorization" => "Bearer #{token}", | |
'Content-Type' => 'application/json', | |
} | |
resp = http.request_post('/graphql', JSON.generate(query), header) | |
JSON.parse(resp.body, symbolize_names: true).tap do |content| | |
raise RequestError.new(content[:errors]) if content[:errors] | |
end | |
end | |
def rich_format(users) | |
users = users.select{|n| n[:sponsorsListing]} | |
users.map do |user| | |
<<~SECTION | |
https://github.com/sponsors/#{user[:login]} | |
============== | |
#{user[:sponsorsListing][:tiers][:nodes].map{|tier| "#{tier[:name]}\n--------\n\n#{tier[:description]}"}.join("\n\n")} | |
SECTION | |
end.join("\n\n\n") | |
end | |
def simple_format(users) | |
users = users.select{|n| n[:sponsorsListing]} | |
users.map do |user| | |
tiers = "(#{user[:sponsorsListing][:tiers][:nodes].map{|t| "$#{t[:monthlyPriceInDollars]}"}.join(", ")})" | |
"* https://github.com/sponsors/#{user[:login]}\t\t#{tiers}" | |
end.join("\n") | |
end | |
def fetch_users(query:) | |
after = nil | |
has_next_page = true | |
users = [] | |
while has_next_page | |
resp = req( | |
query: query, | |
variables: { | |
after: after, | |
}, | |
) | |
following = resp[:data][:viewer][:following] | |
users.concat(following[:nodes]) | |
has_next_page = following[:pageInfo][:hasNextPage] | |
after = following[:pageInfo][:endCursor] | |
end | |
users | |
end | |
RICH_QUERY = <<~QUERY | |
query($after: String) { | |
viewer { | |
following(first: 100, after: $after) { | |
nodes { | |
login | |
sponsorsListing { | |
tiers(first: 10) { | |
nodes { | |
name | |
description | |
} | |
} | |
} | |
} | |
pageInfo { | |
endCursor | |
hasNextPage | |
} | |
} | |
} | |
} | |
QUERY | |
SIMPLE_QUERY = <<~QUERY | |
query($after: String) { | |
viewer { | |
following(first: 100, after: $after) { | |
nodes { | |
login | |
sponsorsListing { | |
tiers(first: 10) { | |
nodes { | |
monthlyPriceInDollars | |
} | |
} | |
} | |
} | |
pageInfo { | |
endCursor | |
hasNextPage | |
} | |
} | |
} | |
} | |
QUERY | |
if ENV['RICH'] | |
users = fetch_users(query: RICH_QUERY) | |
puts rich_format(users) | |
else | |
users = fetch_users(query: SIMPLE_QUERY) | |
puts simple_format(users) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Find users who enable GitHub Sponsors from your following.
Usage
Get your personal access token from https://github.com/settings/tokens.
It needs
user
scope.