2015-07-07 11:51:56 +00:00
|
|
|
namespace :feeds_module do
|
|
|
|
desc "Downloading feeds"
|
|
|
|
task :make_cache,[:url] => :environment do |task,args|
|
2020-04-05 14:36:09 +00:00
|
|
|
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)
|
2020-04-15 10:35:22 +00:00
|
|
|
res_net = Net::HTTP.start(uri.host, uri.port,:use_ssl => uri.scheme == 'https',open_timeout: 60,read_timeout: 60) do |http|
|
2020-04-05 14:36:09 +00:00
|
|
|
req = Net::HTTP::Get.new(uri)
|
2020-04-15 10:35:22 +00:00
|
|
|
http.request(req)
|
2020-04-05 14:36:09 +00:00
|
|
|
end
|
2020-04-15 10:35:22 +00:00
|
|
|
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|
|
2020-04-15 10:35:22 +00:00
|
|
|
res = res.force_encoding("utf-8") rescue ''
|
2020-04-05 14:36:09 +00:00
|
|
|
file.write(res)
|
|
|
|
end
|
|
|
|
site_feed.save
|
|
|
|
end
|
2015-07-07 11:51:56 +00:00
|
|
|
end
|
|
|
|
end
|