|
1
|
|
|
from __future__ import division |
|
2
|
|
|
from __future__ import unicode_literals |
|
3
|
|
|
from builtins import range |
|
4
|
|
|
from past.utils import old_div |
|
5
|
|
|
import hashlib |
|
6
|
|
|
import os |
|
7
|
|
|
import random |
|
8
|
|
|
import string |
|
9
|
|
|
import tempfile |
|
10
|
|
|
import re |
|
11
|
|
|
import time |
|
12
|
|
|
import urllib |
|
13
|
|
|
|
|
14
|
|
|
from datetime import datetime |
|
15
|
|
|
from datetime import timedelta |
|
16
|
|
|
|
|
17
|
|
|
from elodie.compatability import _rename |
|
18
|
|
|
from elodie.external.pyexiftool import ExifTool |
|
19
|
|
|
from elodie.dependencies import get_exiftool |
|
20
|
|
|
from elodie import constants |
|
21
|
|
|
|
|
22
|
|
View Code Duplication |
def checksum(file_path, blocksize=65536): |
|
|
|
|
|
|
23
|
|
|
hasher = hashlib.sha256() |
|
24
|
|
|
with open(file_path, 'rb') as f: |
|
25
|
|
|
buf = f.read(blocksize) |
|
26
|
|
|
|
|
27
|
|
|
while len(buf) > 0: |
|
28
|
|
|
hasher.update(buf) |
|
29
|
|
|
buf = f.read(blocksize) |
|
30
|
|
|
return hasher.hexdigest() |
|
31
|
|
|
return None |
|
32
|
|
|
|
|
33
|
|
|
def create_working_folder(format=None): |
|
34
|
|
|
temporary_folder = tempfile.gettempdir() |
|
35
|
|
|
folder = os.path.join(temporary_folder, random_string(10, format), random_string(10, format)) |
|
36
|
|
|
os.makedirs(folder) |
|
37
|
|
|
|
|
38
|
|
|
return (temporary_folder, folder) |
|
39
|
|
|
|
|
40
|
|
|
def download_file(name, destination): |
|
41
|
|
|
try: |
|
42
|
|
|
url_to_file = 'https://s3.amazonaws.com/jmathai/github/elodie/{}'.format(name) |
|
43
|
|
|
# urlretrieve works differently for python 2 and 3 |
|
44
|
|
|
if constants.python_version < 3: |
|
45
|
|
|
final_name = '{}/{}{}'.format(destination, random_string(10), os.path.splitext(name)[1]) |
|
46
|
|
|
urllib.urlretrieve( |
|
47
|
|
|
url_to_file, |
|
48
|
|
|
final_name |
|
49
|
|
|
) |
|
50
|
|
|
else: |
|
51
|
|
|
final_name, headers = urllib.request.urlretrieve(url_to_file) |
|
52
|
|
|
return final_name |
|
53
|
|
|
except Exception as e: |
|
54
|
|
|
return False |
|
55
|
|
|
|
|
56
|
|
|
def get_file(name): |
|
57
|
|
|
file_path = get_file_path(name) |
|
58
|
|
|
if not os.path.isfile(file_path): |
|
59
|
|
|
return False |
|
60
|
|
|
|
|
61
|
|
|
return file_path |
|
62
|
|
|
|
|
63
|
|
|
def get_file_path(name): |
|
64
|
|
|
current_folder = os.path.dirname(os.path.realpath(__file__)) |
|
65
|
|
|
return os.path.join(current_folder, 'files', name) |
|
66
|
|
|
|
|
67
|
|
|
def get_test_location(): |
|
68
|
|
|
return (61.013710, 99.196656, 'Siberia') |
|
69
|
|
|
|
|
70
|
|
|
def populate_folder(number_of_files, include_invalid=False): |
|
71
|
|
|
folder = '%s/%s' % (tempfile.gettempdir(), random_string(10)) |
|
72
|
|
|
os.makedirs(folder) |
|
73
|
|
|
|
|
74
|
|
|
for x in range(0, number_of_files): |
|
75
|
|
|
ext = 'jpg' if x % 2 == 0 else 'txt' |
|
76
|
|
|
fname = '%s/%s.%s' % (folder, x, ext) |
|
77
|
|
|
with open(fname, 'a'): |
|
78
|
|
|
os.utime(fname, None) |
|
79
|
|
|
|
|
80
|
|
|
if include_invalid: |
|
81
|
|
|
fname = '%s/%s' % (folder, 'invalid.invalid') |
|
82
|
|
|
with open(fname, 'a'): |
|
83
|
|
|
os.utime(fname, None) |
|
84
|
|
|
|
|
85
|
|
|
return folder |
|
86
|
|
|
|
|
87
|
|
|
def random_string(length, format=None): |
|
88
|
|
|
format_choice = string.ascii_uppercase + string.digits |
|
89
|
|
|
if format == 'int': |
|
90
|
|
|
format_choice = string.digits |
|
91
|
|
|
elif format == 'str': |
|
92
|
|
|
format_choice = string.asci_uppercase |
|
93
|
|
|
|
|
94
|
|
|
return ''.join(random.SystemRandom().choice(format_choice) for _ in range(length)) |
|
95
|
|
|
|
|
96
|
|
|
def random_decimal(): |
|
97
|
|
|
return random.random() |
|
98
|
|
|
|
|
99
|
|
|
def random_coordinate(coordinate, precision): |
|
100
|
|
|
# Here we add to the decimal section of the coordinate by a given precision |
|
101
|
|
|
return coordinate + ((old_div(10.0, (10.0**precision))) * random_decimal()) |
|
102
|
|
|
|
|
103
|
|
|
def temp_dir(): |
|
104
|
|
|
return tempfile.gettempdir() |
|
105
|
|
|
|
|
106
|
|
|
def is_windows(): |
|
107
|
|
|
return os.name == 'nt' |
|
108
|
|
|
|
|
109
|
|
|
# path_tz_fix(file_name) |
|
110
|
|
|
# Change timestamp in file_name by the offset |
|
111
|
|
|
# between UTC and local time, i.e. |
|
112
|
|
|
# 2015-12-05_00-59-26-with-title-some-title.jpg -> |
|
113
|
|
|
# 2015-12-04_20-59-26-with-title-some-title.jpg |
|
114
|
|
|
# (Windows only) |
|
115
|
|
|
|
|
116
|
|
|
def path_tz_fix(file_name): |
|
117
|
|
|
if is_windows(): |
|
118
|
|
|
# Calculate the offset between UTC and local time |
|
119
|
|
|
tz_shift = old_div((datetime.fromtimestamp(0) - |
|
120
|
|
|
datetime.utcfromtimestamp(0)).seconds,3600) |
|
121
|
|
|
# replace timestamp in file_name |
|
122
|
|
|
m = re.search('(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2})',file_name) |
|
123
|
|
|
t_date = datetime.fromtimestamp(time.mktime(time.strptime(m.group(0), '%Y-%m-%d_%H-%M-%S'))) |
|
124
|
|
|
s_date_fix = (t_date-timedelta(hours=tz_shift)).strftime('%Y-%m-%d_%H-%M-%S') |
|
125
|
|
|
return re.sub('\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}',s_date_fix,file_name) |
|
126
|
|
|
else: |
|
127
|
|
|
return file_name |
|
128
|
|
|
|
|
129
|
|
|
# time_convert(s_time) |
|
130
|
|
|
# Change s_time (struct_time) by the offset |
|
131
|
|
|
# between UTC and local time |
|
132
|
|
|
# (Windows only) |
|
133
|
|
|
|
|
134
|
|
|
def time_convert(s_time): |
|
135
|
|
|
if is_windows(): |
|
136
|
|
|
return time.gmtime((time.mktime(s_time))) |
|
137
|
|
|
else: |
|
138
|
|
|
return s_time |
|
139
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
# isclose(a,b,rel_tol) |
|
142
|
|
|
# To compare float coordinates a and b |
|
143
|
|
|
# with relative tolerance c |
|
144
|
|
|
|
|
145
|
|
|
def isclose(a, b, rel_tol = 1e-8): |
|
146
|
|
|
if not isinstance(a, (int, float)) or not isinstance(b, (int, float)): |
|
147
|
|
|
return False |
|
148
|
|
|
|
|
149
|
|
|
diff = abs(a - b) |
|
150
|
|
|
return (diff <= abs(rel_tol * a) and |
|
151
|
|
|
diff <= abs(rel_tol * b)) |
|
152
|
|
|
|
|
153
|
|
|
def reset_dbs(): |
|
154
|
|
|
""" Back up hash_db and location_db """ |
|
155
|
|
|
# This is no longer needed. See gh-322 |
|
156
|
|
|
# https://github.com/jmathai/elodie/issues/322 |
|
157
|
|
|
pass |
|
158
|
|
|
|
|
159
|
|
|
def restore_dbs(): |
|
160
|
|
|
""" Restore back ups of hash_db and location_db """ |
|
161
|
|
|
# This is no longer needed. See gh-322 |
|
162
|
|
|
# https://github.com/jmathai/elodie/issues/322 |
|
163
|
|
|
pass |
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
def setup_module(): |
|
167
|
|
|
exiftool_addedargs = [ |
|
168
|
|
|
u'-config', |
|
169
|
|
|
u'"{}"'.format(constants.exiftool_config) |
|
170
|
|
|
] |
|
171
|
|
|
ExifTool(executable_=get_exiftool(), addedargs=exiftool_addedargs).start() |
|
172
|
|
|
|
|
173
|
|
|
def teardown_module(): |
|
174
|
|
|
ExifTool().terminate |
|
175
|
|
|
|