Accepted-Language is moved to constants. TZ fix functions is moved to helper
This commit is contained in:
parent
c9618311fe
commit
0d154d67c9
|
@ -21,3 +21,6 @@ script_directory = path.dirname(path.dirname(path.abspath(__file__)))
|
||||||
|
|
||||||
#: Path to Elodie's ExifTool config file.
|
#: Path to Elodie's ExifTool config file.
|
||||||
exiftool_config = path.join(script_directory,'configs','ExifTool_config')
|
exiftool_config = path.join(script_directory,'configs','ExifTool_config')
|
||||||
|
|
||||||
|
#: Accepted language in responses from MapQuest
|
||||||
|
accepted_language = 'en'
|
||||||
|
|
|
@ -161,7 +161,7 @@ def reverse_lookup(lat, lon):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
params = {'format': 'json', 'key': key, 'lat': lat, 'lon': lon}
|
params = {'format': 'json', 'key': key, 'lat': lat, 'lon': lon}
|
||||||
headers = {"Accept-Language": "en-US,en;q=0.5"}
|
headers = {"Accept-Language": constants.accepted_language}
|
||||||
r = requests.get(
|
r = requests.get(
|
||||||
'http://open.mapquestapi.com/nominatim/v1/reverse.php?%s' %
|
'http://open.mapquestapi.com/nominatim/v1/reverse.php?%s' %
|
||||||
urllib.urlencode(params),headers=headers
|
urllib.urlencode(params),headers=headers
|
||||||
|
|
|
@ -19,18 +19,6 @@ from nose.plugins.skip import SkipTest
|
||||||
|
|
||||||
os.environ['TZ'] = 'GMT'
|
os.environ['TZ'] = 'GMT'
|
||||||
|
|
||||||
if os.name == 'nt':
|
|
||||||
tz_shift = (datetime.fromtimestamp(0) -
|
|
||||||
datetime.utcfromtimestamp(0)).seconds/3600
|
|
||||||
else:
|
|
||||||
tz_shift = 0
|
|
||||||
|
|
||||||
def path_tz_fix(s_path):
|
|
||||||
#some_prefix2015-12-05_00-59-26-with-title-some-title.jpg
|
|
||||||
m = re.search('(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2})',s_path)
|
|
||||||
t_date = datetime.fromtimestamp(time.mktime(time.strptime(m.group(0), '%Y-%m-%d_%H-%M-%S')))
|
|
||||||
s_date_fix = (t_date-timedelta(hours=tz_shift)).strftime('%Y-%m-%d_%H-%M-%S')
|
|
||||||
return re.sub('\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}',s_date_fix,s_path)
|
|
||||||
|
|
||||||
def test_create_directory_success():
|
def test_create_directory_success():
|
||||||
filesystem = FileSystem()
|
filesystem = FileSystem()
|
||||||
|
@ -144,14 +132,14 @@ def test_get_file_name_plain():
|
||||||
media = Photo(helper.get_file('plain.jpg'))
|
media = Photo(helper.get_file('plain.jpg'))
|
||||||
file_name = filesystem.get_file_name(media)
|
file_name = filesystem.get_file_name(media)
|
||||||
|
|
||||||
assert file_name == path_tz_fix('2015-12-05_00-59-26-plain.jpg'), file_name
|
assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-plain.jpg'), file_name
|
||||||
|
|
||||||
def test_get_file_name_with_title():
|
def test_get_file_name_with_title():
|
||||||
filesystem = FileSystem()
|
filesystem = FileSystem()
|
||||||
media = Photo(helper.get_file('with-title.jpg'))
|
media = Photo(helper.get_file('with-title.jpg'))
|
||||||
file_name = filesystem.get_file_name(media)
|
file_name = filesystem.get_file_name(media)
|
||||||
|
|
||||||
assert file_name == path_tz_fix('2015-12-05_00-59-26-with-title-some-title.jpg'), file_name
|
assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-with-title-some-title.jpg'), file_name
|
||||||
|
|
||||||
def test_get_folder_name_by_date():
|
def test_get_folder_name_by_date():
|
||||||
filesystem = FileSystem()
|
filesystem = FileSystem()
|
||||||
|
@ -211,7 +199,7 @@ def test_process_file_plain():
|
||||||
|
|
||||||
assert origin_checksum is not None, origin_checksum
|
assert origin_checksum is not None, origin_checksum
|
||||||
assert origin_checksum == destination_checksum, destination_checksum
|
assert origin_checksum == destination_checksum, destination_checksum
|
||||||
assert path_tz_fix(os.path.join('2015-12-Dec','Unknown Location','2015-12-05_00-59-26-photo.jpg')) in destination, destination
|
assert helper.path_tz_fix(os.path.join('2015-12-Dec','Unknown Location','2015-12-05_00-59-26-photo.jpg')) in destination, destination
|
||||||
|
|
||||||
def test_process_file_with_title():
|
def test_process_file_with_title():
|
||||||
filesystem = FileSystem()
|
filesystem = FileSystem()
|
||||||
|
@ -231,7 +219,7 @@ def test_process_file_with_title():
|
||||||
|
|
||||||
assert origin_checksum is not None, origin_checksum
|
assert origin_checksum is not None, origin_checksum
|
||||||
assert origin_checksum == destination_checksum, destination_checksum
|
assert origin_checksum == destination_checksum, destination_checksum
|
||||||
assert path_tz_fix(os.path.join('2015-12-Dec','Unknown Location','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination
|
assert helper.path_tz_fix(os.path.join('2015-12-Dec','Unknown Location','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination
|
||||||
|
|
||||||
def test_process_file_with_location():
|
def test_process_file_with_location():
|
||||||
filesystem = FileSystem()
|
filesystem = FileSystem()
|
||||||
|
@ -251,7 +239,7 @@ def test_process_file_with_location():
|
||||||
|
|
||||||
assert origin_checksum is not None, origin_checksum
|
assert origin_checksum is not None, origin_checksum
|
||||||
assert origin_checksum == destination_checksum, destination_checksum
|
assert origin_checksum == destination_checksum, destination_checksum
|
||||||
assert path_tz_fix(os.path.join('2015-12-Dec','Sunnyvale','2015-12-05_00-59-26-photo.jpg')) in destination, destination
|
assert helper.path_tz_fix(os.path.join('2015-12-Dec','Sunnyvale','2015-12-05_00-59-26-photo.jpg')) in destination, destination
|
||||||
|
|
||||||
def test_process_file_with_location_and_title():
|
def test_process_file_with_location_and_title():
|
||||||
filesystem = FileSystem()
|
filesystem = FileSystem()
|
||||||
|
@ -271,7 +259,7 @@ def test_process_file_with_location_and_title():
|
||||||
|
|
||||||
assert origin_checksum is not None, origin_checksum
|
assert origin_checksum is not None, origin_checksum
|
||||||
assert origin_checksum == destination_checksum, destination_checksum
|
assert origin_checksum == destination_checksum, destination_checksum
|
||||||
assert path_tz_fix(os.path.join('2015-12-Dec','Sunnyvale','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination
|
assert helper.path_tz_fix(os.path.join('2015-12-Dec','Sunnyvale','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination
|
||||||
|
|
||||||
def test_process_file_with_album():
|
def test_process_file_with_album():
|
||||||
filesystem = FileSystem()
|
filesystem = FileSystem()
|
||||||
|
@ -291,7 +279,7 @@ def test_process_file_with_album():
|
||||||
|
|
||||||
assert origin_checksum is not None, origin_checksum
|
assert origin_checksum is not None, origin_checksum
|
||||||
assert origin_checksum == destination_checksum, destination_checksum
|
assert origin_checksum == destination_checksum, destination_checksum
|
||||||
assert path_tz_fix(os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo.jpg')) in destination, destination
|
assert helper.path_tz_fix(os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo.jpg')) in destination, destination
|
||||||
|
|
||||||
def test_process_file_with_album_and_title():
|
def test_process_file_with_album_and_title():
|
||||||
filesystem = FileSystem()
|
filesystem = FileSystem()
|
||||||
|
@ -311,7 +299,7 @@ def test_process_file_with_album_and_title():
|
||||||
|
|
||||||
assert origin_checksum is not None, origin_checksum
|
assert origin_checksum is not None, origin_checksum
|
||||||
assert origin_checksum == destination_checksum, destination_checksum
|
assert origin_checksum == destination_checksum, destination_checksum
|
||||||
assert path_tz_fix(os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination
|
assert helper.path_tz_fix(os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination
|
||||||
|
|
||||||
def test_process_file_with_album_and_title_and_location():
|
def test_process_file_with_album_and_title_and_location():
|
||||||
filesystem = FileSystem()
|
filesystem = FileSystem()
|
||||||
|
@ -331,4 +319,4 @@ def test_process_file_with_album_and_title_and_location():
|
||||||
|
|
||||||
assert origin_checksum is not None, origin_checksum
|
assert origin_checksum is not None, origin_checksum
|
||||||
assert origin_checksum == destination_checksum, destination_checksum
|
assert origin_checksum == destination_checksum, destination_checksum
|
||||||
assert path_tz_fix(os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination
|
assert helper.path_tz_fix(os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination
|
||||||
|
|
|
@ -3,6 +3,11 @@ import os
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import re
|
||||||
|
import time
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
def checksum(file_path, blocksize=65536):
|
def checksum(file_path, blocksize=65536):
|
||||||
hasher = hashlib.sha256()
|
hasher = hashlib.sha256()
|
||||||
|
@ -53,3 +58,37 @@ def random_coordinate(coordinate, precision):
|
||||||
|
|
||||||
def temp_dir():
|
def temp_dir():
|
||||||
return tempfile.gettempdir()
|
return tempfile.gettempdir()
|
||||||
|
|
||||||
|
def is_windows():
|
||||||
|
return os.name == 'nt'
|
||||||
|
|
||||||
|
# path_tz_fix(file_name)
|
||||||
|
# Change timestamp in file_name by the offset
|
||||||
|
# between UTC and local time, i.e.
|
||||||
|
# 2015-12-05_00-59-26-with-title-some-title.jpg ->
|
||||||
|
# 2015-12-04_20-59-26-with-title-some-title.jpg
|
||||||
|
# (Windows only)
|
||||||
|
|
||||||
|
def path_tz_fix(file_name):
|
||||||
|
if is_windows():
|
||||||
|
# Calculate the offset between UTC and local time
|
||||||
|
tz_shift = (datetime.fromtimestamp(0) -
|
||||||
|
datetime.utcfromtimestamp(0)).seconds/3600
|
||||||
|
# replace timestamp in file_name
|
||||||
|
m = re.search('(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2})',file_name)
|
||||||
|
t_date = datetime.fromtimestamp(time.mktime(time.strptime(m.group(0), '%Y-%m-%d_%H-%M-%S')))
|
||||||
|
s_date_fix = (t_date-timedelta(hours=tz_shift)).strftime('%Y-%m-%d_%H-%M-%S')
|
||||||
|
return re.sub('\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}',s_date_fix,file_name)
|
||||||
|
else:
|
||||||
|
return file_name
|
||||||
|
|
||||||
|
# time_convert(s_time)
|
||||||
|
# Change s_time (struct_time) by the offset
|
||||||
|
# between UTC and local time
|
||||||
|
# (Windows only)
|
||||||
|
|
||||||
|
def time_convert(s_time):
|
||||||
|
if is_windows():
|
||||||
|
return time.gmtime((time.mktime(s_time)))
|
||||||
|
else:
|
||||||
|
return s_time
|
||||||
|
|
|
@ -4,7 +4,6 @@ import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from datetime import timedelta
|
|
||||||
import shutil
|
import shutil
|
||||||
import tempfile
|
import tempfile
|
||||||
import time
|
import time
|
||||||
|
@ -19,14 +18,6 @@ from elodie.media.media import Media
|
||||||
from elodie.media.photo import Photo
|
from elodie.media.photo import Photo
|
||||||
|
|
||||||
os.environ['TZ'] = 'GMT'
|
os.environ['TZ'] = 'GMT'
|
||||||
if os.name == 'nt':
|
|
||||||
tz_shift = (datetime.fromtimestamp(0) -
|
|
||||||
datetime.utcfromtimestamp(0)).seconds/3600
|
|
||||||
else:
|
|
||||||
tz_shift = 0
|
|
||||||
|
|
||||||
def time_convert(s_time):
|
|
||||||
return datetime.fromtimestamp(time.mktime(s_time))
|
|
||||||
|
|
||||||
def test_photo_extensions():
|
def test_photo_extensions():
|
||||||
photo = Photo()
|
photo = Photo()
|
||||||
|
@ -105,7 +96,7 @@ def test_get_date_taken():
|
||||||
date_taken = photo.get_date_taken()
|
date_taken = photo.get_date_taken()
|
||||||
|
|
||||||
# assert date_taken == (2015, 12, 5, 0, 59, 26, 5, 339, 0), date_taken
|
# assert date_taken == (2015, 12, 5, 0, 59, 26, 5, 339, 0), date_taken
|
||||||
assert time_convert(date_taken) == time_convert((2015, 12, 5, 0, 59, 26, 5, 339, 0)) - timedelta(hours = tz_shift), date_taken
|
assert date_taken == helper.time_convert((2015, 12, 5, 0, 59, 26, 5, 339, 0)), date_taken
|
||||||
|
|
||||||
def test_get_date_taken_without_exif():
|
def test_get_date_taken_without_exif():
|
||||||
source = helper.get_file('no-exif.jpg')
|
source = helper.get_file('no-exif.jpg')
|
||||||
|
@ -147,7 +138,7 @@ def test_set_date_taken_with_missing_datetimeoriginal():
|
||||||
shutil.rmtree(folder)
|
shutil.rmtree(folder)
|
||||||
|
|
||||||
#assert date_taken == (2013, 9, 30, 7, 6, 5, 0, 273, 0), metadata['date_taken']
|
#assert date_taken == (2013, 9, 30, 7, 6, 5, 0, 273, 0), metadata['date_taken']
|
||||||
assert time_convert(date_taken) == time_convert((2013, 9, 30, 7, 6, 5, 0, 273, 0)) - timedelta(hours = tz_shift), metadata['date_taken']
|
assert date_taken == helper.time_convert((2013, 9, 30, 7, 6, 5, 0, 273, 0)), metadata['date_taken']
|
||||||
|
|
||||||
def test_set_date_taken():
|
def test_set_date_taken():
|
||||||
temporary_folder, folder = helper.create_working_folder()
|
temporary_folder, folder = helper.create_working_folder()
|
||||||
|
@ -168,7 +159,7 @@ def test_set_date_taken():
|
||||||
shutil.rmtree(folder)
|
shutil.rmtree(folder)
|
||||||
|
|
||||||
#assert date_taken == (2013, 9, 30, 7, 6, 5, 0, 273, 0), metadata['date_taken']
|
#assert date_taken == (2013, 9, 30, 7, 6, 5, 0, 273, 0), metadata['date_taken']
|
||||||
assert time_convert(date_taken) == time_convert((2013, 9, 30, 7, 6, 5, 0, 273, 0)) - timedelta(hours = tz_shift), metadata['date_taken']
|
assert date_taken == helper.time_convert((2013, 9, 30, 7, 6, 5, 0, 273, 0)), metadata['date_taken']
|
||||||
|
|
||||||
def test_set_location():
|
def test_set_location():
|
||||||
raise SkipTest('gh-31, precision is lost in conversion from decimal to dms')
|
raise SkipTest('gh-31, precision is lost in conversion from decimal to dms')
|
||||||
|
|
Loading…
Reference in New Issue