Last active
March 21, 2017 09:18
-
-
Save dmitriy-sqrt/5c5b28bc4ecb7237e6b4ec35434fda02 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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