<commit_before>require './app'
require 'exifr'
require 'digest'
require 'json'
require 'byebug'

class PhoIn
  def self.import_photo(file_path, tags)
    image=EXIFR::JPEG.new(file_path)
    tags = create_tags(tags)
    file = File.new(file_path)
    camera = create_camera(image.make, image.model)
    create_photo(image, camera, file, tags)
  end

  def self.create_tags(json)
    return [] if json.nil?
    ret = []
    tags = JSON.parse(json)
    tags.each{|k,v| ret << Tag.find_or_create(tag: v)}
    ret
  end

  def self.create_camera(make, model)
    Camera.find_or_create(make: make, model: model)
  end

  def self.create_photo(image, camera, file, tags)
    sha= Digest::SHA256.file(file)
    photo = Photo.find(hash: sha.hexdigest)
    if(photo.nil?)
      photo = Photo.new(
        hash: sha.hexdigest,
        path: File.realpath(file),
        size: file.size,
        date: image.date_time,
        exposure_time: image.exif&.exposure_time,
        f_stop: image.exif&.f_number,
        iso_speed: image.iso_speed_ratings,
        shutter_speed: image.shutter_speed_value,
        aperture: image.aperture_value,
        brightness: image.brightness_value,
        focal_length: image.focal_length,
        flash: (image.flash == 1),
        x_resolution: image.x_resolution,
        y_resolution: image.y_resolution,
        direction: image.gps_img_direction,
        altitude: image.gps_altitude,
        width: image.width,
        height: image.height,
        latitude: image.exif&.gps&.latitude,
        longitude: image.exif&.gps&.longitude
      )
      photo.camera = camera
      photo.save
      tags.each do |tag|
        photo.add_tag tag
      end
      photo.save
    end
    photo
  end
end

image, tags = ARGV
PhoIn.import_photo(image, tags)
<commit_msg>add orientation to import script<commit_after>require './app'
require 'exifr'
require 'rmagick'
require 'digest'
require 'json'
require 'byebug'

class PhoIn
  def self.import_photo(file_path, tags)
    image=EXIFR::JPEG.new(file_path)
    magick = Magick::Image.read(file_path).first
    tags = create_tags(tags)
    file = File.new(file_path)
    camera = create_camera(image.make, image.model)
    create_photo(magick, image, camera, file, tags)
  end

  def self.create_tags(json)
    return [] if json.nil?
    ret = []
    tags = JSON.parse(json)
    tags.each{|k,v| ret << Tag.find_or_create(tag: v)}
    ret
  end

  def self.create_camera(make, model)
    Camera.find_or_create(make: make, model: model)
  end

  def self.create_photo(magick, image, camera, file, tags)
    sha= Digest::SHA256.file(file)
    photo = Photo.find(hash: sha.hexdigest)
    if(photo.nil?)
      photo = Photo.new(
        hash: sha.hexdigest,
        path: File.realpath(file),
        size: file.size,
        date: image.date_time,
        exposure_time: image.exif&.exposure_time,
        f_stop: image.exif&.f_number,
        iso_speed: image.iso_speed_ratings,
        shutter_speed: image.shutter_speed_value,
        aperture: image.aperture_value,
        brightness: image.brightness_value,
        focal_length: image.focal_length,
        flash: (image.flash == 1),
        x_resolution: image.x_resolution,
        y_resolution: image.y_resolution,
        direction: image.gps_img_direction,
        altitude: image.gps_altitude,
        width: image.width,
        height: image.height,
        latitude: image.exif&.gps&.latitude,
        longitude: image.exif&.gps&.longitude,
        orientation: magick.orientation&.to_i || image.exif&.orientation&.to_i
      )
      photo.camera = camera
      photo.save
      tags.each do |tag|
        photo.add_tag tag
      end
      photo.save
    end
    photo
  end
end

image, tags = ARGV
PhoIn.import_photo(image, tags)

{'fraction_non_alphanumeric': 0.11037234042553191, 'fraction_numerical': 0.002127659574468085, 'mean_word_length': 2.74975074775673, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2585287', 'n_tokens_mistral': 1330, 'n_tokens_neox': 1291, 'n_words': 259}