Skip to content

Commit

Permalink
Merge branch 'main' into unique_filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarons authored Oct 5, 2023
2 parents bd929f0 + 8246547 commit 9fe2011
Show file tree
Hide file tree
Showing 28 changed files with 1,942 additions and 47 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/close-stale.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ jobs:
steps:
- uses: actions/stale@v4
with:
stale-issue-message: "This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days."
days-before-stale: 30
stale-issue-message: "This issue is stale because it has been open 1 year with no activity. Remove stale label or comment or this will be closed in 5 days."
days-before-stale: 365
days-before-close: 5
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ photos:
remove_obsolete: false
sync_interval: 500
all_albums: false # Optional, default false. If true preserve album structure. If same photo is in multpile albums creates duplicates on filesystem
# Optional, default false. If true all files have globally unique filenames, it should matter when all_albums is true and one photo is in pultpile album
unique_filenames: false
unique_filenames: false # Optional, default false. If true all files have globally unique filenames, it should matter when filter > albums is empty and one photo is in multiple album
filters:
# if all_albums is false list of albums to download, if all_albums is true list of ignored albums
# if empty and all_albums is false download all photos to "all" folder. if empty and all_albums is true download all folders
Expand Down
3 changes: 3 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ photos:
destination: "photos"
remove_obsolete: false
sync_interval: 500
all_albums: false # Optional, default false. If true preserve album structure. If same photo is in multpile albums creates duplicates on filesystem
filters:
# if all_albums is false list of albums to download, if all_albums is true list of ignored albums
# if empty and all_albums is false download all photos to "all" folder. if empty and all_albums is true download all folders
albums:
- "album 1"
- "album2"
Expand Down
1 change: 0 additions & 1 deletion src/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ def get_photos_unique_file_names(config):
LOGGER.info("Using unique filenames.")
return unique_filenames


def prepare_root_destination(config):
"""Prepare root destination."""
LOGGER.debug("Checking root destination ...")
Expand Down
35 changes: 11 additions & 24 deletions src/sync_drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import re
import time
import unicodedata
import zipfile
from pathlib import Path
from shutil import copyfileobj, rmtree, unpack_archive
from shutil import copyfileobj, rmtree

import magic
from icloudpy import exceptions
Expand Down Expand Up @@ -153,11 +154,11 @@ def process_package(local_file):
archive_file += ".zip"
os.rename(local_file, archive_file)
LOGGER.info(f"Unpacking {archive_file} to {os.path.dirname(archive_file)}")
unpack_archive(filename=archive_file, extract_dir=os.path.dirname(archive_file))
try:
os.rename(unicodedata.normalize("NFD", local_file), local_file)
except Exception as e:
LOGGER.warning("Normalizing failed - " + str(e))
zipfile.ZipFile(archive_file).extractall(path=os.path.dirname(archive_file))
normalized_path = unicodedata.normalize("NFD", local_file)
if normalized_path is not local_file:
os.rename(local_file, normalized_path)
local_file = normalized_path
os.remove(archive_file)
elif "application/gzip" == magic_object.from_file(filename=local_file):
archive_file += ".gz"
Expand All @@ -174,7 +175,7 @@ def process_package(local_file):
)
return False
LOGGER.info(f"Successfully unpacked the package {archive_file}.")
return True
return local_file


def is_package(item):
Expand All @@ -195,13 +196,13 @@ def download_file(item, local_file):
with open(local_file, "wb") as file_out:
copyfileobj(response.raw, file_out)
if response.url and "/packageDownload?" in response.url:
process_package(local_file=local_file)
local_file = process_package(local_file=local_file)
item_modified_time = time.mktime(item.date_modified.timetuple())
os.utime(local_file, (item_modified_time, item_modified_time))
except (exceptions.ICloudPyAPIResponseException, FileNotFoundError, Exception) as e:
LOGGER.error(f"Failed to download {local_file}: {str(e)}")
return False
return True
return local_file


def process_file(item, destination_path, filters, ignore, files):
Expand All @@ -221,21 +222,7 @@ def process_file(item, destination_path, filters, ignore, files):
return False
elif file_exists(item=item, local_file=local_file):
return False
download_file(item=item, local_file=local_file)
if item_is_package:
for f in Path(local_file).glob("**/*"):
f = str(f)
f_normalized = unicodedata.normalize("NFC", f)
try:
os.rename(f, f_normalized)
except Exception as e:
LOGGER.warning("Normalizing failed - " + str(e))
f_dir = os.path.dirname(f)
# delete empty folder if any after normalization
with os.scandir(f_dir) as it:
if not any(it):
os.rmdir(f_dir)
files.add(f_normalized)
local_file = download_file(item=item, local_file=local_file)
return True


Expand Down
2 changes: 0 additions & 2 deletions src/sync_photos.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,6 @@ def sync_photos(config, photos):
unique_file_names = config_parser.get_photos_unique_file_names(config=config)
if download_all:
for album in photos.albums.keys():
if album in iter(filters["albums"]):
continue
sync_album(
album=photos.albums[album],
destination_path=os.path.join(destination_path, album),
Expand Down
Binary file added tests/data/Fotoksiążka-Wzór.xmcf.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 9fe2011

Please sign in to comment.