Skip to content

Instantly share code, notes, and snippets.

@sdorunga
Forked from anonymous/-
Created December 2, 2013 09:11
Show Gist options
  • Save sdorunga/7746921 to your computer and use it in GitHub Desktop.
Save sdorunga/7746921 to your computer and use it in GitHub Desktop.
Agency importer script
# run it after XX:05 since rate and availability is scheduled to be updated
billy_blue_ids = ["3883", "3884", "3885", "3887", "3896", "3904", "3942", "3958", "3961", "3962", "3963", "4092", "4300", "5918", "6168", "6169", "6176", "6177", "6184", "6298", "6302", "6441", "6589", "9275", "9655", "9697", "11805", "13475", "13827", "20266", "20267", "20268", "21389", "21398", "21400", "21406", "21440", "21482", "21493", "21494", "21483", "17512", "20528", "20530", "20531", "20532", "20533", "20534", "20535", "20536", "20537", "20538", "20539", "20541", "20542", "20543", "20545", "20546", "20969", "20971", "20972", "20973", "20974", "20975", "20983", "20984", "20986", "20995", "20996", "20998", "21001", "21002", "21004", "21005", "21006", "21007", "21286", "21302", "21303", "21311", "21317", "21335", "21336", "21337", "21338", "21339", "21340", "21341", "21353", "21354", "21358", "21360", "21362", "21364", "21365", "21366", "21367", "21368", "21369", "21390", "21479", "20599", "21331", "21375", "21463", "21464", "11946", "14699", "20957", "21107", "21292", "21298", "21318", "21319", "21320", "21330", "21439", "9825", "10636", "13631", "15686", "19270", "20345", "20346", "20897", "20951", "21106", "21109", "21119", "21193", "21195", "21260", "21269", "21287", "21289", "21327", "21328", "21329", "21371", "21372", "21373", "21374", "21392", "21393", "21396", "21397", "21403", "21404", "21405", "21408", "21438", "21461"]
billy_pds_ids = ["21299", "21315", "21363", "21376", "21441", "21442", "21443", "21444", "21446", "21447", "21448", "21449", "21450", "21515", "21516", "21517", "21518", "21519", "21520", "21521", "3265", "3315", "9517", "9518", "13909", "18062", "18065", "2942", "3702", "21052", "21059", "21060", "21061", "21062", "21316", "21321", "21322", "21323", "21324", "21325", "21344"]
puts "INFO: Properties for both the usernames before syncing: #{Property.count(:conditions => ['external_source = ?', 'billy_pds'])}"
{:billypds => billy_pds_ids, :billyblue => billy_blue_ids}.each do |username, external_ids|
display_name = username.to_s #'billypds' or 'billyblue'
user = User.find_by_display_name(display_name) # if user is nil then stop and investigate
raise "User is not present, double check your data" unless user
url = "http://xmlsync.billypds.com/details.xml?key=6a0edaf839a128f87b2422c78d2cb84e&id=#{external_ids.join(',')}"
response = Net::HTTP.get_response URI.parse(url)
parser_type = AgentPropertyFeed.response_type(AgentPropertyFeed::HttpConnection::BillyPds.new) # BillyPds is used for both billypds and billyblue
agent_data = parser_type.new(response.body).details
agent_data.size
# ----------------------------------------------------------------------------------------------------
agent_data.each do |attributes|
puts "CREATING: #{attributes[:external_id]}"
begin
attributes.reject!{|k,_| [:tax, :availability, :status].include? k}
property_attributes = attributes.slice!(:cleaning_fee, :deposit)
property_images = property_attributes.delete(:images)
AgentPropertyFeed::PropertyImporter.create!(:base_attributes => property_attributes, :images => property_images, :additional_fees => attributes, :user => user, :external_source => 'billy_pds')
rescue => e
# take a note of errored ids
puts "ERROR: #{e.message} : #{property_attributes[:external_id]}"
end
end
# ----------------------------------------------------------------------------------------------------
# make imported properties unpublished
Property.all(:conditions => ["external_source = ? AND external_id IN (?)", 'billy_pds', external_ids]).each do |property|
property.update_attributes(
:published => false,
:hidden_from_search => true,
:cancellation_policy => "relaxed_100_50"
#:payment_method => "BCV"
)
end
good_external_ids = Property.all(:conditions => ["external_source = ? AND external_id IN (?)", 'billy_pds', external_ids]).map(&:external_id).map(&:to_i)
# show which external ids failed
failed_ids = external_ids - good_external_ids
puts "WARNING: The following ids have failed importation: #{failed_ids}" unless failed_ids.empty?
good_external_ids.each do |external_id|
puts "R&A FOR: #{external_id}"
unless Property.find_by_id(external_id)
puts "ERROR: Property with #{external_id} not present"
next
end
AgentPropertyFeed::DataImporter.update_rate_and_availability(AgentPropertyFeed::HttpConnection::BillyPds, :collector_methods => ['details.xml', 'availability.xml', 'rates.xml', 'rate_anomaly.xml'], :params => {:id => external_id})
end
end
puts "INFO: Properties for both the usernames after syncing: #{Property.count(:conditions => ['external_source = ?', 'billy_pds'])}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment