Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Fix Missing Restaurant Image

Fix Restaurant Cover Images

host = 'https://hungryhub.com'

def fix_image_url(url)
  return url if url.include?('http')

  host = 'https://images.hungryhub.com'
  URI.join(host, URI.escape(url)).to_s
end

errors = []

# get pictures from production
dummy_logo_url = 'https://images.hungryhub.com/uploads/restaurant/logo/997/12764618_993228954095365_7698085734652638054_o.jpg'
dummy_picture_urls = ['uploads/restaurants/997/photos/32963/RackMultipart20200220-229-13mf4s0.jpg',
                      'uploads/restaurants/997/photos/32964/RackMultipart20200220-229-fsfiie.jpg',
                      'uploads/restaurants/997/photos/39630/RackMultipart20200703-231-we36ta.jpg',
                      'uploads/restaurants/997/photos/39632/RackMultipart20200703-231-31m2jk.jpg',
                      'uploads/restaurants/997/photos/39636/RackMultipart20200703-205-1t0mil.jpg',
                      'uploads/restaurants/997/photos/79297/RackMultipart20220707-107-tbtyt2.jpg',
                      'uploads/restaurants/997/photos/79303/RackMultipart20220707-107-1x10xhy.jpg',
                      'uploads/restaurants/997/photos/79304/RackMultipart20220707-135-6d09zd.jpg',
                      'uploads/restaurants/997/photos/79306/RackMultipart20220707-135-6k460h.jpg',
                      'uploads/restaurants/997/photos/79308/RackMultipart20220707-135-1bhp2vp.jpg',
                      'uploads/restaurants/997/photos/79310/RackMultipart20220707-135-iykepw.jpg',
                      'uploads/restaurants/997/photos/84638/RackMultipart20220922-113-10dcd3u.jpg',
                      'uploads/restaurants/997/photos/94984/RackMultipart20230420-134-qtys55.jpg',
                      'uploads/restaurants/997/photos/94985/RackMultipart20230420-110-14rfghz.jpg',
                      'uploads/restaurants/997/photos/94986/RackMultipart20230420-110-awerec.jpg',
                      'uploads/restaurants/997/photos/94987/RackMultipart20230420-110-alwgvl.jpg',
                      'uploads/restaurants/997/photos/94988/RackMultipart20230420-110-1u4ykbu.jpg',
                      'uploads/restaurants/997/photos/94989/RackMultipart20230420-110-lp29jl.jpg',
                      'uploads/restaurants/997/photos/94990/RackMultipart20230420-110-upguyj.jpg',
                      'uploads/restaurants/997/photos/94991/RackMultipart20230420-110-q7i1av.jpg',
                      'uploads/restaurants/997/photos/94992/RackMultipart20230420-109-53qw2.jpg']

Restaurant.active.not_expired.find_each do |restaurant|
  if restaurant.logo.blank? || (restaurant.logo.present? && !restaurant.logo.file.present?)
    data = Retriable.retriable do
      response = Faraday.get "#{host}/api/v5/restaurants/#{restaurant.id}.json?include_packages=false&include_pictures=true&minor_version=3&preview_mode=false"
      JSON.parse(response.body)
    end
    attributes = data['data']['attributes']

    logo_url = attributes['logo_url']['medium']
    if logo_url.present?
      restaurant.remote_logo_url = fix_image_url(logo_url)
      restaurant.save!
    else
      restaurant.remote_logo_url = fix_image_url(dummy_logo_url)
      restaurant.save!
    end
  end

  if data.nil?
    data = Retriable.retriable do
      response = Faraday.get "#{host}/api/v5/restaurants/#{restaurant.id}.json?include_packages=false&include_pictures=true&minor_version=3&preview_mode=false"
      JSON.parse(response.body)
    end
  end

  pictures = data['included'].select { |r| r['type'] == 'restaurants-pictures' }
  restaurant.pictures.each_with_index do |picture, index|
    if picture.item.blank? || (picture.item.present? && !picture.item.file.present?)
      prod_picture = pictures[index]
      if prod_picture.present?
        picture.remote_item_url = fix_image_url(prod_picture['attributes']['item']['url'])
        picture.save!
      else
        picture.remote_item_url = fix_image_url(dummy_picture_urls.sample)
        picture.save!
      end
    else
      picture.remote_item_url = fix_image_url(dummy_picture_urls.sample)
      picture.save!
    end
  end

  if restaurant.pictures.cover.blank?
    picture = restaurant.pictures.first
    if picture
      picture.tag_list << 'cover'
      picture.save!
    end
  end

  GenerateCompactRestaurantsWorker.perform_async(restaurant.id)

  puts "done restaurant id ->>>>>>>>>>>> #{restaurant.id}"

  restaurant.refresh_view_cache_key
  restaurant.touch
rescue StandardError
  errors.push restaurant
  next
end