let rake can redirect to the url when response is 301

This commit is contained in:
chiu 2020-04-15 18:35:22 +08:00
parent 8e7fe3ffe7
commit 9b4eec8a71
1 changed files with 17 additions and 3 deletions

View File

@ -7,12 +7,26 @@ namespace :feeds_module do
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.start(uri.host, uri.port,:use_ssl => uri.scheme == 'https',open_timeout: 60,read_timeout: 60) do |http|
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).body rescue ''
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 ''
File.open(File.join(feed_directory.to_s,site_feed.feed_uid + ".json"),"w") do |file|
res.force_encoding("utf-8")
res = res.force_encoding("utf-8") rescue ''
file.write(res)
end
site_feed.save