namespace :feeds_module do desc "Downloading feeds" task :make_cache,[:url] => :environment do |task,args| main_directory = File.join("#{Rails.root}","public","site_feeds") FileUtils.mkdir_p(main_directory) if !File.exists?(main_directory) SiteFeed.enabled.each do |site_feed| feed_directory = File.join(main_directory.to_s, site_feed.id.to_s) FileUtils.mkdir_p(feed_directory) if !File.exists?(feed_directory) uri = URI(site_feed.feed_url) res_net = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https', open_timeout: 60,read_timeout: 60, verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http| req = Net::HTTP::Get.new(uri) http.request(req) end if (res_net.code == '301' rescue true) begin uri = URI(res_net['location']) res_net = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https', open_timeout: 60,read_timeout: 60, verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http| req = Net::HTTP::Get.new(uri) http.request(req) end rescue => e res_net = '' puts e puts e.backtrace end end res = res_net.body rescue '' File.open(File.join(feed_directory.to_s,site_feed.feed_uid + ".json"),"w") do |file| res = res.force_encoding("utf-8") rescue '' file.write(res) end site_feed.save end end end