class SiteFeed include Mongoid::Document include Mongoid::Timestamps field :remote_site_url field :merge_with_category field :channel_name field :channel_title, :localize => true field :channel_key field :feed_name, localize: true field :disabled, type: Boolean, default: false field :feed_url field :feed_uid require 'feed_model/cache' require 'fileutils' include FeedModel::Cache Category.send(:include,FeedModel::Cache) after_create do crontab_list = `crontab -l`.split("\n") rescue [] site_root = Rails.root.to_s if crontab_list.select{|s| s.include?(site_root) && s.include?("feeds_module:make_cache")} == [] `(crontab -l ; echo "*/5 * * * * /bin/bash -l -c 'cd #{site_root} && bundle exec rake feeds_module:make_cache > /dev/null'") | crontab` end end before_destroy do tmp = SiteFeedAnnc.where(feed_id: self.id).first main_directory = File.join("#{Rails.root}","public","site_feeds") feed_directory = File.join(main_directory.to_s, self.id.to_s) FileUtils.rm_rf(feed_directory.to_s) if !tmp.nil? tmp.destroy end end scope :enabled, ->{where(:disabled => false)} def get_annc(force_refresh=false) main_directory = File.join("#{Rails.root}","public","site_feeds") feed_directory = File.join(main_directory.to_s, self.id.to_s) if !force_refresh && File.exists?(feed_directory) anns = JSON.parse(File.read(File.join(feed_directory.to_s, self.feed_uid + ".json")))[self.channel_key.pluralize] rescue [] else uri = URI(self.feed_url) res = get_response_body(uri) rescue '' FileUtils.mkdir_p(feed_directory) if !File.exists?(feed_directory) File.open(File.join(feed_directory.to_s,self.feed_uid + ".json"),"w") do |file| res.force_encoding("utf-8") file.write(res) end anns = JSON.parse(res)[self.channel_key.pluralize] rescue [] end anns end def get_response_body(uri) res = 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.code == "302" || res.code == "301" location = res['Location'] cookie = res['Set-Cookie'] if location[0] == "/" uri = URI.parse("#{uri.scheme}://#{uri.host}#{location}") else uri = URI.parse(location) end res = 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) req['Cookie'] = cookie http.request(req) end return res.body else return res.body end end def category Category.find(self.merge_with_category) rescue nil end def channel_title_for_cache !self[:channel_title].to_s.empty? ? self[:channel_title] : I18n.t("feed.source") end end