<commit_before>namespace :iphoto do  
  desc "Resets iPhoto data in database"
  task(:reset => :environment) do
    puts "Destroying #{Media.count} media entries"
    Media.delete_all
    puts "Destroying #{Roll.count} roll entries"
    Roll.delete_all
  end

  desc "Updates iPhoto data incrementally into database"
  task(:update => :environment) do
    @@library = IphotoLibrary.path
    albumdata = IphotoLibrary.albumdata_path

    result = Plist::parse_xml(albumdata)

    @rolls = result['List of Rolls']
    @list = result['Master Image List']

    @rolls.each do |roll|
      roll_model = Roll.find(roll['RollID'])
      if roll_model.nil?
        created = create_roll(roll)

        puts "Created #{created}"
      else
        update_roll(roll_model, roll)
      end
    end

    @list.each_pair do |key, value|
      media_model = Media.find(key)
      if media_model.nil?
        case value['MediaType']
        when 'Image'
          created = create_media('Photo', key, value)
        when 'Movie'
          created = create_media('Movie', key, value)
        end
        puts "Created #{created}"
      else
        update_media(media_model, value)
      end
    end
  end

  desc "Load iPhoto library AlbumData.xml into database"
  task(:load => :reset) do
    @@library = IphotoLibrary.path
    albumdata = IphotoLibrary.albumdata_path

    result = Plist::parse_xml(albumdata)

    @rolls = result['List of Rolls']
    @list = result['Master Image List']

    roll_count = 0
    @rolls.each do |roll|
      create_roll(roll)
      roll_count += 1
    end
    
    puts "Created #{roll_count} roll entries"
<commit_msg>update task deletes, RecordNotFound issue fixed [#2 state:resolved]<commit_after>namespace :iphoto do  
  desc "Resets iPhoto data in database"
  task(:reset => :environment) do
    puts "Destroying #{Media.count} media entries"
    Media.delete_all
    puts "Destroying #{Roll.count} roll entries"
    Roll.delete_all
  end

  desc "Updates iPhoto data incrementally into database"
  task(:update => :environment) do
    @@library = IphotoLibrary.path
    albumdata = IphotoLibrary.albumdata_path

    result = Plist::parse_xml(albumdata)

    @rolls = result['List of Rolls']
    @list = result['Master Image List']

    # First checks if any rolls or media have been deleted, pushes those changes
    existing_ids = Roll.find(:all, :select => 'id').map { |m| m.id }
    current_ids = @rolls.map { |r| r['RollID'] }
    nonexistent_ids = existing_ids - current_ids

    unless nonexistent_ids.empty?
      puts "Deleting rolls with ids: #{nonexistent_ids.inspect}"
      Roll.delete(nonexistent_ids)
    end

    existing_ids = Media.find(:all, :select => 'id').map { |m| m.id }
    current_ids = @list.map { |k, v| k.to_i }
    nonexistent_ids = existing_ids - current_ids

    unless nonexistent_ids.empty?
      puts "Deleting media with ids: #{nonexistent_ids.inspect}"
      Media.delete(nonexistent_ids)
    end

    # Next create new rolls or media / update existing ones
    
    @rolls.each do |roll|
      begin
        roll_model = Roll.find(roll['RollID'])
      rescue ActiveRecord::RecordNotFound
        created = create_roll(roll)
        puts "Created #{created}"
      else
        update_roll(roll_model, roll)
      end
    end

    @list.each_pair do |key, value|
      begin
        media_model = Media.find(key)
      rescue ActiveRecord::RecordNotFound
        if media_model.nil?
          case value['MediaType']
          when 'Image'
            created = create_media('Photo', key, value)
          when 'Movie'
            created = create_media('Movie', key, value)
          end
          puts "Created #{created}"
        end
      else
        update_media(media_model, value)
      end
    end
    
  end

  desc "Load iPhoto library AlbumData.xml into database"
  task(:load => :reset) do
    @@library = IphotoLibrary.path
    albumdata = IphotoLibrary.albumdata_path

    result = Plist::parse_xml(albumdata)

    @rolls = result['List of Rolls']
    @list = result['Master Image List']

    roll_count = 0
    @rolls.each do |roll|
      create_roll(roll)
      roll_count += 1
    end
    
    puts "Created #{roll_count} roll entries"

{'fraction_non_alphanumeric': 0.10222960151802657, 'fraction_numerical': 0.0011859582542694497, 'mean_word_length': 2.695880806310254, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5979037', 'n_tokens_mistral': 1318, 'n_tokens_neox': 1292, 'n_words': 353}