Module: DownloadUtils
- Included in:
- TedTalk::Converter
- Defined in:
- lib/ted_talk/download_utils.rb
Class Method Summary collapse
- .download_successful?(full_file_path, file_size) ⇒ Boolean
- .get_binary(url, without_cache = false) ⇒ Object
- .get_final_location(url) ⇒ Object
- .get_html(url, without_cache = false) ⇒ Object
- .get_json(url, without_cache = false) ⇒ Object
- .get_wav(video_filepath) ⇒ Object
Class Method Details
.download_successful?(full_file_path, file_size) ⇒ Boolean
110 111 112 |
# File 'lib/ted_talk/download_utils.rb', line 110 def download_successful?(full_file_path, file_size) File.exist?(full_file_path) && File.size(full_file_path) == file_size end |
.get_binary(url, without_cache = false) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ted_talk/download_utils.rb', line 62 def get_binary(url, without_cache = false) url = get_final_location(url) basename = File.basename(url) filepath = CACHE_DIR + "/" + basename return filepath if File.exists? filepath file = File.new(filepath, "wb") file_size = 0 uri = URI(url) puts "Downloading file: " + basename Net::HTTP.start(uri.host, uri.port) do |http| http.request_get(uri.request_uri) do |res| file_size = res.read_header["content-length"].to_i = ProgressBar.new(basename, file_size) .file_transfer_mode res.read_body do |segment| .inc(segment.size) file.write(segment) end end end file.close print "\n" download_successful?(filepath, file_size) ? filepath : false end |
.get_final_location(url) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/ted_talk/download_utils.rb', line 97 def get_final_location(url) begin Net::HTTP.get_response(URI(url)) do |res| location = res["location"] return url if location.nil? return get_final_location(location) end rescue => e puts "Not able to reach at the final location" return url end end |
.get_html(url, without_cache = false) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ted_talk/download_utils.rb', line 11 def get_html(url, without_cache = false) url = get_final_location(url) key = Digest::MD5.new.update(url).to_s html = "" if File.exists?(CACHE_DIR + "/" + key) and !without_cache html = File.read(CACHE_DIR + "/" + key) else begin uri = URI(url) res = Net::HTTP.get_response(uri) if res.is_a?(Net::HTTPSuccess) html = res.body else puts "HTML download error" exit end File.open(CACHE_DIR + "/" + key, "w") do |f| f.write html end rescue => e puts "Not able to download HTML" exit end end return html end |
.get_json(url, without_cache = false) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/ted_talk/download_utils.rb', line 38 def get_json(url, without_cache = false) url = get_final_location(url) key = Digest::MD5.new.update(url).to_s script = nil if File.exists?(CACHE_DIR + "/" + key) and !without_cache json_text = File.read(CACHE_DIR + "/" + key) script = JSON.parse(json_text) else begin uri = URI(url) res = Net::HTTP.get_response(uri) json_text = res.body script = JSON.parse(json_text) File.open(CACHE_DIR + "/" + key, "w") do |f| f.write JSON.pretty_generate script end rescue => e puts "Not able to download HTML" exit end end return script end |
.get_wav(video_filepath) ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/ted_talk/download_utils.rb', line 87 def get_wav(video_filepath) ffmpeg = UnixTools::check_command(FFMPEG) basename = File.basename(video_filepath, ".*") filepath = CACHE_DIR + "/" + basename + ".wav" return filepath if File.exists? filepath puts "Converting to audio: #{basename}.wav" `#{ffmpeg} -loglevel panic -i #{video_filepath} -ac 1 -vn -acodec pcm_s16le -ar 44100 #{filepath}` return filepath end |