Skip to content

Instantly share code, notes, and snippets.

@johnholdun
Created August 20, 2021 17:31
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 johnholdun/69700b4908d2a7dcfef15a1b6c18a008 to your computer and use it in GitHub Desktop.
Save johnholdun/69700b4908d2a7dcfef15a1b6c18a008 to your computer and use it in GitHub Desktop.
Get a list of your updates to literal.club. There's no guarantee this will continue to work over time!
require 'date'
require 'json'
require 'net/http'
require 'uri'
# This should be your username
USERNAME = 'johnholdun'.freeze
QUERY = <<-QUERY.freeze
query getBooks($username: String) {
profile(where: { handle: $username }) {
readingStates(first: 100, orderBy: { createdAt: desc }) {
createdAt
status
book {
title
authors {
name
}
}
}
}
}
QUERY
params = { query: QUERY, variables: { username: USERNAME }, operationName: 'getBooks' }
uri = URI.parse('https://literal.club/graphql')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri)
request.body = params.to_json
request['Content-Type'] = 'application/json'
response = http.request(request)
result = JSON.parse(response.body, symbolize_names: true)
events = result.dig(:data, :profile, :readingStates)
events.each do |event|
status =
case event[:status]
when 'DROPPED' then '🙅‍♂️'
when 'WANTS_TO_READ' then '🔜'
when 'IS_READING' then '👓'
when 'FINISHED' then '✅'
end
authors = event[:book][:authors].map { |a| a[:name] }.join(', ') + ','
title = "*#{event[:book][:title]}*."
date = Date.parse(event[:createdAt]).iso8601
line = [status, authors, title, date]
puts "- #{line.join(' ')}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment