Skip to content

Instantly share code, notes, and snippets.

@pocke

pocke/gh-sponsors.rb

Last active Dec 4, 2019
Embed
What would you like to do?
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
@pocke

This comment has been minimized.

Copy link
Owner Author

@pocke pocke commented Dec 4, 2019

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.

191204184857

$ GITHUB_ACCESS_TOEKN=<YOUR PERSONAL ACCESS TOKEN> ruby gh-sponsors.rb

# WIth rich format
$ RICH=1 GITHUB_ACCESS_TOEKN=<YOUR PERSONAL ACCESS TOKEN> ruby gh-sponsors.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment