Skip to content

Instantly share code, notes, and snippets.

@kl
Last active December 19, 2015 15:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kl/5976027 to your computer and use it in GitHub Desktop.
Save kl/5976027 to your computer and use it in GitHub Desktop.
Yotsubato TTS downloader
#encoding: utf-8
require 'fileutils'
require 'open-uri'
require 'sanitize'
#require 'pry'
class YotsubatoDownloader
API = ARGV[1] || "http://208.109.168.116/GetAudio1.ashx?speaker=302&content="
SAVE_FOLDER = "./tts/"
OUT_NAME = "import_me.txt"
SEPARATOR = "\t"
def initialize
@hash = Time.now.hash.abs.to_s
end
def download(file_path)
reader = NotesReader.new
items = reader.read_items(file_path)
make_out_file(items)
download_wavs(items)
end
private
def make_out_file(items)
File.open(OUT_NAME, "w") do |out_file|
items.each_with_index do |item, index|
trans = item[:english] + " [sound:#{@hash}_#{index}.wav]"
if item[:kanji]
line = item[:kanji] + SEPARATOR + item[:kana] + SEPARATOR + trans
else
line = item[:kana] + SEPARATOR + " " + SEPARATOR + trans
end
out_file.puts(line)
end
end
end
def download_wavs(items)
make_save_dir
items.each_with_index do |item, index|
wav = get_wav_data(item[:kana])
write_wav(wav, "#{@hash}_#{index}.wav")
puts "Downloading .wav #{index}"
end
end
def make_save_dir
FileUtils.mkdir(SAVE_FOLDER) unless File.directory?(SAVE_FOLDER)
end
def get_wav_data(japanese)
request = API + URI::encode(japanese + "。")
open(request).read
end
def write_wav(wav_data, filename)
File.open(SAVE_FOLDER + filename, "wb") { |f| f.write(wav_data) }
end
end
class NotesReader
def read_items(file_path)
IO.foreach(file_path).map do |line|
cleaned = Sanitize.clean(line)
fields = cleaned.split("\t")
reading = fields[0].lstrip.rstrip
rest = fields[1].lstrip.rstrip
if rest.include?(" ")
kana, english = rest.split(" ")
{kanji: reading, kana: kana, english: english}
else
{kanji: nil, kana: reading, english: rest}
end
end
end
end
##### SCRIPT START #####
if ARGV[0].nil?
puts "Usage: yotsubato.exe [path to exported file]"
exit
end
html_file = File.expand_path(ARGV[0])
if !File.exist?(html_file) || !File.file?(html_file)
puts "File #{html_file} does not seem to exist. Try again."
exit
end
begin
YotsubatoDownloader.new.download(html_file)
rescue Exception => e
if e.class != Interrupt
puts "Something went wrong!"
puts "Error is: #{e.inspect}"
end
puts "Aborting..."
exit
end
puts "Script finished successfully!"
puts "Exiting..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment