<commit_before>#!~/.rbenv/shims/ruby
# coding: utf-8
### ------------------------------------------
## install geocoder.
# $ gem install geocoder
# Or
## use Google Maps API
# Usage Limits.
# 2,500 free requests / day & 50 req / sec
#
# how to get API key.
# URL:
# API key name:
#
# URL encoding:
#
# for HTTP GET
# usr typhoeus / net-http / open-uri
#
# to handle JSON
# jq is better.

require 'net/http'
require 'uri'

require 'json'

key = "your api key"
address = "Imperial Palace"
ARGV[0] ? key = ARGV[0] : key
ARGV[1] ? address = ARGV[1] : address

def show_usage
  p "Need parameters placename & apikey"
  p "or change variable \"key\" to your key."
  p "$ #{$0} place_name apikey"
end

if key == "your api key" then
  show_usage
  exit
end


params = URI.encode_www_form({ key: key, address: address })
p params

uri = URI.parse("https://maps.googleapis.com/maps/api/geocode/json?#{params}")
response = Net::HTTP.get_response(uri)

# p response.body

json_data = JSON.parse(response.body)
#p json_data.class
p json_data['results'][0]['formatted_address']
p json_data['results'][0]['geometry']['location']["lat"]
p json_data['results'][0]['geometry']['location']["lng"]


require "mini_exiftool"

def set_timestamp_to_exiftags (time: , file: )
  photo = MiniExiftool.new file.to_s
  # p photo.date_time_original
  # photo.date_time_original = time
  # photo.modify_date = time  # date time
  # photo.create_date = time  # date time digitized
  p "Version ==============================--"
  p photo.GPS_Version_ID
  p photo.gps_latitude
  p photo.gps_latitude_ref
  photo.gps_latitude_ref = "North" # base on Japan
  p photo.gps_longitude
  p photo.gps_longitude_ref
  photo.gps_longitude_ref = "E" # base on Japan
  return
  begin
    photo.save!
    puts "done to rewrite exif timestamp : (#{file})"
  rescue => e
    puts e
  end
end

# create filelist

def create_filename_list (param)
  ar = []
  Dir::glob(param, File::FNM_CASEFOLD){|f|
    next unless FileTest.file?(f)
    ar << f
  }
  return ar
end


# set start time.
ARGV[3] ? date = ARGV[0] : date = Time.now.to_s

# extension
ext = ".jpg"
location = Dir::pwd

# pick up files
param_of_search = location + "/**/*" + ext

filelist = create_filename_list param_of_search

localtime = Time.parse(date)

filelist.each { |f|
  set_timestamp_to_exiftags file:f, time: localtime
  File::utime(localtime, localtime, f)
  localtime = localtime + 10
}<commit_msg>to keep original timestamp & apply location info. this will be v1.0.<commit_after>#!~/.rbenv/shims/ruby
# coding: utf-8
### ------------------------------------------
## install geocoder.
# $ gem install geocoder
# Or
## use Google Maps API
# Usage Limits.
# 2,500 free requests / day & 50 req / sec
#
# how to get API key.
# URL on google:
#
# URL encoding:
#
# for HTTP GET
# usr typhoeus / net-http / open-uri
#
# to handle JSON
# jq is better.

require 'net/http'
require 'uri'
require "mini_exiftool"

require 'json'

key = "your api key"
address = "Imperial Palace"


ARGV[0] ? address = ARGV[0] : address
ARGV[1] ? key = ARGV[1] : key

def show_usage
  printf "Need parameters placename & apikey"
  puts "or rewrite variable \"key\" to your key."
  puts "$ #{$0} \[place_name\] \[apikey\]"
end

if key == "your api key" then
  show_usage
  exit
end


params = URI.encode_www_form({ key: key, address: address })
p params

uri = URI.parse("https://maps.googleapis.com/maps/api/geocode/json?#{params}")
response = Net::HTTP.get_response(uri)

# p response.body

json_data = JSON.parse(response.body)
p json_data['results'][0]['formatted_address']      # for debug
lat = json_data['results'][0]['geometry']['location']["lat"]
lng = json_data['results'][0]['geometry']['location']["lng"]

p "lat = (#{lat})" 
p "lng = (#{lng})" 

def set_location_info_to_exiftags (lat: , lng: , file: )
  photo = MiniExiftool.new file.to_s
  photo.gps_latitude = lat
  photo.gps_latitude_ref = "North" # base on Japan
  photo.gps_longitude = lng
  photo.gps_longitude_ref = "E" # base on Japan

  begin
    photo.save!
    puts "done to rewrite exif for location : (#{file})"
  rescue => e
    puts e
  end
end

# create filelist

def create_filename_list (param)
  ar = []
  Dir::glob(param, File::FNM_CASEFOLD){|f|
    next unless FileTest.file?(f)
    ar << f
  }
  return ar
end

# extension
ext = ".jpg"
location = Dir::pwd

# pick up files
param_of_search = location + "/**/*" + ext

filelist = create_filename_list param_of_search

filelist.each { |f|
  # keep a time stamp of the file.
  origintime = File.stat(f).mtime
  set_location_info_to_exiftags lat: lat, lng: lng, file:f
  File::utime(origintime, origintime, f) 
}
{'fraction_non_alphanumeric': 0.16203404438698557, 'fraction_numerical': 0.007756948933419522, 'mean_word_length': 3.5824284304047382, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '122683', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1703, 'n_words': 457}