Skip to content

Instantly share code, notes, and snippets.

@yar
Created August 9, 2013 11:10
Show Gist options
  • Save yar/6192861 to your computer and use it in GitHub Desktop.
Save yar/6192861 to your computer and use it in GitHub Desktop.
batch-renaming files like 254--08.00-08.30--Комиссия.mp4 to 20c6655a-4b0d-11e1-8a16-001999c6cab9/1330833600-1330835400.mp4
require 'json'
# batch-renaming files like
# 254--08.00-08.30--Комиссия.mp4
# to
# 20c6655a-4b0d-11e1-8a16-001999c6cab9/1330833600-1330835400.mp4
#
File.read("spec/sample_files/json_results.txt").lines.map do |line|
JSON.parse(line)
end.sort_by { |json| json["video_start"] }.each do |json|
json["channels"].each do |channel|
old_name = json["name"] + "--" + Time.at(json["video_start"]+json["utc_offset"]*60).utc.strftime("%H.%M")+"-"+Time.at(json["video_start"]+json["utc_offset"]*60+30*60).utc.strftime("%H.%M") + "--" + channel["view"] + ".mp4"
new_dir, new_name = channel["url"].gsub(/^.*video\/[^\/]+\/(.*\.mp4).*$/, '\\1').split("/")
puts "mkdir -p #{new_dir}; mv #{old_name} #{new_dir}/#{new_name}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment