-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathsync-icloud.sh
executable file
·2445 lines (2374 loc) · 107 KB
/
sync-icloud.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/ash
##### Functions #####
initialise_script()
{
log_info "***** boredazfcuk/icloudpd container v1.0.$(cat /opt/build_version.txt) started *****"
log_info "***** For support, please go here: https://github.com/boredazfcuk/docker-icloudpd *****"
log_info "$(cat /etc/*-release | grep "^NAME" | sed 's/NAME=//g' | sed 's/"//g') $(cat /etc/*-release | grep "VERSION_ID" | sed 's/VERSION_ID=//g' | sed 's/"//g')"
log_info "Python version: $(cat /tmp/icloudpd/python_version)"
log_info "icloud-photos-downloader version: $(cat /tmp/icloudpd/icloudpd_version)"
log_info "Loading configuration from: ${config_file}"
source "${config_file}"
save_ifs="${IFS}"
lan_ip="$(hostname -i)"
login_counter=0
apple_id="$(echo -n "${apple_id}" | tr '[:upper:]' '[:lower:]')"
cookie_file="$(echo -n "${apple_id//[^a-z0-9_]/}")"
local icloud_dot_com dns_counter
if [ "${icloud_china:=false}" = true ]
then
icloud_domain="icloud.com.cn"
else
icloud_domain="icloud.com"
fi
case "${synchronisation_interval:=86400}" in
21600) synchronisation_interval=21600;; # 6 hours
43200) synchronisation_interval=43200;; # 12 hours
86400) synchronisation_interval=86400;; # 24 hours
129600) synchronisation_interval=129600;; # 36 hours
172800) synchronisation_interval=172800;; # 48 hours
604800) synchronisation_interval=604800;; # 7 days
*) synchronisation_interval=86400;; # 24 hours
esac
if [ "${synchronisation_delay:=0}" -gt 60 ]
then
synchronisation_delay=60
fi
log_debug "Running user id: $(id --user)"
log_debug "Running group id: $(id --group)"
if [ "${debug_logging}" = true ]
then
log_debug "Local user: ${user:0:2}********:${user_id}"
log_debug "Local group: ${group:0:2}********:${group_id}"
else
log_info "Local user: ${user}:${user_id}"
log_info "Local group: ${group}:${group_id}"
fi
log_debug "Force GID: ${force_gid}"
log_debug "LAN IP Address: ${lan_ip}"
log_debug "Default gateway: $(ip route | grep default | awk '{print $3}')"
log_debug "DNS server: $(grep nameserver /etc/resolv.conf | awk '{print $2}')"
icloud_dot_com="$(nslookup -type=a ${icloud_domain} | grep -v "127.0.0.1" | grep Address | tail -1 | awk '{print $2}')"
while [ -z "${icloud_dot_com}" ]
do
if [ "${dns_counter:=0}" = 0 ]
then
log_warning "Cannot find ${icloud_domain} IP address - retrying"
fi
sleep 10
icloud_dot_com="$(nslookup -type=a ${icloud_domain} | grep -v "127.0.0.1" | grep Address | tail -1 | awk '{print $2}')"
dns_counter=$((dns_counter+1))
if [ "${dns_counter}" = 12 ]
then
log_error "Cannot find ${icloud_domain} IP address. Please check your DNS/Firewall settings - exiting"
sleep 120
exit 1
fi
done
log_debug "IP address for ${icloud_domain}: ${icloud_dot_com}"
if [ "${debug_logging}" = true ]
then
log_info "Debug logging: Enabled"
log_level="debug"
else
log_info "Debug logging: Disabled"
log_level="info"
fi
apple_id_prefix="${apple_id%%@*}"
apple_id_suffix="${apple_id##*@}"
apple_id_domain="${apple_id_suffix%%.*}"
apple_id_tld="${apple_id##*.}"
apple_id_censored="${apple_id_prefix:0:1}******${apple_id_prefix:0-1}@${apple_id_domain:0:1}******.${apple_id_tld}"
log_info "Apple ID: ${apple_id_censored}"
cookie_file_censored="$(echo -n "${apple_id_censored//[^a-z0-9_*]/}")"
log_info "Cookie path: /config/${cookie_file_censored}"
log_info "Cookie expiry notification period: ${notification_days}"
log_info "Download destination directory: ${download_path}"
log_info "Folder structure: ${folder_structure}"
log_debug "Directory permissions: ${directory_permissions}"
log_debug "File permissions: ${file_permissions}"
log_info "Keep Unicode: ${keep_unicode}"
log_info "Live Photo MOV Filename Policy: ${live_photo_mov_filename_policy}"
log_info "File Match Policy: ${file_match_policy}"
log_info "Synchronisation interval: ${synchronisation_interval}"
log_info "Synchronisation delay (minutes): ${synchronisation_delay}"
log_info "Set EXIF date/time: ${set_exif_datetime}"
log_info "Auto delete: ${auto_delete}"
log_info "Delete after download: ${delete_after_download}"
if [ "${keep_icloud_recent_only}" = true ]
then
log_info "Keep iCloud recent : Enabled"
log_info "Keep iCloud recent days: ${keep_icloud_recent_days}"
fi
log_info "Delete empty directories: ${delete_empty_directories}"
log_info "Photo size: ${photo_size}"
log_info "Align RAW: ${align_raw}"
log_info "Single pass mode: ${single_pass}"
if [ "${single_pass}" = true ]
then
log_debug "Single pass mode enabled. Disabling download check"
skip_check=true
fi
log_info "Skip download check: ${skip_check}"
log_info "Skip live photos: ${skip_live_photos}"
if [ "${recent_only}" ]
then
log_info "Number of most recently added photos to download: ${recent_only}"
else
log_info "Number of most recently added photos to download: Download All Photos"
fi
if [ "${photo_album}" ]
then
log_info "Downloading photos from album(s): ${photo_album}"
elif [ "${photo_library}" ]
then
log_info "Downloading photos from library: ${photo_library}"
else
log_info "Downloading photos from: Download All Photos"
fi
if [ "${until_found}" ]
then
log_info "Stop downloading when prexisiting files count is: ${until_found}"
else
log_info "Stop downloading when prexisiting files count is: Download All Photos"
fi
if [ "${skip_live_photos}" = false ]
then
log_info "Live photo size: ${live_photo_size}"
fi
log_info "Skip videos: ${skip_videos}"
log_info "Convert HEIC to JPEG: ${convert_heic_to_jpeg}"
if [ "${convert_heic_to_jpeg}" = true ]
then
log_debug "JPEG conversion quality: ${jpeg_quality}"
fi
if [ "${jpeg_path}" ]
then
log_info "Converted JPEGs path: ${jpeg_path}"
fi
if [ "${delete_accompanying}" = true ]
then
log_info "Delete accompanying files (.JPG/.HEIC.MOV)"
fi
if [ "${notification_type}" ]
then
configure_notifications
fi
log_info "Downloading from: ${icloud_domain}"
if [ "${icloud_china}" = true ]
then
if [ "${auth_china}" = true ]
then
auth_domain="cn"
fi
fi
if [ "${fake_user_agent}" = true ]
then
log_info "User agent impersonation for curl: Enabled"
curl_user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edge/122.0.0.0"
else
log_debug "User agent impersonation for curl: Disabled"
fi
log_info "Authentication domain: ${auth_domain:=com}"
if [ "${nextcloud_upload}" = true ]
then
log_info "Nextcloud upload: Enabled"
nextcloud_url_scheme="${nextcloud_url%//*}//"
nextcloud_url_suffix="${nextcloud_url##*//}"
nextcloud_url_domain="${nextcloud_url_suffix%%/*}"
nextcloud_url_tld="${nextcloud_url_domain#*.}"
nextcloud_url_webroot="${nextcloud_url_suffix#*/}"
nextcloud_url_censored="${nextcloud_url_domain:0:1}********.${nextcloud_url_tld}/${nextcloud_url_webroot%/}${nextcloud_target_dir%/}/"
nextcloud_url_censored="${nextcloud_url_censored//\/\///}"
log_debug "Nextcloud username: ${nextcloud_username:0:1}********${nextcloud_username:0-1}"
log_debug "Nextcloud target directory: ${nextcloud_target_dir}"
log_debug "Nextcloud destination URL: ${nextcloud_url_scheme}${nextcloud_url_censored}"
else
log_debug "Nextcloud upload: Disabled"
fi
if [ "${synology_ignore_path}" = true ]
then
log_info "Ignore Synology extended attribute directories: Enabled"
ignore_path="*/@eaDir*"
else
log_debug "Ignore Synology extended attribute directories: Disabled"
ignore_path=""
fi
source /opt/icloudpd/bin/activate
log_debug "Activated Python virtual environment for icloudpd"
}
log_info()
{
echo "$(date '+%Y-%m-%d %H:%M:%S') INFO ${1}"
}
log_info_n()
{
echo -n "$(date '+%Y-%m-%d %H:%M:%S') INFO ${1}... "
}
log_warning()
{
echo "$(date '+%Y-%m-%d %H:%M:%S') WARNING ${1}"
}
log_error()
{
echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR ${1}"
}
log_debug()
{
if [ "${debug_logging}" = true ]
then
echo "$(date '+%Y-%m-%d %H:%M:%S') DEBUG ${1}"
fi
}
run_as()
{
if [ "$(id -u)" = 0 ]
then
su "${user}" -s /bin/ash -c "${1}"
else
/bin/ash -c "${command_to_run}"
fi
}
clean_notification_title()
{
if [ "${notification_title}" ]
then
notification_title="${notification_title//[^a-zA-Z0-9_ ]/}"
log_debug "Cleaned notification title: ${notification_title}"
else
log_debug "Notification title: ${notification_title:=boredazfcuk/iCloudPD}"
fi
}
configure_notifications()
{
if [ -z "${prowl_api_key}" ] && [ -z "${pushover_token}" ] && [ -z "${telegram_token}" ] && [ -z "${webhook_id}" ] && [ -z "${dingtalk_token}" ] && [ -z "${discord_token}" ] && [ -z "${iyuu_token}" ] && [ -z "${wecom_secret}" ] && [ -z "${gotify_app_token}" ] && [ -z "${bark_device_key}" ] && [ -z "${msmtp_pass}" ]
then
log_warning "${notification_type} notifications enabled, but API key/token/secret not set - disabling notifications"
unset notification_type
else
if [ "${notification_type}" = "Prowl" ] && [ "${prowl_api_key}" ]
then
log_info "${notification_type} notifications enabled"
clean_notification_title
log_debug "${notification_type} api key: ${prowl_api_key:0:2}********${prowl_api_key:0-2}"
notification_url="https://api.prowlapp.com/publicapi/add"
log_debug "${notification_type} notification URL: ${notification_url}"
elif [ "${notification_type}" = "Pushover" ] && [ "${pushover_user}" ] && [ "${pushover_token}" ]
then
log_info "${notification_type} notifications enabled"
clean_notification_title
log_debug "${notification_type} user: ${pushover_user:0:2}********${pushover_user:0-2}"
log_debug "${notification_type} token: ${pushover_token:0:2}********${pushover_token:0-2}"
if [ "${pushover_sound}" ]
then
case "${pushover_sound}" in
pushover|bike|bugle|cashregister|classical|cosmic|falling|gamelan|incoming|intermission|magic|mechanical|pianobar|siren|spacealarm|tugboat|alien|climb|persistent|echo|updown|vibrate|none)
log_debug "${notification_type} sound: ${pushover_sound}"
;;
*)
log_debug "${notification_type} sound not recognised. Using default"
unset pushover_sound
esac
fi
notification_url="https://api.pushover.net/1/messages.json"
log_debug "${notification_type} notification URL: ${notification_url}"
elif [ "${notification_type}" = "Telegram" ] && [ "${telegram_token}" ] && [ "${telegram_chat_id}" ]
then
if [ "${telegram_http}" = true ]
then
telegram_protocol="http"
else
telegram_protocol="https"
fi
if [ "${telegram_server}" ]
then
telegram_base_url="${telegram_protocol}://${telegram_server}/bot${telegram_token}"
else
telegram_base_url="${telegram_protocol}://api.telegram.org/bot${telegram_token}"
fi
notification_url="${telegram_base_url}/sendMessage"
log_info "${notification_type} notifications enabled"
clean_notification_title
log_debug "${notification_type} token: ${telegram_token:0:2}********${telegram_token:0-2}"
log_debug "${notification_type} chat id: ${telegram_chat_id:0:2}********${telegram_chat_id:0-2}"
log_debug "${notification_type} polling: ${telegram_polling}"
log_debug "${notification_type} uses HTTP: ${telegram_http}"
if [ "${telegram_server}" ]
then
log_debug "${notification_type} base URL: ${telegram_protocol}://${telegram_server}/bot${telegram_token:0:2}********${telegram_token:0-2}"
else
log_debug "${notification_type} base URL: ${telegram_protocol}://api.telegram.org/bot${telegram_token:0:2}********${telegram_token:0-2}"
fi
##################
log_debug "${notification_type} notification URL: ${telegram_protocol}://api.telegram.org/bot${telegram_token:0:2}********${telegram_token:0-2}/sendMessage"
if [ "${script_launch_parameters}" ]
then
telegram_polling="false"
fi
if [ "${telegram_polling}" = true ]
then
if [ "$(cat /tmp/icloudpd/bot_check)" = true ]
then
if [ "${telegram_server}" ]
then
log_debug "Checking ${telegram_server} for updates"
else
log_debug "Checking api.telegram.org for updates"
fi
telegram_update_id_offset_file="/config/telegram_update_id.num"
telegram_update_id_offset="$(head -1 ${telegram_update_id_offset_file})"
log_info "Latest update id: ${telegram_update_id_offset}"
else
telegram_polling=false
fi
fi
if [ "${telegram_silent_file_notifications}" ]
then
telegram_silent_file_notifications=true
fi
log_debug "${notification_type} silent file notifications: ${telegram_silent_file_notifications:=false}"
elif [ "${notification_type}" = "openhab" ] && [ "${webhook_server}" ] && [ "${webhook_id}" ]
then
if [ "${webhook_https}" = true ]
then
webhook_scheme="https"
else
webhook_scheme="http"
fi
log_info "${notification_type} notifications enabled"
notification_url="${webhook_scheme}://${webhook_server}:${webhook_port}${webhook_path}${webhook_id}"
log_debug "${notification_type} server: ${webhook_server}"
log_debug "${notification_type} port: ${webhook_port:=8123}"
log_debug "${notification_type} path: ${webhook_path:=/rest/items/}"
log_debug "${notification_type} ID: ${webhook_id:0:2}********${webhook_id:0-2}"
log_debug "${notification_type} notification URL: ${webhook_scheme}://${webhook_server}:${webhook_port}${webhook_path}${webhook_id:0:2}********${webhook_id:0-2}"
elif [ "${notification_type}" = "Webhook" ] && [ "${webhook_server}" ] && [ "${webhook_id}" ]
then
if [ "${webhook_https}" = true ]
then
webhook_scheme="https"
else
webhook_scheme="http"
fi
log_info "${notification_type} notifications enabled"
clean_notification_title
notification_url="${webhook_scheme}://${webhook_server}:${webhook_port}${webhook_path}${webhook_id}"
log_debug "${notification_type} server: ${webhook_server}"
log_debug "${notification_type} port: ${webhook_port:=8123}"
log_debug "${notification_type} path: ${webhook_path:=/api/webhook/}"
log_debug "${notification_type} ID: ${webhook_id:0:2}********${webhook_id:0-2}"
log_debug "${notification_type} notification URL: ${webhook_scheme}://${webhook_server}:${webhook_port}${webhook_path}${webhook_id:0:2}********${webhook_id:0-2}"
log_debug "${notification_type} body keyword: ${webhook_body:=data}"
if [ "${webhook_insecure}" ] && [ "${debug_logging}" = true ]
then
log_debug "${notification_type} insecure certificates allowed"
fi
elif [ "${notification_type}" = "Discord" ] && [ "${discord_id}" ] && [ "${discord_token}" ]
then
log_info "${notification_type} notifications enabled"
clean_notification_title
notification_url="https://discord.com/api/webhooks/${discord_id}/${discord_token}"
log_debug "${notification_type} Discord ID: ${discord_id:0:2}********${discord_id:0-2}"
log_debug "${notification_type} Discord token: ${discord_token:0:2}********${discord_token:0-2}"
log_debug "${notification_type} notification URL: https://discord.com/api/webhooks/${discord_id:0:2}********${discord_id:0-2}/${discord_token:0:2}********${discord_token:0-2}"
elif [ "${notification_type}" = "Dingtalk" ] && [ "${dingtalk_token}" ]
then
notification_url="https://oapi.dingtalk.com/robot/send?access_token=${dingtalk_token}"
log_info "${notification_type} notifications enabled"
log_debug "${notification_type} token: ${dingtalk_token:0:2}********${dingtalk_token:0-2}"
log_debug "${notification_type} notification URL: https://oapi.dingtalk.com/robot/send?access_token=${dingtalk_token:0:2}********${dingtalk_token:0-2}"
elif [ "${notification_type}" = "IYUU" ] && [ "${iyuu_token}" ]
then
notification_url="http://iyuu.cn/${iyuu_token}.send?"
log_info "${notification_type} notifications enabled"
log_debug "${notification_type} token: ${iyuu_token}"
log_debug "${notification_type} notification URL: http://iyuu.cn/${iyuu_token:0:2}********${iyuu_token:0-2}.send?"
elif [ "${notification_type}" = "WeCom" ] && [ "${wecom_id}" ] && [ "${wecom_secret}" ]
then
wecom_base_url="https://qyapi.weixin.qq.com"
if [ "${wecom_proxy}" ]
then
wecom_base_url="${wecom_proxy}"
log_debug "${notification_type} notifications proxy enabled : ${wecom_proxy}"
fi
wecom_token_url="${wecom_base_url}/cgi-bin/gettoken?corpid=${wecom_id}&corpsecret=${wecom_secret}"
if [ "${fake_user_agent}" = true ]
then
wecom_token="$(/usr/bin/curl --silent --user-agent "${curl_user_agent}" --get "${wecom_token_url}" | awk -F\" '{print $10}')"
else
wecom_token="$(/usr/bin/curl --silent --get "${wecom_token_url}" | awk -F\" '{print $10}')"
fi
wecom_token_expiry="$(date --date='2 hour')"
notification_url="${wecom_base_url}/cgi-bin/message/send?access_token=${wecom_token}"
log_info "${notification_type} notifications enabled"
log_debug "${notification_type} token: ${wecom_token:0:2}********${wecom_token:0-2}"
log_debug "${notification_type} token expiry time: $(date -d "${wecom_token_expiry}")"
log_debug "${notification_type} notification URL: ${wecom_base_url}/cgi-bin/message/send?access_token=${wecom_token:0:2}********${wecom_token:0-2}"
elif [ "${notification_type}" = "Gotify" ] && [ "${gotify_app_token}" ] && [ "${gotify_server_url}" ]
then
if [ "${gotify_https}" = true ]
then
gotify_scheme="https"
else
gotify_scheme="http"
fi
log_info "${notification_type} notifications enabled"
clean_notification_title
notification_url="${gotify_scheme}://${gotify_server_url}/message?token=${gotify_app_token}"
log_debug "${notification_type} token: ${gotify_app_token:0:2}********${gotify_app_token:0-2}"
log_debug "${notification_type} server URL: ${gotify_scheme}://${gotify_server_url}"
log_debug "${notification_type} notification URL: ${gotify_scheme}://${gotify_server_url}/message?token=${gotify_app_token:0:2}********${gotify_app_token:0-2}"
elif [ "${notification_type}" = "Bark" ] && [ "${bark_device_key}" ] && [ "${bark_server}" ]
then
log_info "${notification_type} notifications enabled"
clean_notification_title
notification_url="http://${bark_server}/push"
log_debug "${notification_type} device key: ${bark_device_key:0:2}********${bark_device_key:0-2}"
log_debug "${notification_type} server: ${bark_server}"
log_debug "${notification_type} notification URL: http://${bark_server}/push"
elif [ "${notification_type}" = "msmtp" ] && [ "${msmtp_host}" ] && [ "${msmtp_port}" ] && [ "${msmtp_user}" ] && [ "${msmtp_pass}" ]
then
log_info "${notification_type} notifications enabled"
else
log_warning "${notification_type} notifications enabled, but configured incorrectly - disabling notifications"
unset notification_type prowl_api_key pushover_user pushover_token telegram_token telegram_chat_id webhook_scheme webhook_server webhook_port webhook_id dingtalk_token discord_id discord_token iyuu_token wecom_id wecom_secret gotify_app_token gotify_scheme gotify_server_url bark_device_key bark_server
fi
if [ "${startup_notification}" = true ]
then
log_debug "Startup notification: Enabled"
if [ "${icloud_china}" = false ]
then
send_notification "startup" "iCloudPD container started" "0" "iCloudPD container starting for Apple ID: ${apple_id}"
else
send_notification "startup" "iCloudPD container started" "0" "启动成功,开始同步当前 Apple ID 中的照片" "" "" "" "开始同步 ${name} 的 iCloud 图库" "Apple ID: ${apple_id}"
fi
else
log_debug "Startup notification: Disabled"
fi
if [ "${download_notifications}" = true ]
then
log_debug "Download notifications: Enabled"
else
log_debug "Download notifications: Disabled"
unset download_notifications
fi
if [ "${delete_notifications}" = true ]
then
log_debug "Delete notifications: Enabled"
else
log_debug "Delete notifications: Disabled"
unset delete_notifications
fi
fi
}
list_libraries()
{
local shared_libraries
if [ "${authentication_type}" = "MFA" ]
then
check_multifactor_authentication_cookie
else
check_web_cookie
fi
IFS=$'\n'
if [ "${skip_download}" = false ]
then
shared_libraries="$(run_as "/opt/icloudpd/bin/icloudpd --username ${apple_id} --cookie-directory /config --domain ${auth_domain} --directory /dev/null --list-libraries | sed '1d'")"
fi
log_info "Shared libraries:"
for library in ${shared_libraries}
do
log_info " - ${library}"
done
IFS="${save_ifs}"
}
list_albums()
{
local photo_albums
if [ "${authentication_type}" = "MFA" ]
then
check_multifactor_authentication_cookie
else
check_web_cookie
fi
IFS=$'\n'
if [ "${skip_download}" = false ]
then
photo_albums="$(run_as "/opt/icloudpd/bin/icloudpd --username ${apple_id} --cookie-directory /config --domain ${auth_domain} --directory /dev/null --list-albums | sed '1d' | sed '/^Albums:$/d'")"
fi
log_info "Photo albums:"
for photo_album in ${photo_albums}
do
log_info " - ${photo_album}"
done
IFS="${save_ifs}"
}
delete_password()
{
if [ -f "/config/python_keyring/keyring_pass.cfg" ]
then
log_warning "Keyring file /config/python_keyring/keyring_pass.cfg exists, but --remove-keyring command line switch has been invoked. Removing in 30 seconds"
if [ -z "${warnings_acknowledged}" ]
then
sleep 30
else
log_info "Warnings acknowledged, removing immediately"
fi
rm "/config/python_keyring/keyring_pass.cfg"
else
log_error "Keyring file does not exist"
fi
}
configure_password()
{
log_debug "Configure password"
if [ -f "/config/python_keyring/keyring_pass.cfg" ]
then
if [ "$(grep -c "=" "/config/python_keyring/keyring_pass.cfg")" -eq 0 ]
then
log_debug "Keyring file /config/python_keyring/keyring_pass.cfg exists, but does not contain any credentials. Removing"
rm "/config/python_keyring/keyring_pass.cfg"
fi
fi
if [ ! -f "/config/python_keyring/keyring_pass.cfg" ]
then
if [ "${initialise_container}" ]
then
log_debug "Adding password to keyring file: /config/python_keyring/keyring_pass.cfg"
run_as "/opt/icloudpd/bin/icloud --username ${apple_id} --domain ${auth_domain}"
else
log_error "Keyring file /config/python_keyring/keyring_pass.cfg does not exist"
log_error " - Please add the your password to the system keyring using the --Initialise script command line option"
log_error " - Syntax: docker exec -it <container name> sync-icloud.sh --Initialise"
log_error " - Example: docker exec -it icloudpd sync-icloud.sh --Initialise"
log_error "Waiting for keyring file to be created..."
local counter
counter="${counter:=0}"
while [ ! -f "/config/python_keyring/keyring_pass.cfg" ]
do
sleep 5
counter=$((counter + 1))
if [ "${counter}" -eq 360 ]
then
log_error "Keyring file has not appeared within 30 minutes. Restarting container..."
exit 1
fi
done
log_debug "Keyring file exists, continuing"
fi
else
log_debug "Using password stored in keyring file: /config/python_keyring/keyring_pass.cfg"
fi
if [ ! -f "/config/python_keyring/keyring_pass.cfg" ]
then
log_error "Keyring file does not exist. Please try again"
sleep 120
exit 1
fi
}
generate_cookie()
{
if [ -f "/config/${cookie_file}" ]
then
mv "/config/${cookie_file}" "/config/${cookie_file}.bak"
fi
if [ -f "/config/${cookie_file}.session" ]
then
mv "/config/${cookie_file}.session" "/config/${cookie_file}session.bak"
fi
log_debug "Generate ${authentication_type} cookie using password stored in keyring file"
run_as "/opt/icloudpd/bin/icloudpd --username ${apple_id} --cookie-directory /config --auth-only --domain ${auth_domain}"
if [ "${authentication_type}" = "MFA" ]
then
if [ "$(grep -c "X-APPLE-WEBAUTH-HSA-TRUST" "/config/${cookie_file}")" -eq 1 ]
then
log_info "Multifactor authentication cookie generated. Sync should now be successful"
else
log_error "Multifactor authentication information missing from cookie. Authentication has failed"
log_error " - Was the correct password entered?"
log_error " - Was the multifactor authentication code mistyped?"
log_error " - Can you log into ${icloud_domain} without receiving pop-up notifications?"
if [ "${icloud_china}" = true ]
then
log_error " - Are you based in China? You will need to set the icloud_china variable"
fi
fi
else
log_debug "Web cookie generated. Sync should now be successful"
fi
}
check_mount()
{
log_info "Check download directory mounted correctly..."
if [ ! -f "${download_path}/.mounted" ]
then
log_warning "Failsafe file ${download_path}/.mounted file is not present. Waiting for failsafe file to be created..."
local counter
counter="0"
fi
while [ ! -f "${download_path}/.mounted" ]
do
sleep 5
counter=$((counter + 1))
if [ "${counter}" -eq 360 ]
then
log_error "Failsafe file has not appeared within 30 minutes. Restarting container..."
exit 1
fi
done
log_info "Failsafe file ${download_path}/.mounted exists, continuing"
}
# set_owner_and_permissions_config()
# {
# log_debug "Set owner and group on icloudpd temp directory"
# chown -R "${user_id}:${group_id}" "/tmp/icloudpd"
# log_debug "Set owner and group on config directory"
# chown -R "${user_id}:${group_id}" "/config"
# }
# set_owner_and_permissions_downloads()
# {
# log_debug "Set owner on iCloud directory, if required"
# find "${download_path}" ! -type l ! -user "${user_id}" ! -path "${ignore_path}" -exec chown "${user_id}" {} +
# log_debug "Set group on iCloud directory, if required"
# find "${download_path}" ! -type l ! -group "${group_id}" ! -path "${ignore_path}" -exec chgrp "${group_id}" {} +
# log_debug "Set ${directory_permissions} permissions on iCloud directories, if required"
# find "${download_path}" -type d ! -perm "${directory_permissions}" ! -path "${ignore_path}" -exec chmod "${directory_permissions}" '{}' +
# log_debug "Set ${file_permissions} permissions on iCloud files, if required"
# find "${download_path}" -type f ! -perm "${file_permissions}" ! -path "${ignore_path}" -exec chmod "${file_permissions}" '{}' +
# if [ "${jpeg_path}" ]
# then
# log_debug "Set owner on jpeg directory, if required"
# find "${jpeg_path}" ! -type l ! -user "${user_id}" ! -path "${ignore_path}" -exec chown "${user_id}" {} +
# log_debug "Set group on jpeg directory, if required"
# find "${jpeg_path}" ! -type l ! -group "${group_id}" ! -path "${ignore_path}" -exec chgrp "${group_id}" {} +
# log_debug "Set ${directory_permissions} permissions on jpeg directories, if required"
# find "${jpeg_path}" -type d ! -perm "${directory_permissions}" ! -path "${ignore_path}" -exec chmod "${directory_permissions}" '{}' +
# log_debug "Set ${file_permissions} permissions on jpeg files, if required"
# find "${jpeg_path}" -type f ! -perm "${file_permissions}" ! -path "${ignore_path}" -exec chmod "${file_permissions}" '{}' +
# fi
# }
check_permissions()
{
if [ "$(run_as "${user}" "if ! test -w \"${download_path}\"; then echo false; fi")" = false ]
then
log_warning "User ${user}:${user_id} cannot write to directory: ${download_path} - Attempting to set permissions"
# set_owner_and_permissions_downloads
if [ "$(run_as "${user}" "if ! test -w \"${download_path}\"; then echo false; fi")" = false ]
then
log_error "User ${user}:${user_id} still cannot write to directory: ${download_path}"
log_error " - Fixing permissions failed - Cannot continue, exiting"
sleep 120
exit 1
fi
fi
if [ "$(run_as "${user}" "if ! test -w \"${jpeg_path}\"; then echo false; fi")" = false ]
then
log_warning "User ${user}:${user_id} cannot write to directory: ${jpeg_path} - Attempting to set permissions"
# set_owner_and_permissions_downloads
if [ "$(run_as "${user}" "if ! test -w \"${jpeg_path}\"; then echo false; fi")" = false ]
then
log_error "User ${user}:${user_id} still cannot write to directory: ${jpeg_path}"
log_error " - Fixing permissions failed - Cannot continue, exiting"
sleep 120
exit 1
fi
fi
}
check_keyring_exists()
{
if [ -f "/config/python_keyring/keyring_pass.cfg" ]
then
log_info "Keyring file exists, continuing"
else
log_error "Keyring does not exist"
log_error " - Please add your password to the system keyring by using the --Initialise script command line option"
log_error " - Syntax: docker exec -it <container name> sync-icloud.sh --Initialise"
log_error " - Example: docker exec -it icloudpd sync-icloud.sh --Initialise"
log_error "Waiting for keyring file to be created..."
local counter
counter="${counter:=0}"
while [ ! -f "/config/python_keyring/keyring_pass.cfg" ]
do
sleep 5
counter=$((counter + 1))
if [ "${counter}" -eq 360 ]
then
log_error "Keyring file has not appeared within 30 minutes. Restarting container..."
exit 1
fi
done
log_info "Keyring file exists, continuing"
fi
}
wait_for_cookie()
{
if [ "${1}" = "DisplayMessage" ]
then
log_error "Waiting for valid cookie file to be created..."
log_error " - Please create your cookie using the --Initialise script command line option"
log_error " - Syntax: docker exec -it <container name> sync-icloud.sh --Initialise"
log_error " - Example: docker exec -it icloudpd sync-icloud.sh --Initialise"
fi
local counter
counter="${counter:=0}"
while [ ! -f "/config/${cookie_file}" ]
do
sleep 5
counter=$((counter + 1))
if [ "${counter}" -eq 360 ]
then
log_error "Valid cookie file has not appeared within 30 minutes. Restarting container..."
exit 1
fi
done
}
wait_for_authentication()
{
local counter
counter="${counter:=0}"
while [ "$(grep -c "X-APPLE-WEBAUTH-HSA-TRUST" "/config/${cookie_file}" >/dev/null 2>&1 && echo 1 || echo 0)" -eq 0 ]
do
sleep 5
counter=$((counter + 1))
if [ "${counter}" -eq 360 ]
then
log_error "Valid cookie file has not appeared within 30 minutes. Restarting container..."
exit 1
fi
done
}
check_web_cookie()
{
if [ -f "/config/${cookie_file}" ]
then
log_debug "Web cookie exists"
web_cookie_expire_date="$(grep "X_APPLE_WEB_KB" "/config/${cookie_file}" | sed -e 's#.*expires="\(.*\)Z"; HttpOnly.*#\1#')"
else
log_error "Web cookie does not exist"
wait_for_cookie DisplayMessage
log_info "Cookie file exists, continuing"
fi
}
check_multifactor_authentication_cookie()
{
if [ -f "/config/${cookie_file}" ]
then
log_debug "Multifactor authentication cookie exists"
else
log_error "Multifactor authentication cookie does not exist"
wait_for_cookie DisplayMessage
log_debug "Multifactor authentication cookie file exists, checking validity..."
fi
if [ "$(grep -c "X-APPLE-DS-WEB-SESSION-TOKEN" "/config/${cookie_file}")" -eq 1 ] && [ "$(grep -c "X-APPLE-WEBAUTH-HSA-TRUST" "/config/${cookie_file}")" -eq 0 ]
then
log_debug "Multifactor authentication cookie exists, but not autenticated. Waiting for authentication to complete..."
wait_for_authentication
log_debug "Multifactor authentication authentication complete, checking expiry date..."
fi
if [ "$(grep -c "X-APPLE-WEBAUTH-USER" "/config/${cookie_file}")" -eq 1 ]
then
mfa_expire_date="$(grep "X-APPLE-WEBAUTH-USER" "/config/${cookie_file}" | sed -e 's#.*expires="\(.*\)Z"; HttpOnly.*#\1#')"
mfa_expire_seconds="$(date -d "${mfa_expire_date}" '+%s')"
days_remaining="$(($((mfa_expire_seconds - $(date '+%s'))) / 86400))"
echo "${days_remaining}" > "/config/DAYS_REMAINING"
if [ "${days_remaining}" -gt 0 ]
then
valid_mfa_cookie=true
log_debug "Valid multifactor authentication cookie found. Days until expiration: ${days_remaining}"
else
rm -f "/config/${cookie_file}"
log_error "Cookie expired at: ${mfa_expire_date}"
log_error "Expired cookie file has been removed. Restarting container in 5 minutes"
sleep 300
exit 1
fi
else
rm -f "/config/${cookie_file}"
log_error "Cookie is not multifactor authentication capable, authentication type may have changed"
log_error "Invalid cookie file has been removed. Restarting container in 5 minutes"
sleep 300
exit 1
fi
}
display_multifactor_authentication_expiry()
{
local error_message
log_info "Multifactor authentication cookie expires: ${mfa_expire_date/ / @ }"
log_info "Days remaining until expiration: ${days_remaining}"
if [ "${days_remaining}" -le "${notification_days}" ]
then
if [ "${days_remaining}" -eq 1 ]
then
cookie_status="cookie expired"
if [ "${icloud_china}" = false ]
then
error_message="Final day before multifactor authentication cookie expires for Apple ID: ${apple_id} - Please reinitialise now. This is your last reminder"
else
error_message="今天是 ${name} 的 Apple ID 两步验证 cookie 到期前的最后一天 - 请立即重新初始化,这是最后的提醒"
fi
else
cookie_status="cookie expiration"
if [ "${icloud_china}" = false ]
then
error_message="Only ${days_remaining} days until multifactor authentication cookie expires for Apple ID: ${apple_id} - Please reinitialise"
else
error_message="${days_remaining} 天后 ${name} 的 Apple ID 两步验证将到期 - 请立即重新初始化"
fi
fi
log_warning "${error_message}"
if [ "${synchronisation_time:=$(date +%s -d '+15 minutes')}" -gt "${next_notification_time:=$(date +%s)}" ]
then
if [ "${icloud_china}" = false ]
then
send_notification "${cookie_status}" "Multifactor Authentication Cookie Expiration" "2" "${error_message}"
else
send_notification "${cookie_status}" "Multifactor Authentication Cookie Expiration" "2" "${error_message}" "" "" "" "${days_remaining} 天后,${name} 的身份验证到期" "${error_message}"
fi
next_notification_time="$(date +%s -d "+24 hour")"
log_debug "Next notification not before: $(date +%H:%M:%S -d "${next_notification_time} seconds")"
fi
fi
}
check_files()
{
if [ -f "/tmp/icloudpd/icloudpd_check.log" ]
then
rm "/tmp/icloudpd/icloudpd_check.log"
fi
log_info "Check for new files using password stored in keyring file"
log_info "Generating list of files in iCloud. This may take a long time if you have a large photo collection. Please be patient. Nothing is being downloaded at this time"
log_debug "Launch command: /opt/icloudpd/bin/icloudpd --directory ${download_path} --cookie-directory /config --username ${apple_id} --domain ${auth_domain} --folder-structure ${folder_structure} --only-print-filenames"
>/tmp/icloudpd/icloudpd_check_error
run_as "(/opt/icloudpd/bin/icloudpd --directory ${download_path} --cookie-directory /config --username ${apple_id} --domain ${auth_domain} --folder-structure ${folder_structure} --only-print-filenames 2>/tmp/icloudpd/icloudpd_check_error; echo $? >/tmp/icloudpd/icloudpd_check_exit_code) | tee /tmp/icloudpd/icloudpd_check.log"
check_exit_code="$(cat /tmp/icloudpd/icloudpd_check_exit_code)"
if [ "${check_exit_code}" -ne 0 ] || [ -s /tmp/icloudpd/icloudpd_check_error ]
then
log_error "Failed check for new files files"
log_error " - Can you log into ${icloud_domain} without receiving pop-up notifications?"
log_error "Error debugging info:"
log_error "$(cat /tmp/icloudpd/icloudpd_check_error)"
if [ "${debug_logging}" != true ]
then
log_error "Please set debug_logging=true in your icloudpd.conf file then reproduce the error"
log_error "***** Once you have captured this log file, please post it along with a description of your problem, here: https://github.com/boredazfcuk/docker-icloudpd/issues *****"
else
log_error "***** Please post the above debug log, along with a description of your problem, here: https://github.com/boredazfcuk/docker-icloudpd/issues *****"
fi
if [ "${icloud_china}" = false ]
then
send_notification "failure" "iCloudPD container failure" "0" "iCloudPD failed check for new files for Apple ID: ${apple_id}"
else
syn_end_time="$(date '+%H:%M:%S')"
syn_next_time="$(date +%H:%M:%S -d "${synchronisation_interval} seconds")"
send_notification "failure" "iCloudPD container failure" "0" "检查 iCloud 图库新照片失败,将在 ${syn_next_time} 再次尝试" "" "" "" "检查 ${name} 的 iCloud 图库新照片失败" "将在 ${syn_next_time} 再次尝试"
fi
else
log_info "Check successful"
check_files_count="$(wc --lines /tmp/icloudpd/icloudpd_check.log | awk '{print $1}')"
if [ "${check_files_count}" -gt 0 ]
then
log_info "New files detected: ${check_files_count}"
else
log_info "No new files detected. Nothing to download"
fi
fi
login_counter=$((login_counter + 1))
}
downloaded_files_notification()
{
IFS=$'\n'
local new_files_count new_files_preview new_files_text
new_files="$(grep "Downloaded /" /tmp/icloudpd/icloudpd_sync.log)"
new_files_count="$(grep -c "Downloaded /" /tmp/icloudpd/icloudpd_sync.log)"
if [ "${new_files_count:=0}" -gt 0 ]
then
log_info "New files downloaded: ${new_files_count}"
new_files_preview="$(echo "${new_files}" | cut --delimiter " " --fields 9- | sed -e "s%${download_path}/%%g" | head -10)"
new_files_preview_count="$(echo "${new_files_preview}" | wc -l)"
if [ "${icloud_china}" = false ]
then
new_files_text="Files downloaded for Apple ID ${apple_id}: ${new_files_count}"
send_notification "downloaded files" "New files detected" "0" "${new_files_text}" "${new_files_preview_count}" "downloaded" "${new_files_preview}"
else
# 结束时间、下次同步时间
syn_end_time="$(date '+%H:%M:%S')"
syn_next_time="$(date +%H:%M:%S -d "${synchronisation_interval} seconds")"
new_files_text="iCloud 图库同步完成,新增 ${new_files_count} 张照片"
send_notification "downloaded files" "New files detected" "0" "${new_files_text}" "${new_files_preview_count}" "下载" "${new_files_preview}" "新增 ${new_files_count} 张照片 - ${name}" "下次同步时间 ${syn_next_time}"
fi
fi
IFS="${save_ifs}"
}
deleted_files_notification()
{
IFS=$'\n'
local deleted_files deleted_files_count deleted_files_preview deleted_files_text
deleted_files="$(grep "Deleted /" /tmp/icloudpd/icloudpd_sync.log)"
deleted_files_count="$(grep -c "Deleted /" /tmp/icloudpd/icloudpd_sync.log)"
if [ "${deleted_files_count:=0}" -gt 0 ]
then
log_info "Number of files deleted: ${deleted_files_count}"
deleted_files_preview="$(echo "${deleted_files}" | cut --delimiter " " --fields 9- | sed -e "s%${download_path}/%%g" -e "s%!$%%g" | tail -10)"
deleted_files_preview_count="$(echo "${deleted_files_preview}" | wc -l)"
if [ "${icloud_china}" = false ]
then
deleted_files_text="Files deleted for Apple ID ${apple_id}: ${deleted_files_count}"
send_notification "deleted files" "Recently deleted files detected" "0" "${deleted_files_text}" "${deleted_files_preview_count}" "deleted" "${deleted_files_preview}"
else
# 结束时间、下次同步时间
syn_end_time="$(date '+%H:%M:%S')"
syn_next_time="$(date +%H:%M:%S -d "${synchronisation_interval} seconds")"
deleted_files_text="iCloud 图库同步完成,删除 ${deleted_files_count} 张照片"
send_notification "deleted files" "Recently deleted files detected" "0" "${deleted_files_text}" "${deleted_files_preview_count}" "删除" "${deleted_files_preview}" "删除 ${deleted_files_count} 张照片 - ${name}" "下次同步时间 ${syn_next_time}"
fi
fi
IFS="${save_ifs}"
}
download_albums()
{
local all_albums albums_to_download
if [ "${photo_album}" = "all albums" ]
then
all_albums="$(run_as "/opt/icloudpd/bin/icloudpd --username ${apple_id} --cookie-directory /config --domain ${auth_domain} --directory /dev/null --list-albums | sed '1d' | sed '/^Albums:$/d'")"
log_debug "Buildling list of albums to download..."
IFS=$'\n'
for album in ${all_albums}
do
if [ "${skip_album}" ]
then
if [ ! "${skip_album}" = "${album}" ]
then
log_debug " - ${album}"
if [ -z "${albums_to_download}" ]
then
albums_to_download="${album}"
else
albums_to_download="${albums_to_download},${album}"
fi
fi
else
log_debug " - ${album}"
if [ -z "${albums_to_download}" ]
then
albums_to_download="${album}"
else
albums_to_download="${albums_to_download},${album}"
fi
fi
done
else
albums_to_download="${photo_album}"
fi
IFS=","
log_debug "Starting albums download..."
for album in ${albums_to_download}
do
log_info "Downloading album: ${album}"