Last active
November 12, 2018 14:59
-
-
Save manton/d840af1ea9048215de40985b22668cca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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