Skip to content

Instantly share code, notes, and snippets.

@manton

manton/timetable_import.rb

Last active Nov 12, 2018
Embed
What would you like to do?
require "json"
require "cgi"
# JSON Feed file, folder of MP3s, and Micro.blog auth token
json_path = "/Users/manton/Dropbox/Documents/Timetable/Backup/timetable_posts.json"
episodes_path = "/Users/manton/Dropbox/Documents/Timetable/Episodes"
token = "..."
info = JSON.parse(IO.read(json_path))
episodes = info["items"]
for e in episodes
# get title, content, and date from JSON
title = e["title"]
s = e["content_html"]
d = e["date_published"]
# parse episode number from title in format: "Episode 123: Title"
episode_number = title.split(":").first.gsub("Episode ", "")
puts "Publishing episode: #{episode_number}..."
# upload MP3 to media endpoint
response = `curl https://micro.blog/micropub/media -i -F "file=@#{episodes_path}/Timetable_#{episode_number}.mp3" -H "Authorization: Bearer #{token}"`
headers = response.split("\n")
for h in headers
# published MP3 URL comes back in a Location header
name = h.split(": ").first
val = h.split(": ").last
if name == "Location"
# append audio tag to end of post text
s = s + "\n" + "<audio controls=\"controls\" src=\"#{val}\" />"
# publish the post
`curl https://micro.blog/micropub -d "name=#{CGI.escape(title)}" -d "content=#{CGI.escape(s)}" -d "published=#{d}" -H "Authorization: Bearer #{token}"`
break
end
end
puts "Done!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment