#!/usr/local/bin/perl -w
###########################################
# photo-index -- Index iPhone pic Exif GPS
#   data on Elasticsearch
# Mike Schilli, 2014 (m@perlmeister.com)
###########################################
use strict;
use File::Find;
use Elasticsearch;
use IPhonePicGeo;

my $idx = "photos";
my $dir = glob "~/iphone";

my $es = Elasticsearch->new( );

eval {  # Delete existing index if present
   $es->indices->delete( index => $idx ) };

$es->indices->create(
  index => $idx,
  body  => {
    mappings => {
      photo => {
        properties => {
          Location => {
            type => "geo_point" } } } } }
);

find sub {
  my $pic = $File::Find::name;

  return if ! -f $pic;
  return if $pic !~ /.jpg$/i;

  my( $lat, $lon ) = 
    IPhonePicGeo::photo_latlon( $pic );
  return if !defined $lat;

  $es->index( 
    index => $idx,
    type  => "photo",
    body  => {
      file     => $pic,
      Location => [ $lat, $lon ],
    },
  );

  print "Added: $pic ($lat/$lon)\n";

}, $dir;
