2015-07-07 11:51:56 +00:00
|
|
|
class SiteFeed
|
|
|
|
include Mongoid::Document
|
|
|
|
include Mongoid::Timestamps
|
2022-05-22 10:36:26 +00:00
|
|
|
ModuleAppSyncFields = ["feeds_model", "feeds_uid_field", "feeds_update_callback", "feeds_time_field", "feeds_finish_callback", "feeds_update_statuses_callback"]
|
2015-07-07 11:51:56 +00:00
|
|
|
field :remote_site_url
|
|
|
|
field :merge_with_category
|
|
|
|
field :channel_name
|
2015-11-09 17:38:30 +00:00
|
|
|
field :channel_title, :localize => true
|
2015-07-07 11:51:56 +00:00
|
|
|
field :channel_key
|
|
|
|
field :feed_name, localize: true
|
|
|
|
field :disabled, type: Boolean, default: false
|
|
|
|
field :feed_url
|
|
|
|
field :feed_uid
|
2022-05-22 10:36:26 +00:00
|
|
|
field :feeds_model
|
|
|
|
field :feeds_uid_field
|
|
|
|
field :feeds_update_callback
|
|
|
|
field :feeds_time_field, type: String, default: "postdate"
|
|
|
|
field :feeds_finish_callback
|
|
|
|
field :feeds_update_statuses_callback
|
2022-05-09 12:10:29 +00:00
|
|
|
field :enable_notify, type: Boolean, default: false
|
2020-04-05 14:36:09 +00:00
|
|
|
require 'feed_model/cache'
|
2020-07-31 06:50:17 +00:00
|
|
|
require 'fileutils'
|
2020-04-05 14:36:09 +00:00
|
|
|
include FeedModel::Cache
|
|
|
|
Category.send(:include,FeedModel::Cache)
|
2020-07-31 06:50:17 +00:00
|
|
|
after_create do
|
2022-05-09 12:10:29 +00:00
|
|
|
self.add_notify
|
2020-07-31 06:50:17 +00:00
|
|
|
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
|
2020-04-08 12:54:44 +00:00
|
|
|
before_destroy do
|
2022-05-09 12:10:29 +00:00
|
|
|
self.remove_notify
|
2021-11-18 05:59:37 +00:00
|
|
|
tmp = SiteFeedAnnc.where(feed_id: self.id)
|
2020-07-31 06:50:17 +00:00
|
|
|
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)
|
2021-11-18 05:59:37 +00:00
|
|
|
if tmp.count!=0
|
2020-04-08 12:54:44 +00:00
|
|
|
tmp.destroy
|
|
|
|
end
|
|
|
|
end
|
2022-05-22 10:36:26 +00:00
|
|
|
after_save do
|
|
|
|
unless @skip_callback
|
|
|
|
self.sync_data_to_annc
|
|
|
|
end
|
|
|
|
end
|
2015-07-07 11:51:56 +00:00
|
|
|
scope :enabled, ->{where(:disabled => false)}
|
2022-05-22 10:36:26 +00:00
|
|
|
def sync_data_to_annc(site_feed_annc=nil)
|
|
|
|
category_title = self.category[:title] rescue {}
|
|
|
|
tmp_channel_title = self.channel_title_for_cache
|
|
|
|
clone_fields =["channel_key", "merge_with_category", "remote_site_url", "feeds_model", "feeds_uid_field", "feeds_update_callback", "feeds_time_field", "feeds_update_statuses_callback"]
|
|
|
|
(site_feed_annc ? [site_feed_annc] : SiteFeedAnnc.where(:feed_id=>self.id)).each do |tmp|
|
|
|
|
clone_fields.each do |f|
|
|
|
|
tmp.send("#{f}=", self.send(f))
|
|
|
|
end
|
|
|
|
tmp[:feed_name] = self[:feed_name]
|
|
|
|
tmp.category_title = category_title
|
|
|
|
tmp.channel_title = tmp_channel_title
|
2022-05-24 08:23:55 +00:00
|
|
|
tmp.save
|
2022-05-22 10:36:26 +00:00
|
|
|
end
|
|
|
|
end
|
2021-09-15 08:43:19 +00:00
|
|
|
def get_annc(force_refresh=false)
|
2020-04-05 14:36:09 +00:00
|
|
|
main_directory = File.join("#{Rails.root}","public","site_feeds")
|
|
|
|
feed_directory = File.join(main_directory.to_s, self.id.to_s)
|
2021-09-15 08:43:19 +00:00
|
|
|
if !force_refresh && File.exists?(feed_directory)
|
2020-07-31 06:50:17 +00:00
|
|
|
anns = JSON.parse(File.read(File.join(feed_directory.to_s, self.feed_uid + ".json")))[self.channel_key.pluralize] rescue []
|
2020-04-05 14:36:09 +00:00
|
|
|
else
|
|
|
|
uri = URI(self.feed_url)
|
2020-07-31 06:50:17 +00:00
|
|
|
res = get_response_body(uri) rescue ''
|
2020-04-05 14:36:09 +00:00
|
|
|
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
|
2020-07-31 06:50:17 +00:00
|
|
|
anns = JSON.parse(res)[self.channel_key.pluralize] rescue []
|
2020-04-05 14:36:09 +00:00
|
|
|
end
|
|
|
|
anns
|
|
|
|
end
|
2020-07-31 06:50:17 +00:00
|
|
|
def get_response_body(uri)
|
2021-01-11 12:29:49 +00:00
|
|
|
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
|
2020-07-31 06:50:17 +00:00
|
|
|
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
|
2021-01-11 12:29:49 +00:00
|
|
|
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
|
2020-07-31 06:50:17 +00:00
|
|
|
return res.body
|
|
|
|
else
|
|
|
|
return res.body
|
|
|
|
end
|
|
|
|
end
|
2016-04-12 06:23:43 +00:00
|
|
|
def category
|
|
|
|
Category.find(self.merge_with_category) rescue nil
|
|
|
|
end
|
2020-04-05 14:36:09 +00:00
|
|
|
def channel_title_for_cache
|
|
|
|
!self[:channel_title].to_s.empty? ? self[:channel_title] : I18n.t("feed.source")
|
|
|
|
end
|
2022-05-10 02:00:50 +00:00
|
|
|
def http_request(http, request)
|
|
|
|
response = http.request(request)
|
|
|
|
if response.code.to_i == 301 || response.code.to_i == 302
|
|
|
|
location = response["location"]
|
|
|
|
new_uri = URI(location)
|
|
|
|
http = Net::HTTP.new(new_uri.host, new_uri.port)
|
|
|
|
if location.include?('https')
|
|
|
|
http.use_ssl = true
|
|
|
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
|
|
end
|
|
|
|
request.instance_variable_set(:@path, new_uri.path)
|
|
|
|
response = http_request(http, request)
|
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
2022-05-09 12:10:29 +00:00
|
|
|
def add_notify
|
|
|
|
unless self.enable_notify
|
|
|
|
root_url = Site.first.root_url rescue ""
|
|
|
|
if root_url.present?
|
|
|
|
uri = URI(self.remote_site_url)
|
|
|
|
http_req = Net::HTTP.new(uri.host, uri.port)
|
|
|
|
if self.remote_site_url.include?('https')
|
|
|
|
http_req.use_ssl = true
|
2022-05-19 07:53:11 +00:00
|
|
|
http_req.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
2022-05-09 12:10:29 +00:00
|
|
|
end
|
|
|
|
http_req.open_timeout = 10
|
|
|
|
request = Net::HTTP::Post.new("/xhr/#{self.channel_key.pluralize}/feed_add_remote/#{self.feed_uid}", 'Content-Type' => 'application/json')
|
|
|
|
request.body = {"url"=>root_url}.to_json
|
2022-05-10 02:00:50 +00:00
|
|
|
response = http_request( http_req , request )
|
2022-05-09 15:11:07 +00:00
|
|
|
if response.code.to_i == 200
|
|
|
|
self.update(:enable_notify=>true)
|
|
|
|
end
|
2022-05-09 12:10:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def remove_notify
|
|
|
|
if self.enable_notify
|
|
|
|
root_url = Site.first.root_url rescue ""
|
|
|
|
if root_url.present?
|
|
|
|
uri = URI(self.remote_site_url)
|
|
|
|
http_req = Net::HTTP.new(uri.host, uri.port)
|
|
|
|
if self.remote_site_url.include?('https')
|
|
|
|
http_req.use_ssl = true
|
2022-05-19 07:53:11 +00:00
|
|
|
http_req.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
2022-05-09 12:10:29 +00:00
|
|
|
end
|
|
|
|
http_req.open_timeout = 10
|
|
|
|
request = Net::HTTP::Post.new("/xhr/#{self.channel_key.pluralize}/feed_remove_remote/#{self.feed_uid}", 'Content-Type' => 'application/json')
|
|
|
|
request.body = {"url"=>root_url}.to_json
|
2022-05-10 02:00:50 +00:00
|
|
|
response = http_request( http_req , request )
|
2022-05-09 15:11:07 +00:00
|
|
|
if response.code.to_i == 200
|
|
|
|
self.update(:enable_notify=>false)
|
|
|
|
end
|
2022-05-09 12:10:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-07-07 11:51:56 +00:00
|
|
|
end
|