Skip to content

Instantly share code, notes, and snippets.

@dmitriy-sqrt
Last active March 21, 2017 09:18
Show Gist options
  • Save dmitriy-sqrt/5c5b28bc4ecb7237e6b4ec35434fda02 to your computer and use it in GitHub Desktop.
Save dmitriy-sqrt/5c5b28bc4ecb7237e6b4ec35434fda02 to your computer and use it in GitHub Desktop.
# Delete missing (deleted/draft) news_items from Budstikka internal articles
# Using 404 amedia response code to identify such articles
newspaper = Newspaper.find(4);
min_news_age = 5.days.ago
news_items_scope = NewsItem.internal.where(newspaper_id: newspaper.id).where("created_at > ?", min_news_age);
duplicated_titles = news_items_scope.group('title').count.select{|k,v| v > 1};
news_items = news_items_scope.where(title: duplicated_titles.keys).to_a;
destroyed_items_count = 0
news_items.each do |item|
url = "#{Content::Amedia::Client::CONTENT_URL}#{item.external_id}"
response = Excon.head(url)
if response.status == 404
puts "Destroyed NewsItem #{item.id} (#{item.url}"
item.destroy
destroyed_items_count = destroyed_items_count + 1
else
#puts "Item OK #{item.id} #{item.url}"
end
end;
puts "Items destroyed: #{destroyed_items_count}"
puts "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment