feeds/lib/tasks/feeds_tasks.rake

35 lines
1.3 KiB
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 = Net::HTTP.start(uri.host, uri.port,:use_ssl => uri.scheme == 'https',open_timeout: 60,read_timeout: 60) do |http|
req = Net::HTTP::Get.new(uri)
http.request(req)
end
if (res_net.code == '301' return 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) 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 ''
2020-04-07 14:08:52 +00:00
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
2015-07-07 11:51:56 +00:00
end
end