Created
February 21, 2015 04:24
-
-
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
This file contains hidden or 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 '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