Skip to content

Instantly share code, notes, and snippets.

@hSATAC
Created December 21, 2015 18:02
Show Gist options
  • Save hSATAC/14728434748e755d773c to your computer and use it in GitHub Desktop.
Save hSATAC/14728434748e755d773c to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
def upload_and_generate_mobi(source_file, output_file, title)
puts "Dealing #{source_file} ..."
# get key
key = `curl -s http://ebook.cdict.info/mobi/ | grep progress_key`.match(/progress_key=([a-zA-Z0-9]+)/)[1]
`curl -s "http://ebook.cdict.info/mobi/revalid.php?progress_key=#{key}"`
`curl -s \
-F "APC_UPLOAD_PROGRESS=#{key}" \
-F "progress_key=#{key}" \
-F "title=#{title}" \
-F "author=" \
-F "font=hei" \
-F "country=default" \
-F "part=0" \
-F "contents=1" \
-F "transfer=USB" \
-F "txt_file=@#{source_file}" \
http://ebook.cdict.info/mobi/target.php`
`curl -s "http://ebook.cdict.info/mobi/getprogress.php?progress_key=#{key}"`
`curl -s "http://ebook.cdict.info/mobi/chkfile.php?progress_key=#{key}"`
`curl -s "http://ebook.cdict.info/mobi/recode_file.php?progress_key=#{key}&code=UTF-8"`
`curl -s "http://ebook.cdict.info/mobi/gen_mobi.php?progress_key=#{key}"`
puts "Downloading #{output_file} ..."
`wget "http://ebook.cdict.info/mobi/download.php?progress_key=#{key}" -O "#{output_file}" -q`
end
files = Dir.glob(ARGV[0]).select {|f|File.file? f}
abort "Please provide file name" if files.size == 0
files.each do |f|
basename = File.basename(f, '.txt')
upload_and_generate_mobi(f, "#{basename}.mobi", basename)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment