#!/usr/bin/env python3
'''
	jpeglocation --- Extract GPS information and show Google Map format ---

Copyright (c) 2025, Koh-ichi Ito
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, 
  this list of conditions and the following disclaimer in the documentation 
  and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its contributors 
  may be used to endorse or promote products derived from this software 
  without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''

import exifread
import os
import re
import sys


REGE_EXT_JPEG = re.compile(r'\.(?i:JPEG|JPG)$')


class NotAJPEG(Exception):
	pass


class NoGPSInfo(Exception):
	pass


def location(file_name):
	'''
	Extract GPS information from EXIF and return in Google Map format.
	'''

	# Try all files is strict way.
	if not REGE_EXT_JPEG.search(file_name):
		raise NotAJPEG(f'{file_name} must not be a JPEG file.')
	with open(file_name, 'rb') as f:
		exif_data = exifread.process_file(f)
	#
	# Neither documentation nor getter method on "values" is provided
	# as of exifread version 3.0.0.
	#
	if 'GPS GPSLatitudeRef' not in exif_data:
		raise NoGPSInfo(f'No GPS information on {file_name}')
	latitude_ref = exif_data['GPS GPSLatitudeRef']
	latitude_deg = exif_data["GPS GPSLatitude"].values[0]
	latitude_min = exif_data["GPS GPSLatitude"].values[1]
	latitude_sec = exif_data["GPS GPSLatitude"].values[2].decimal()
	longitude_ref = exif_data['GPS GPSLongitudeRef']
	longitude_deg = exif_data['GPS GPSLongitude'].values[0]
	longitude_min = exif_data['GPS GPSLongitude'].values[1]
	longitude_sec = exif_data['GPS GPSLongitude'].values[2].decimal()
	return f'{latitude_deg}°{latitude_min}\'{latitude_sec}"{latitude_ref} {longitude_deg}°{longitude_min}\'{longitude_sec}"{longitude_ref}'

if __name__ == '__main__':
	MyName = os.path.basename(sys.argv[0] )
	for file_name in sys.argv[1:]:
		try:
			print('{}: {}'.format(file_name, location(file_name) ) )
		except Exception as exc:
			sys.stderr.write(f'{MyName}: {exc}\n')
			# Continue, don't abort.
	sys.exit(os.EX_OK)
