Skip to content

Instantly share code, notes, and snippets.

@yogendra689
Last active October 14, 2017 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yogendra689/5349f2772897c9fdde70900e92f68633 to your computer and use it in GitHub Desktop.
Save yogendra689/5349f2772897c9fdde70900e92f68633 to your computer and use it in GitHub Desktop.
# edcast query:
select cards.id, resources.url, cards.ecl_id from cards inner join resources on cards.resource_id = resources.id where resources.url like '%www.lynda.com%' and cards.organization_id=428 and cards.ecl_id is not null and ecl_metadata like '%68c2d101-21ca-4738-a289-61b33ad3e412%'
# approx 1882 cards
# get csv
# Ecl app
require 'open-uri'
require 'csv'
contents = open("/Users/yogendra/Desktop/hpe_ecl_cards.csv")
CSV.foreach(contents, headers: false, header_converters: :symbol, encoding: 'ISO-8859-1', col_sep: ',' ) do |row|
next if row[0] == 'id'
puts "card_id: #{row[0]}"
puts "url: #{row[1]}"
edcast_card_id = row[0]
original_url = row[1]
old_ecl_id = row[2]
url_without_query = row[1].split("?").first
source_id = "68c2d101-21ca-4738-a289-61b33ad3e412"
new_updated_ecl_ids = {}
content_item = ContentItem.search("*", where: {"source_id" => source_id, url: original_url},limit: 1).first
if content_item
if old_ecl_id != content_item.id
new_updated_ecl_ids[edcast_card_id] = content_item.id
end
else
if original_url != url_without_query
content_item = ContentItem.search("*", where: {"source_id" => source_id, url: url_without_query},limit: 1).first
if content_item
if old_ecl_id != content_item.id
new_updated_ecl_ids[edcast_card_id] = content_item.id
end
end
end
end
end
# Edcast
new_updated_ecl_ids.each do |k, v|
card = Card.where(id: k).first
if card
card.update(ecl_id: v)
if card.resource.present?
resource = card.resource
uri = URI(resource.url)
if uri.query != "org=hpe.com"
params = URI.decode_www_form("") << ['org', 'hpe.com']
uri.query = URI.encode_www_form(params)
modified_url = uri.to_s
card.ecl_metadata['external_id'] = modified_url
card.save!
resource.url = modified_url
resource.save!
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment