scribe/src/clients/medium_client.cr
Edward Loveall cef1bc256d
Add unique ID to headings
The `name` field on the `paragraph` type contains a unique ID for the
paragraph. It's not guaranteed to be there, on images for example like
in the `fd8d091ab8ef` post, but it's there for everything else I can
find.

This enables deep linking. There's no way to get to the deep link other
than opening up the web console. I wanted to link every heading, but
you can actually have links in part of a heading so that's not tenable.
Maybe a "permalink" link next to every heading?
2023-03-25 11:20:14 -04:00

70 lines
1.6 KiB
Crystal

require "json"
class MediumClient
def self.post_data(post_id : String) : PostResponse::Root
client = HTTP::Client.new("medium.com", tls: true)
response = client.post("/_/graphql", headers: headers, body: body(post_id))
PostResponse::Root.from_json(response.body)
end
private def self.headers : HTTP::Headers
HTTP::Headers{
"Accept" => "application/json",
"Content-Type" => "application/json; charset=utf-8",
}
end
private def self.body(post_id : String) : String
query = <<-GRAPHQL
query {
post(id: "#{post_id}") {
title
createdAt
creator {
id
name
}
content {
bodyModel {
paragraphs {
name
text
type
href
layout
markups {
title
type
href
userId
start
end
anchorType
}
iframe {
mediaResource {
href
iframeSrc
iframeWidth
iframeHeight
}
}
metadata {
id
originalWidth
originalHeight
}
}
}
}
}
}
GRAPHQL
JSON.build do |json|
json.object do
json.field "query", query
json.field "variables", {} of String => String
end
end
end
end