#!/usr/bin/perl -- use DBI; use Image::ExifTool; use Image::ExifTool::Location; use Geo::GeoNames; use Data::Dumper; my $geo = new Geo::GeoNames(); my $exif = Image::ExifTool->new(); $TMP_PATH="/tmp"; #LOCAL TEMP PATH (no last slash) $G2_URL=""; #FULL URL TO GALLERY2 (no last slash) $G2_USER=""; #GALLERY2 USERNAME WITH ENOUGH RIGHTS $G2_PASS=""; #GALLERY2 PASSWORD $g2_ownerId = ""; $dbh = DBI->connect("dbi:mysql::", '', ''); $g2_albumDir = ""; #GET THE LOGIN PAGE OF GALLERY2 (LOGIN WITH ADMIN ACCOUNT). `wget --quiet --cookies=on --output-document=$TMP_PATH/g2_loginpage --save-cookies $TMP_PATH/myg2cookies "$G2_URL/main.php?g2_controller=core.UserLogin&g2_form[formName]=UserLogin&g2_form[username]=$G2_USER&g2_form[password]=$G2_PASS&g2_form[action][login]=Login"`; #GRAB THE AUTH_TOKEN (WE NEED IT IN THE NEXT STEP). $AUTHTOKEN=`less $TMP_PATH/g2_loginpage | grep -m1 g2_authToken | cut -d"_" -f8 | cut -d"%" -f2 | cut -d"D" -f2- | cut -d"&" -f1`; $AUTHTOKEN =~ s/\n//; my ($selected) = @_; my $str = ""; $query = "SELECT * FROM g_AlbumItem ai LEFT JOIN g_FileSystemEntity fse ON ai.g_id = fse.g_id"; $sth = $dbh->prepare($query); $rs = $sth->execute; while ($r = $sth->fetchrow_hashref()) { #$albumId = print $r->{'g_pathComponent'}."\n"; $albumId = $r->{'g_id'}; $parentId = $albumId; $i=0; $pathStr = ""; while (($parentId != 7) && ($i < 5)) { $i++; $pathQuery = "select g_pathComponent Path,g_parentId ParentId from g_FileSystemEntity fse". " LEFT JOIN g_ChildEntity ce ON fse.g_Id = ce.g_Id". " LEFT JOIN g_Item parent ON parent.g_Id = ce.g_parentId". " WHERE fse.g_id = ?"; $pathHandler = $dbh->prepare($pathQuery); $pathHandler->execute($parentId); $path = $pathHandler->fetchrow_hashref(); $parentId = $path->{'ParentId'}; $pathStr = $path->{'Path'} . "/" . $pathStr; } $childrenQuery = "select g_pathComponent Path,g_ownerId OwnerId,fse.g_id ItemId, g_parentId ParentId FROM g_ChildEntity ce ". " LEFT JOIN g_FileSystemEntity fse ON fse.g_Id = ce.g_Id". " LEFT JOIN g_Item parent ON parent.g_Id = ce.g_parentId". " WHERE ce.g_parentId = ? and g_ownerId = $G2_ownerId"; $childrenHandler = $dbh->prepare($childrenQuery); $childrenHandler->execute($albumId); $fileList = ""; while ($child = $childrenHandler->fetchrow_hashref()) { $childPath = "$g2_albumDir/".$pathStr.$child->{'Path'}; if ($childPath =~ /.jpg\Z/i) { my ($lat, $lon) = $exif->GetLocation(); # Extract info from existing image $exif->ExtractInfo($childPath); my ($lat, $lon) = $exif->GetLocation(); if ($lat ne "") { $itemId = $child->{'ItemId'}; print $childPath."\n"; $result = $geo->find_nearby_placename( lat => $lat, lng => $lon, style=> "FULL", maxRows => 1, radius => 1 ); $tag1 = $result->[0]->{'name'}; $tag2 = $result->[0]->{'countryName'}; $tag3 = $result->[0]->{'adminName1'}; if ($tag1 !~ /HASH/) { `wget --quiet --output-document=/dev/null --cookies=on --load-cookies $TMP_PATH/myg2cookies "$G2_URL/main.php?g2_view=tags.TagsCallback&g2_itemId=$itemId&g2_command=newTag&g2_tagName=$tag1&g2_authToken=$AUTHTOKEN&" `; } if ($tag2 !~ /HASH/) { `wget --quiet --output-document=/dev/null --cookies=on --load-cookies $TMP_PATH/myg2cookies "$G2_URL/main.php?g2_view=tags.TagsCallback&g2_itemId=$itemId&g2_command=newTag&g2_tagName=$tag2&g2_authToken=$AUTHTOKEN&" `; } if (($tag3 ne "") && ($tag3 !~ /HASH/)) { `wget --quiet --output-document=/dev/null --cookies=on --load-cookies $TMP_PATH/myg2cookies "$G2_URL/main.php?g2_view=tags.TagsCallback&g2_itemId=$itemId&g2_command=newTag&g2_tagName=$tag3&g2_authToken=$AUTHTOKEN&" `; } } } } } `rm -f $TMP_PATH/g2_loginpage`; `rm -f $TMP_PATH/myg2cookies`;