Skip to content

Instantly share code, notes, and snippets.

@philss
Created February 21, 2015 04:24
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 philss/511612d3e68056c0f504 to your computer and use it in GitHub Desktop.
Save philss/511612d3e68056c0f504 to your computer and use it in GitHub Desktop.
This script fetches the audio file length in bytes for each audio file of ZOFE podcast
require 'yaml'
Dir.glob("./_posts/*.md").each do |post_file_path|
post = YAML.load_file(post_file_path)
audio_url = post.fetch("audio")
# NOTE: cURL may return multiple lines with length because
# it follows the redirects of the server.
#
lengths = `curl -I -L #{audio_url} | grep "Content-Length"`
max_length =
lengths
.split("\n")
.map { |content_length_str| content_length_str.split("\s").last.to_i }
.max
file_content = File.read(post_file_path)
file_content.gsub!(/(audio\:.*\n)/) do |audio_str_with_url|
"#{audio_str_with_url}audio_file_length: #{max_length}\n"
end
File.write(post_file_path, file_content)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment