2016-01-08 23:49:06 +01:00
|
|
|
"""
|
2021-07-30 07:41:02 +02:00
|
|
|
Methods for interacting with information Dozo caches about stored media.
|
2016-01-08 23:49:06 +01:00
|
|
|
"""
|
2016-03-12 20:09:28 +01:00
|
|
|
from builtins import map
|
|
|
|
from builtins import object
|
2016-01-08 23:49:06 +01:00
|
|
|
|
2015-10-10 09:02:53 +02:00
|
|
|
import json
|
2015-12-29 09:07:50 +01:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2016-12-13 08:54:33 +01:00
|
|
|
from math import radians, cos, sqrt
|
|
|
|
from shutil import copyfile
|
|
|
|
from time import strftime
|
|
|
|
|
2021-07-30 07:41:02 +02:00
|
|
|
from dozo import constants
|
2015-10-10 09:02:53 +02:00
|
|
|
|
2016-01-02 08:23:06 +01:00
|
|
|
|
2015-10-10 09:02:53 +02:00
|
|
|
class Db(object):
|
2016-01-08 23:49:06 +01:00
|
|
|
|
2021-07-30 07:41:02 +02:00
|
|
|
"""A class for interacting with the JSON files created by Dozo."""
|
2016-01-08 23:49:06 +01:00
|
|
|
|
2021-06-12 19:49:29 +02:00
|
|
|
def __init__(self, target_dir):
|
2021-07-30 07:41:02 +02:00
|
|
|
# verify that the application directory (~/.dozo) exists,
|
2016-01-02 08:23:06 +01:00
|
|
|
# else create it
|
2021-06-12 19:49:29 +02:00
|
|
|
# if not os.path.exists(constants.application_directory):
|
|
|
|
# os.makedirs(constants.application_directory)
|
2015-10-10 09:02:53 +02:00
|
|
|
|
2021-06-12 19:49:29 +02:00
|
|
|
# Create dir for target database
|
2021-07-30 07:41:02 +02:00
|
|
|
dirname = os.path.join(target_dir, '.dozo')
|
2021-06-12 19:49:29 +02:00
|
|
|
# Legacy dir
|
|
|
|
# dirname = constants.application_directory
|
|
|
|
|
|
|
|
if not os.path.exists(dirname):
|
|
|
|
try:
|
|
|
|
os.makedirs(dirname)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# self.hash_db = constants.hash_db
|
|
|
|
self.hash_db_file = os.path.join(dirname, constants.hash_db)
|
|
|
|
self.check_db(self.hash_db_file)
|
2015-10-15 08:40:40 +02:00
|
|
|
|
2015-10-10 09:02:53 +02:00
|
|
|
self.hash_db = {}
|
2015-10-15 08:40:40 +02:00
|
|
|
|
2016-01-02 08:23:06 +01:00
|
|
|
# We know from above that this file exists so we open it
|
|
|
|
# for reading only.
|
2021-06-12 19:49:29 +02:00
|
|
|
with open(self.hash_db_file, 'r') as f:
|
2015-10-10 09:02:53 +02:00
|
|
|
try:
|
|
|
|
self.hash_db = json.load(f)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2015-12-24 20:39:28 +01:00
|
|
|
|
2021-06-12 19:49:29 +02:00
|
|
|
# self.location_db_file = constants.location_db
|
|
|
|
self.location_db_file = os.path.join(dirname, constants.location_db)
|
|
|
|
self.check_db(self.location_db_file)
|
2015-12-24 20:39:28 +01:00
|
|
|
|
|
|
|
self.location_db = []
|
|
|
|
|
2016-01-02 08:23:06 +01:00
|
|
|
# We know from above that this file exists so we open it
|
|
|
|
# for reading only.
|
2021-06-12 19:49:29 +02:00
|
|
|
with open(self.location_db_file, 'r') as f:
|
2015-12-24 20:39:28 +01:00
|
|
|
try:
|
|
|
|
self.location_db = json.load(f)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
2021-06-12 19:49:29 +02:00
|
|
|
def check_db(self, db_file):
|
|
|
|
'''Load db from file'''
|
|
|
|
# If the hash db doesn't exist we create it.
|
|
|
|
# Otherwise we only open for reading
|
|
|
|
if not os.path.isfile(db_file):
|
|
|
|
with open(db_file, 'a'):
|
|
|
|
os.utime(db_file, None)
|
|
|
|
|
2015-10-10 09:02:53 +02:00
|
|
|
def add_hash(self, key, value, write=False):
|
2016-01-08 23:49:06 +01:00
|
|
|
"""Add a hash to the hash db.
|
|
|
|
|
|
|
|
:param str key:
|
|
|
|
:param str value:
|
|
|
|
:param bool write: If true, write the hash db to disk.
|
|
|
|
"""
|
2015-10-10 09:02:53 +02:00
|
|
|
self.hash_db[key] = value
|
2016-01-02 08:23:06 +01:00
|
|
|
if(write is True):
|
2015-10-10 09:02:53 +02:00
|
|
|
self.update_hash_db()
|
|
|
|
|
2016-12-13 08:54:33 +01:00
|
|
|
# Location database
|
|
|
|
# Currently quite simple just a list of long/lat pairs with a name
|
|
|
|
# If it gets many entries a lookup might take too long and a better
|
|
|
|
# structure might be needed. Some speed up ideas:
|
|
|
|
# - Sort it and inter-half method can be used
|
|
|
|
# - Use integer part of long or lat as key to get a lower search list
|
|
|
|
# - Cache a small number of lookups, photos are likely to be taken in
|
|
|
|
# clusters around a spot during import.
|
|
|
|
def add_location(self, latitude, longitude, place, write=False):
|
|
|
|
"""Add a location to the database.
|
|
|
|
|
|
|
|
:param float latitude: Latitude of the location.
|
|
|
|
:param float longitude: Longitude of the location.
|
|
|
|
:param str place: Name for the location.
|
|
|
|
:param bool write: If true, write the location db to disk.
|
|
|
|
"""
|
|
|
|
data = {}
|
|
|
|
data['lat'] = latitude
|
|
|
|
data['long'] = longitude
|
|
|
|
data['name'] = place
|
|
|
|
self.location_db.append(data)
|
|
|
|
if(write is True):
|
|
|
|
self.update_location_db()
|
|
|
|
|
|
|
|
def backup_hash_db(self):
|
|
|
|
"""Backs up the hash db."""
|
2021-06-12 19:49:29 +02:00
|
|
|
# TODO
|
|
|
|
if os.path.isfile(self.hash_db_file):
|
2016-12-13 08:54:33 +01:00
|
|
|
mask = strftime('%Y-%m-%d_%H-%M-%S')
|
2021-06-12 19:49:29 +02:00
|
|
|
backup_file_name = '%s-%s' % (self.hash_db_file, mask)
|
|
|
|
copyfile(self.hash_db_file, backup_file_name)
|
2016-12-13 08:54:33 +01:00
|
|
|
return backup_file_name
|
|
|
|
|
2015-10-10 09:02:53 +02:00
|
|
|
def check_hash(self, key):
|
2016-01-08 23:49:06 +01:00
|
|
|
"""Check whether a hash is present for the given key.
|
|
|
|
|
|
|
|
:param str key:
|
|
|
|
:returns: bool
|
|
|
|
"""
|
2015-10-10 09:02:53 +02:00
|
|
|
return key in self.hash_db
|
|
|
|
|
2016-12-13 08:54:33 +01:00
|
|
|
def get_hash(self, key):
|
|
|
|
"""Get the hash value for a given key.
|
2016-01-08 23:49:06 +01:00
|
|
|
|
2016-12-13 08:54:33 +01:00
|
|
|
:param str key:
|
|
|
|
:returns: str or None
|
2016-01-08 23:49:06 +01:00
|
|
|
"""
|
2016-12-13 08:54:33 +01:00
|
|
|
if(self.check_hash(key) is True):
|
|
|
|
return self.hash_db[key]
|
|
|
|
return None
|
2015-12-24 20:39:28 +01:00
|
|
|
|
2016-01-02 08:23:06 +01:00
|
|
|
def get_location_name(self, latitude, longitude, threshold_m):
|
2016-01-08 23:49:06 +01:00
|
|
|
"""Find a name for a location in the database.
|
|
|
|
|
|
|
|
:param float latitude: Latitude of the location.
|
|
|
|
:param float longitude: Longitude of the location.
|
|
|
|
:param int threshold_m: Location in the database must be this close to
|
|
|
|
the given latitude and longitude.
|
|
|
|
:returns: str, or None if a matching location couldn't be found.
|
|
|
|
"""
|
2016-03-12 20:09:28 +01:00
|
|
|
last_d = sys.maxsize
|
2015-12-29 09:07:50 +01:00
|
|
|
name = None
|
2015-12-24 20:39:28 +01:00
|
|
|
for data in self.location_db:
|
2016-01-08 23:49:06 +01:00
|
|
|
# As threshold is quite small use simple math
|
2016-01-02 08:23:06 +01:00
|
|
|
# From http://stackoverflow.com/questions/15736995/how-can-i-quickly-estimate-the-distance-between-two-latitude-longitude-points # noqa
|
2015-12-24 20:39:28 +01:00
|
|
|
# convert decimal degrees to radians
|
|
|
|
|
2016-03-12 20:09:28 +01:00
|
|
|
lon1, lat1, lon2, lat2 = list(map(
|
2016-01-02 08:23:06 +01:00
|
|
|
radians,
|
|
|
|
[longitude, latitude, data['long'], data['lat']]
|
2016-03-12 20:09:28 +01:00
|
|
|
))
|
2015-12-24 20:39:28 +01:00
|
|
|
|
2016-01-02 18:34:43 +01:00
|
|
|
r = 6371000 # radius of the earth in m
|
|
|
|
x = (lon2 - lon1) * cos(0.5 * (lat2 + lat1))
|
2015-12-24 20:39:28 +01:00
|
|
|
y = lat2 - lat1
|
2016-01-02 18:34:43 +01:00
|
|
|
d = r * sqrt(x * x + y * y)
|
2015-12-24 20:39:28 +01:00
|
|
|
# Use if closer then threshold_km reuse lookup
|
2015-12-29 09:07:50 +01:00
|
|
|
if(d <= threshold_m and d < last_d):
|
2016-01-02 08:23:06 +01:00
|
|
|
name = data['name']
|
2015-12-29 09:07:50 +01:00
|
|
|
last_d = d
|
2015-12-24 20:39:28 +01:00
|
|
|
|
2015-12-29 09:07:50 +01:00
|
|
|
return name
|
|
|
|
|
|
|
|
def get_location_coordinates(self, name):
|
2016-01-08 23:49:06 +01:00
|
|
|
"""Get the latitude and longitude for a location.
|
|
|
|
|
|
|
|
:param str name: Name of the location.
|
|
|
|
:returns: tuple(float), or None if the location wasn't in the database.
|
|
|
|
"""
|
2015-12-29 09:07:50 +01:00
|
|
|
for data in self.location_db:
|
|
|
|
if data['name'] == name:
|
|
|
|
return (data['lat'], data['long'])
|
|
|
|
|
|
|
|
return None
|
2015-12-24 20:39:28 +01:00
|
|
|
|
2016-12-14 07:27:12 +01:00
|
|
|
def all(self):
|
|
|
|
"""Generator to get all entries from self.hash_db
|
|
|
|
|
|
|
|
:returns tuple(string)
|
|
|
|
"""
|
|
|
|
for checksum, path in self.hash_db.items():
|
|
|
|
yield (checksum, path)
|
|
|
|
|
2016-12-13 08:54:33 +01:00
|
|
|
def reset_hash_db(self):
|
|
|
|
self.hash_db = {}
|
|
|
|
|
|
|
|
def update_hash_db(self):
|
|
|
|
"""Write the hash db to disk."""
|
2021-06-12 19:49:29 +02:00
|
|
|
with open(self.hash_db_file, 'w') as f:
|
2016-12-13 08:54:33 +01:00
|
|
|
json.dump(self.hash_db, f)
|
|
|
|
|
2015-12-24 20:39:28 +01:00
|
|
|
def update_location_db(self):
|
2016-01-08 23:49:06 +01:00
|
|
|
"""Write the location db to disk."""
|
2021-06-12 19:49:29 +02:00
|
|
|
with open(self.location_db_file, 'w') as f:
|
2015-12-24 20:39:28 +01:00
|
|
|
json.dump(self.location_db, f)
|