feeds/lib/tasks/feeds_tasks.rake

16 lines
642 B
Ruby
Raw Normal View History

2015-07-07 11:51:56 +00:00
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::HTTP.get(uri)
file = File.open(File.join(feed_directory.to_s,site_feed.feed_uid + ".json"),"w")
res.force_encoding("utf-8")
file.write(res)
end
end
end