Un plugin para poder publicar en Medium determinados artículos al mismo tiempo que los publicaba en el blog.
module Medium
Jekyll::Hooks.register :posts, :post_render do |post|
# code to call after Jekyll renders a post
if File.exists? ".medium/posts.info"
File.open(".medium/posts.info", 'rb') {|f| @posts = Marshal::load(f)}
else
@posts = []
end
unless post['keywords'].nil?
if post['keywords'].kind_of?(Array) && post['keywords'].include?("Medium")
url = Jekyll.configuration({})['url']
permalink = url + post.url
post_posted = @posts.detect {|post| post["permalink"] == permalink }
if !(post_posted)
post = {"permalink" => permalink, "title" => post.data['title'], "content" => post.content, "response" => ""}
@posts << post
end
File.open(".medium/posts.info", 'wb') {|f| f.write(Marshal.dump(@posts)) }
end
end
end
end
Y en el fichero rakefile hay una tarea para automatizar la publicación en Medium:
medium_integration_token = "YOUR_MEDIUM_INTEGRATION_TOKEN"
medium_user_id = "YOUR_MEDIUM_USER_ID"
task :publish_on_medium do
if File.exists? ".medium/posts.info"
File.open(".medium/posts.info", 'rb') {|f| @posts = Marshal::load(f)}
else
@posts = []
end
@posts.each{ |post|
if post["response"] == ""
puts "Publicando en Medium: " + post["title"]
content = "<h1>#{post['title']}</h1>#{post['content']}"
uri = URI("https://api.medium.com/v1/users/#{medium_user_id}/posts")
Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
request = Net::HTTP::Post.new(uri, {"Authorization" => "Bearer #{medium_integration_token}"})
request.set_form_data('title' => post['title'], 'contentFormat' => 'html', 'content' => content, 'canonicalUrl' => post['permalink'], 'publishStatus' => 'public', 'notifyFollowers' => true)
response = http.request request # Net::HTTPResponse object
post['response'] = response.body
end
end
}
File.open(".medium/posts.info", 'wb') {|f| f.write(Marshal.dump(@posts)) }
end