diff --git a/.gitignore b/.gitignore index 417507f3..f57d06c5 100644 --- a/.gitignore +++ b/.gitignore @@ -103,7 +103,6 @@ celerybeat.pid *.sage.py # Environments -.env .venv env/ venv/ diff --git a/src/notify.py b/src/notify.py index d6628224..11342181 100644 --- a/src/notify.py +++ b/src/notify.py @@ -78,13 +78,16 @@ def notify_discord(config, message, last_send=None, dry_run=False): return sent_on -def send(config, username, last_send=None, dry_run=False): +def send(config, username, last_send=None, dry_run=False, region="global"): """Send notifications.""" sent_on = None + region_opt = "" + if region != "global": + region_opt = f"--region={region} " message = f"""Two-step authentication for iCloud Drive, Photos (Docker) is required. Please login to your server and authenticate. Please run - `docker exec -it --user=abc icloud /bin/sh -c - "icloud --session-directory=/config/session_data --username={username}"`.""" + "icloud --session-directory=/config/session_data {region_opt}--username={username}"`.""" subject = f"icloud-docker: Two step authentication is required for {username}" notify_telegram(config=config, message=message, last_send=last_send, dry_run=dry_run) notify_discord(config=config, message=message, last_send=last_send, dry_run=dry_run) diff --git a/src/sync.py b/src/sync.py index 98b0fd26..1e5d409d 100644 --- a/src/sync.py +++ b/src/sync.py @@ -36,7 +36,7 @@ def get_api_instance( apple_id=username, password=password, cookie_directory=cookie_directory, - auth_endpoint="https://www.icloud.com.cn", + home_endpoint="https://www.icloud.com.cn", setup_endpoint="https://setup.icloud.com.cn/setup/ws/1", ) if server_region == "china" @@ -63,12 +63,12 @@ def sync(): username = config_parser.get_username(config=config) if username: try: + server_region = config_parser.get_region(config=config) if ENV_ICLOUD_PASSWORD_KEY in os.environ: password = os.environ.get(ENV_ICLOUD_PASSWORD_KEY) utils.store_password_in_keyring(username=username, password=password) else: password = utils.get_password_from_keyring(username=username) - server_region = config_parser.get_region(config=config) api = get_api_instance(username=username, password=password, server_region=server_region) if not api.requires_2sa: if "drive" in config and enable_sync_drive: @@ -92,7 +92,7 @@ def sync(): break next_sync = (datetime.datetime.now() + datetime.timedelta(seconds=sleep_for)).strftime("%c") LOGGER.info(f"Retrying login at {next_sync} ...") - last_send = notify.send(config=config, username=username, last_send=last_send) + last_send = notify.send(config=config, username=username, last_send=last_send, region=server_region) sleep(sleep_for) continue except exceptions.ICloudPyNoStoredPasswordAvailableException: @@ -103,7 +103,7 @@ def sync(): break next_sync = (datetime.datetime.now() + datetime.timedelta(seconds=sleep_for)).strftime("%c") LOGGER.info(f"Retrying login at {next_sync} ...") - last_send = notify.send(config=config, username=username, last_send=last_send) + last_send = notify.send(config=config, username=username, last_send=last_send, region=server_region) sleep(sleep_for) continue diff --git a/tests/.env b/tests/.env new file mode 100644 index 00000000..c02d47b3 --- /dev/null +++ b/tests/.env @@ -0,0 +1 @@ +ENV_CONFIG_FILE_PATH=./tests/data/test_config.yaml \ No newline at end of file diff --git a/tests/test_notify.py b/tests/test_notify.py index 33307748..c73469c6 100644 --- a/tests/test_notify.py +++ b/tests/test_notify.py @@ -127,6 +127,16 @@ def test_send_with_username(self): "Subject: icloud-docker: Two step authentication is required", instance.sendmail.mock_calls[0][2]["msg"], ) + self.assertNotIn("--region=", instance.sendmail.mock_calls[0][2]["msg"]) + + def test_send_with_region(self): + """Test for email send with region.""" + username = "username@icloud.com" + with patch("smtplib.SMTP") as smtp: + notify.send(self.config, username, region="some_region") + + instance = smtp.return_value + self.assertIn("--region=some_region", instance.sendmail.mock_calls[0][2]["msg"]) def test_send_fail(self): """Test for failed send."""