forked from boredazfcuk/docker-icloudpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync-icloud.sh
executable file
·2047 lines (1983 loc) · 114 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_config_file(){
{
if [ "$(grep -c "albums_with_dates=" "${config_file}")" -eq 0 ]; then echo albums_with_dates="${albums_with_dates:=false}"; fi
if [ "$(grep -c "apple_id=" "${config_file}")" -eq 0 ]; then echo apple_id="${apple_id}"; fi
if [ "$(grep -c "authentication_type=" "${config_file}")" -eq 0 ]; then echo authentication_type="${authentication_type:=MFA}"; fi
if [ "$(grep -c "auth_china=" "${config_file}")" -eq 0 ]; then echo auth_china="${auth_china:=false}"; fi
if [ "$(grep -c "auto_delete=" "${config_file}")" -eq 0 ]; then echo auto_delete="${auto_delete:=false}"; fi
if [ "$(grep -c "bark_device_key=" "${config_file}")" -eq 0 ]; then echo bark_device_key="${bark_device_key}"; fi
if [ "$(grep -c "bark_server=" "${config_file}")" -eq 0 ]; then echo bark_server="${bark_server}"; fi
if [ "$(grep -c "convert_heic_to_jpeg=" "${config_file}")" -eq 0 ]; then echo convert_heic_to_jpeg="${convert_heic_to_jpeg:=false}"; fi
if [ "$(grep -c "debug_logging=" "${config_file}")" -eq 0 ]; then echo debug_logging="${debug_logging:=false}"; fi
if [ "$(grep -c "delete_accompanying=" "${config_file}")" -eq 0 ]; then echo delete_accompanying="${delete_accompanying:=false}"; fi
if [ "$(grep -c "delete_after_download=" "${config_file}")" -eq 0 ]; then echo delete_after_download="${delete_after_download:=false}"; fi
if [ "$(grep -c "delete_notifications=" "${config_file}")" -eq 0 ]; then echo delete_notifications="${delete_notifications:=true}"; fi
if [ "$(grep -c "dingtalk_token=" "${config_file}")" -eq 0 ]; then echo dingtalk_token="${dingtalk_token}"; fi
if [ "$(grep -c "directory_permissions=" "${config_file}")" -eq 0 ]; then echo directory_permissions="${directory_permissions:=750}"; fi
if [ "$(grep -c "discord_id=" "${config_file}")" -eq 0 ]; then echo discord_id="${discord_id}"; fi
if [ "$(grep -c "discord_token=" "${config_file}")" -eq 0 ]; then echo discord_token="${discord_token}"; fi
if [ "$(grep -c "download_notifications=" "${config_file}")" -eq 0 ]; then echo download_notifications="${download_notifications:=true}"; fi
if [ "$(grep -c "download_path=" "${config_file}")" -eq 0 ]; then echo download_path="${download_path}"; fi
if [ "$(grep -c "file_permissions=" "${config_file}")" -eq 0 ]; then echo file_permissions="${file_permissions:=640}"; fi
if [ "$(grep -c "folder_structure=" "${config_file}")" -eq 0 ]; then echo folder_structure="${folder_structure:={:%Y/%m/%d\}}"; fi
if [ "$(grep -c "gotify_app_token=" "${config_file}")" -eq 0 ]; then echo gotify_app_token="${gotify_app_token}"; fi
if [ "$(grep -c "group=" "${config_file}")" -eq 0 ]; then echo group="${group:=group}"; fi
if [ "$(grep -c "group_id=" "${config_file}")" -eq 0 ]; then echo group_id="${group_id:=1000}"; fi
if [ "$(grep -c "icloud_china=" "${config_file}")" -eq 0 ]; then echo icloud_china="${icloud_china}"; fi
if [ "$(grep -c "iyuu_token=" "${config_file}")" -eq 0 ]; then echo iyuu_token="${iyuu_token}"; fi
if [ "$(grep -c "jpeg_path=" "${config_file}")" -eq 0 ]; then echo jpeg_path="${jpeg_path}"; fi
if [ "$(grep -c "jpeg_quality=" "${config_file}")" -eq 0 ]; then echo jpeg_quality="${jpeg_quality:=90}"; fi
if [ "$(grep -c "nextcloud_delete=" "${config_file}")" -eq 0 ]; then echo nextcloud_delete="${nextcloud_delete:=false}"; fi
if [ "$(grep -c "nextcloud_upload=" "${config_file}")" -eq 0 ]; then echo nextcloud_upload="${nextcloud_upload:=false}"; fi
if [ "$(grep -c "nextcloud_url=" "${config_file}")" -eq 0 ]; then echo nextcloud_url="${nextcloud_url}"; fi
if [ "$(grep -c "nextcloud_username=" "${config_file}")" -eq 0 ]; then echo nextcloud_username="${nextcloud_username}"; fi
if [ "$(grep -c "nextcloud_password=" "${config_file}")" -eq 0 ]; then echo nextcloud_password="${nextcloud_password}"; fi
if [ "$(grep -c "notification_days=" "${config_file}")" -eq 0 ]; then echo notification_days="${notification_days:=7}"; fi
if [ "$(grep -c "notification_type=" "${config_file}")" -eq 0 ]; then echo notification_type="${notification_type}"; fi
if [ "$(grep -c "photo_album=" "${config_file}")" -eq 0 ]; then echo photo_album="${photo_album}"; fi
#if [ "$(grep -c "photo_library=" "${config_file}")" -eq 0 ]; then echo photo_library="${photo_library}"; fi
if [ "$(grep -c "photo_size=" "${config_file}")" -eq 0 ]; then echo photo_size="${photo_size:=original}"; fi
if [ "$(grep -c "prowl_api_key=" "${config_file}")" -eq 0 ]; then echo prowl_api_key="${prowl_api_key}"; fi
if [ "$(grep -c "pushover_sound=" "${config_file}")" -eq 0 ]; then echo pushover_sound="${pushover_sound}"; fi
if [ "$(grep -c "pushover_token=" "${config_file}")" -eq 0 ]; then echo pushover_token="${pushover_token}"; fi
if [ "$(grep -c "pushover_user=" "${config_file}")" -eq 0 ]; then echo pushover_user="${pushover_user}"; fi
if [ "$(grep -c "recent_only=" "${config_file}")" -eq 0 ]; then echo recent_only="${recent_only}"; fi
if [ "$(grep -c "set_exif_datetime=" "${config_file}")" -eq 0 ]; then echo set_exif_datetime="${set_exif_datetime:=false}"; fi
if [ "$(grep -c "skip_album=" "${config_file}")" -eq 0 ]; then echo skip_album="${skip_album}"; fi
if [ "$(grep -c "single_pass=" "${config_file}")" -eq 0 ]; then echo single_pass="${single_pass:=false}"; fi
if [ "$(grep -c "skip_check=" "${config_file}")" -eq 0 ]; then echo skip_check="${skip_check:=false}"; fi
if [ "$(grep -c "skip_download=" "${config_file}")" -eq 0 ]; then echo skip_download="${skip_download:=false}"; fi
if [ "$(grep -c "skip_live_photos=" "${config_file}")" -eq 0 ]; then echo skip_live_photos="${skip_live_photos:=false}"; fi
if [ "$(grep -c "synchronisation_delay=" "${config_file}")" -eq 0 ]; then echo synchronisation_delay="${synchronisation_delay:=0}"; fi
if [ "$(grep -c "synchronisation_interval=" "${config_file}")" -eq 0 ]; then echo synchronisation_interval="${synchronisation_interval:=86400}"; fi
if [ "$(grep -c "synology_ignore_path=" "${config_file}")" -eq 0 ]; then echo synology_ignore_path="${synology_ignore_path:=false}"; fi
if [ "$(grep -c "telegram_chat_id=" "${config_file}")" -eq 0 ]; then echo telegram_chat_id="${telegram_chat_id}"; fi
if [ "$(grep -c "telegram_polling=" "${config_file}")" -eq 0 ]; then echo telegram_polling="${telegram_polling:=true}"; fi
if [ "$(grep -c "telegram_server=" "${config_file}")" -eq 0 ]; then echo telegram_server="${telegram_server}"; fi
if [ "$(grep -c "telegram_silent_file_notifications=" "${config_file}")" -eq 0 ]; then echo telegram_silent_file_notifications="${telegram_silent_file_notifications}"; fi
if [ "$(grep -c "telegram_token=" "${config_file}")" -eq 0 ]; then echo telegram_token="${telegram_token}"; fi
if [ "$(grep -c "trigger_nextlcoudcli_synchronisation=" "${config_file}")" -eq 0 ]; then echo trigger_nextlcoudcli_synchronisation="${trigger_nextlcoudcli_synchronisation}"; fi
if [ "$(grep -c "until_found=" "${config_file}")" -eq 0 ]; then echo until_found="${until_found}"; fi
if [ "$(grep -c "user=" "${config_file}")" -eq 0 ]; then echo user="${user:=user}"; fi
if [ "$(grep -c "user_id=" "${config_file}")" -eq 0 ]; then echo user_id="${user_id:=1000}"; fi
if [ "$(grep -c "webhook_https=" "${config_file}")" -eq 0 ]; then echo webhook_https="${webhook_https:=false}"; fi
if [ "$(grep -c "webhook_id=" "${config_file}")" -eq 0 ]; then echo webhook_id="${webhook_id}"; fi
if [ "$(grep -c "webhook_path=" "${config_file}")" -eq 0 ]; then echo webhook_path="${webhook_path:=/api/webhook/}"; fi
if [ "$(grep -c "webhook_port=" "${config_file}")" -eq 0 ]; then echo webhook_port="${webhook_port:=8123}"; fi
if [ "$(grep -c "webhook_server=" "${config_file}")" -eq 0 ]; then echo webhook_server="${webhook_server}"; fi
if [ "$(grep -c "wecom_id=" "${config_file}")" -eq 0 ]; then echo wecom_id="${wecom_id}"; fi
if [ "$(grep -c "wecom_proxy=" "${config_file}")" -eq 0 ]; then echo wecom_proxy="${wecom_proxy}"; fi
if [ "$(grep -c "wecom_secret=" "${config_file}")" -eq 0 ]; then echo wecom_secret="${wecom_secret}"; fi
} >> "${config_file}"
if [ "${albums_with_dates}" ]; then sed -i "s%^albums_with_dates=.*%albums_with_dates=${albums_with_dates}%" "${config_file}"; fi
if [ "${apple_id}" ]; then sed -i "s%^apple_id=.*%apple_id=${apple_id}%" "${config_file}"; fi
if [ "${authentication_type}" ]; then sed -i "s%^authentication_type=.*%authentication_type=${authentication_type}%" "${config_file}"; fi
if [ "${auth_china}" ]; then sed -i "s%^auth_china=.*%auth_china=${auth_china}%" "${config_file}"; fi
if [ "${auto_delete}" ]; then sed -i "s%^auto_delete=.*%auto_delete=${auto_delete}%" "${config_file}"; fi
if [ "${bark_device_key}" ]; then sed -i "s%^bark_device_key=.*%bark_device_key=${bark_device_key}%" "${config_file}"; fi
if [ "${bark_server}" ]; then sed -i "s%^bark_server=.*%bark_server=${bark_server}%" "${config_file}"; fi
if [ "${convert_heic_to_jpeg}" ]; then sed -i "s%^convert_heic_to_jpeg=.*%convert_heic_to_jpeg=${convert_heic_to_jpeg}%" "${config_file}"; fi
if [ "${debug_logging}" ]; then sed -i "s%^debug_logging=.*%debug_logging=${debug_logging}%" "${config_file}"; fi
if [ "${delete_accompanying}" ]; then sed -i "s%^delete_accompanying=.*%delete_accompanying=${delete_accompanying}%" "${config_file}"; fi
if [ "${delete_after_download}" ]; then sed -i "s%^delete_after_download=.*%delete_after_download=${delete_after_download}%" "${config_file}"; fi
if [ "${delete_notification}" ]; then sed -i "s%^delete_notification=.*%delete_notification=${delete_notification}%" "${config_file}"; fi
if [ "${dingtalk_token}" ]; then sed -i "s%^dingtalk_token=.*%dingtalk_token=${dingtalk_token}%" "${config_file}"; fi
if [ "${directory_permissions}" ]; then sed -i "s%^directory_permissions=.*%directory_permissions=${directory_permissions}%" "${config_file}"; fi
if [ "${discord_id}" ]; then sed -i "s%^discord_id=.*%discord_id=${discord_id}%" "${config_file}"; fi
if [ "${discord_token}" ]; then sed -i "s%^discord_token=.*%discord_token=${discord_token}%" "${config_file}"; fi
if [ "${download_notifications}" ]; then sed -i "s%^download_notifications=.*%download_notifications=${download_notifications}%" "${config_file}"; fi
if [ "${download_path}" ]; then sed -i "s%^download_path=.*%download_path=${download_path}%" "${config_file}"; fi
if [ "${file_permissions}" ]; then sed -i "s%^file_permissions=.*%file_permissions=${file_permissions}%" "${config_file}"; fi
if [ "${folder_structure}" ]; then
sanitised_folder_structure="${folder_structure//\//\\/}"
sed -i "s@^folder_structure=.*@folder_structure=${sanitised_folder_structure}@" "${config_file}"
fi
if [ "${gotify_app_token}" ]; then sed -i "s%^gotify_app_token=.*%gotify_app_token=${gotify_app_token}%" "${config_file}"; fi
if [ "${group}" ]; then sed -i "s%^group=.*%group=${group}%" "${config_file}"; fi
if [ "${group_id}" ]; then sed -i "s%^group_id=.*%group_id=${group_id}%" "${config_file}"; fi
if [ "${icloud_china}" ]; then sed -i "s%^icloud_china=.*%icloud_china=${icloud_china}%" "${config_file}"; fi
if [ "${iyuu_token}" ]; then sed -i "s%^iyuu_token=.*%iyuu_token=${iyuu_token}%" "${config_file}"; fi
if [ "${jpeg_path}" ]; then sed -i "s%^jpeg_path=.*%jpeg_path=${jpeg_path}%" "${config_file}"; fi
if [ "${jpeg_quality}" ]; then sed -i "s%^jpeg_quality=.*%jpeg_quality=${jpeg_quality}%" "${config_file}"; fi
if [ "${nextcloud_delete}" ]; then sed -i "s%^nextcloud_delete=.*%nextcloud_delete=${nextcloud_delete}%" "${config_file}"; fi
if [ "${nextcloud_upload}" ]; then sed -i "s%^nextcloud_upload=.*%nextcloud_upload=${nextcloud_upload}%" "${config_file}"; fi
if [ "${nextcloud_url}" ]; then sed -i "s%^nextcloud_url=.*%nextcloud_url=${nextcloud_url}%" "${config_file}"; fi
if [ "${nextcloud_username}" ]; then sed -i "s%^nextcloud_username=.*%nextcloud_username=${nextcloud_username}%" "${config_file}"; fi
if [ "${nextcloud_password}" ]; then sed -i "s%^nextcloud_password=.*%nextcloud_password=${nextcloud_password}%" "${config_file}"; fi
if [ "${notification_days}" ]; then sed -i "s%^notification_days=.*%notification_days=${notification_days}%" "${config_file}"; fi
if [ "${notification_type}" ]; then sed -i "s%^notification_type=.*%notification_type=${notification_type}%" "${config_file}"; fi
if [ "${photo_album}" ]; then sed -i "s%^photo_album=.*%photo_album=\"${photo_album}\"%" "${config_file}"; fi
#if [ "${photo_library}" ]; then sed -i "s%^photo_library=.*%photo_library=${photo_library}%" "${config_file}"; fi
if [ "${photo_size}" ]; then sed -i "s%^photo_size=.*%photo_size=${photo_size}%" "${config_file}"; fi
if [ "${prowl_api_key}" ]; then sed -i "s%^prowl_api_key=.*%prowl_api_key=${prowl_api_key}%" "${config_file}"; fi
if [ "${pushover_sound}" ]; then sed -i "s%^pushover_sound=.*%pushover_sound=${pushover_sound}%" "${config_file}"; fi
if [ "${pushover_token}" ]; then sed -i "s%^pushover_token=.*%pushover_token=${pushover_token}%" "${config_file}"; fi
if [ "${pushover_user}" ]; then sed -i "s%^pushover_user=.*%pushover_user=${pushover_user}%" "${config_file}"; fi
if [ "${recent_only}" ]; then sed -i "s%^recent_only=.*%recent_only=${recent_only}%" "${config_file}"; fi
if [ "${set_exif_datetime}" ]; then sed -i "s%^set_exif_datetime=.*%set_exif_datetime=${set_exif_datetime}%" "${config_file}"; fi
if [ "${single_pass}" ]; then sed -i "s%^single_pass=.*%single_pass=${single_pass}%" "${config_file}"; fi
if [ "${skip_album}" ]; then sed -i "s%^skip_album=.*%skip_album=\"${skip_album}\"%" "${config_file}"; fi
if [ "${skip_check}" ]; then sed -i "s%^skip_check=.*%skip_check=${skip_check}%" "${config_file}"; fi
if [ "${skip_download}" ]; then sed -i "s%^skip_download=.*%skip_download=${skip_download}%" "${config_file}"; fi
if [ "${skip_live_photos}" ]; then sed -i "s%^skip_live_photos=.*%skip_live_photos=${skip_live_photos}%" "${config_file}"; fi
if [ "${synchronisation_delay}" ]; then sed -i "s%^synchronisation_delay=.*%synchronisation_delay=${synchronisation_delay}%" "${config_file}"; fi
if [ "${synchronisation_interval}" ]; then sed -i "s%^synchronisation_interval=.*%synchronisation_interval=${synchronisation_interval}%" "${config_file}"; fi
if [ "${synology_ignore_path}" ]; then sed -i "s%^synology_ignore_path=.*%synology_ignore_path=${synology_ignore_path}%" "${config_file}"; fi
if [ "${telegram_chat_id}" ]; then sed -i "s%^telegram_chat_id=.*%telegram_chat_id=${telegram_chat_id}%" "${config_file}"; fi
if [ "${telegram_polling}" ]; then sed -i "s%^telegram_polling=.*%telegram_polling=${telegram_polling}%" "${config_file}"; fi
if [ "${telegram_server}" ]; then sed -i "s%^telegram_server=.*%telegram_server=${telegram_server}%" "${config_file}"; fi
if [ "${telegram_silent_file_notifications}" ]; then sed -i "s%^telegram_silent_file_notifications=.*%telegram_silent_file_notifications=${telegram_silent_file_notifications}%" "${config_file}"; fi
if [ "${telegram_token}" ]; then sed -i "s%^telegram_token=.*%telegram_token=${telegram_token}%" "${config_file}"; fi
if [ "${trigger_nextlcoudcli_synchronisation}" ]; then sed -i "s%^trigger_nextlcoudcli_synchronisation=.*%trigger_nextlcoudcli_synchronisation=${trigger_nextlcoudcli_synchronisation}%" "${config_file}"; fi
if [ "${until_found}" ]; then sed -i "s%^until_found=.*%until_found=${until_found}%" "${config_file}"; fi
if [ "${user}" ]; then sed -i "s%^user=.*%user=${user}%" "${config_file}"; fi
if [ "${user_id}" ]; then sed -i "s%^user_id=.*%user_id=${user_id}%" "${config_file}"; fi
if [ "${webhook_https}" ]; then sed -i "s%^webhook_https=.*%webhook_https=${webhook_https}%" "${config_file}"; fi
if [ "${webhook_id}" ]; then sed -i "s%^webhook_id=.*%webhook_id=${webhook_id}%" "${config_file}"; fi
if [ "${webhook_path}" ]; then sed -i "s%^webhook_path=.*%webhook_path=${webhook_path}%" "${config_file}"; fi
if [ "${webhook_port}" ]; then sed -i "s%^webhook_port=.*%webhook_port=${webhook_port}%" "${config_file}"; fi
if [ "${webhook_server}" ]; then sed -i "s%^webhook_server=.*%webhook_server=${webhook_server}%" "${config_file}"; fi
if [ "${wecom_id}" ]; then sed -i "s%^wecom_id=.*%wecom_id=${wecom_id}%" "${config_file}"; fi
if [ "${wecom_proxy}" ]; then sed -i "s%^wecom_proxy=.*%wecom_proxy=${wecom_proxy}%" "${config_file}"; fi
if [ "${wecom_secret}" ]; then sed -i "s%^wecom_secret=.*%wecom_secret=${wecom_secret}%" "${config_file}"; fi
mv "${config_file}" "${config_file}.tmp"
sort "${config_file}.tmp" --output="${config_file}"
chmod --reference="${config_file}.tmp" "${config_file}"
rm "${config_file}.tmp"
sed -i 's/=True/=true/g' "${config_file}"
sed -i 's/=False/=false/g' "${config_file}"
sed -i '/photo_library=/d' "${config_file}"
sed -i 's/authentication_type=2FA/authentication_type=MFA/' "${config_file}"
}
Initialise(){
echo
LogInfo "***** boredazfcuk/icloudpd container for icloud_photo_downloader v1.0.$(cat /build_version.txt) started *****"
LogInfo "***** For support, please go here: https://github.com/boredazfcuk/docker-icloudpd *****"
LogInfo "$(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')"
LogInfo "Python version: $(python3 --version | awk '{print $2}')"
config_file="${config_dir}/icloudpd.conf"
initialise_config_file
LogInfo "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
if [ ! -d "/tmp/icloudpd" ]; then mkdir --parents "/tmp/icloudpd"; fi
if [ -f "/tmp/icloudpd/icloudpd_check_exit_code" ]; then rm "/tmp/icloudpd/icloudpd_check_exit_code"; fi
if [ -f "/tmp/icloudpd/icloudpd_download_exit_code" ]; then rm "/tmp/icloudpd/icloudpd_download_exit_code"; fi
if [ -f "/tmp/icloudpd/icloudpd_check_error" ]; then rm "/tmp/icloudpd/icloudpd_check_error"; fi
if [ -f "/tmp/icloudpd/icloudpd_download_error" ]; then rm "/tmp/icloudpd/icloudpd_download_error"; fi
if [ -f "/tmp/icloudpd/icloudpd_sync.log" ]; then rm "/tmp/icloudpd/icloudpd_sync.log"; fi
if [ -f "/tmp/icloudpd/icloudpd_tracert.err" ]; then rm "/tmp/icloudpd/icloudpd_tracert.err"; fi
touch "/tmp/icloudpd/icloudpd_check_exit_code"
touch "/tmp/icloudpd/icloudpd_download_exit_code"
touch "/tmp/icloudpd/icloudpd_check_error"
touch "/tmp/icloudpd/icloudpd_download_error"
touch "/tmp/icloudpd/icloudpd_sync.log"
touch "/tmp/icloudpd/icloudpd_tracert.err"
if [ -z "${apple_id}" ]; then
LogError "Apple ID not set - exiting"
sleep 120
exit 1
fi
if [ "${apple_password}" -a "${apple_password}" != "usekeyring" ]; then
LogError "Apple password configured with variable which is no longer supported. Please add password to system keyring - exiting"
sleep 120
exit 1
fi
if [ "${apple_password}" = "usekeyring" ]; then
LogWarning "Apple password variable set to 'userkeyring'. This variable can now be removed as it is now the only supported option, so obsolete - continue in 2 minutes"
sleep 120
fi
LogDebug "Running user id: $(id --user)"
LogDebug "Running group id: $(id --group)"
LogDebug "Local user: ${user:=user}:${user_id:=1000}"
LogDebug "Local group: ${group:=group}:${group_id:=1000}"
LogDebug "Force GID: ${force_gid:=false}"
LogDebug "LAN IP Address: ${lan_ip}"
LogDebug "Default gateway: $(ip route | grep default | awk '{print $3}')"
LogDebug "DNS server: $(cat /etc/resolv.conf | grep nameserver | 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
LogWarning "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
LogError "Cannot find ${icloud_domain} IP address. Please check your DNS settings - exiting"
sleep 120
exit 1
fi
done
LogDebug "IP address for ${icloud_domain}: ${icloud_dot_com}"
if [ "$(traceroute -q 1 -w 1 ${icloud_domain} >/dev/null 2>/tmp/icloudpd/icloudpd_tracert.err; echo $?)" = 1 ]; then
LogError "No route to ${icloud_domain} found. Please check your container's network settings - exiting"
LogError "Error debug - $(cat /tmp/icloudpd/icloudpd_tracert.err)"
sleep 120
exit 1
else
LogDebug "Route check to ${icloud_domain} successful"
fi
if [ "${debug_logging:=false}" = true ]; then
LogDebug "Apple ID: (hidden)"
else
LogInfo "Apple ID: ${apple_id}"
fi
LogInfo "Authentication Type: ${authentication_type:=MFA}"
if [ "${debug_logging}" = true ]; then
LogDebug "Cookie path: ${config_dir}/(hidden)"
else
LogInfo "Cookie path: ${config_dir}/${cookie_file}"
fi
LogInfo "Cookie expiry notification period: ${notification_days:=7}"
LogInfo "Download destination directory: ${download_path:=/home/${user}/iCloud}"
if [ ! -d "${download_path}" ]; then
LogInfo "Download directory does not exist."
LogInfo "Creating ${download_path} and configuring permissions."
mkdir --parents "${download_path}"
SetOwnerAndPermissionsDownloads
fi
LogInfo "Folder structure: ${folder_structure:={:%Y/%m/%d\}}"
LogDebug "Directory permissions: ${directory_permissions:=750}"
LogDebug "File permissions: ${file_permissions:=640}"
if [ "${syncronisation_interval}" ]; then
LogWarning "The syncronisation_interval variable contained a typo. This has now been corrected to synchronisation_interval. Please update your container. Defaulting to one sync per 24 hour period"
synchronisation_interval="86400"
fi
LogInfo "Synchronisation interval: ${synchronisation_interval}"
if [ "${synchronisation_interval}" -lt 43200 ]; then
LogWarning "Setting synchronisation_interval to less than 43200 (12 hours) may cause throttling by Apple."
LogWarning "If you run into the following error: "
LogWarning " - private db access disabled for this account. Please wait a few minutes then try again. The remote servers might be trying to throttle requests. (ACCESS_DENIED)"
LogWarning "then please change your synchronisation_interval to 43200 or greater and switch the container off for 6-12 hours so Apple's throttling expires. Continuing in 2 minutes"
if [ "${warnings_acknowledged:=false}" = true ]; then
LogDebug "Throttle warning acknowledged"
else
sleep 120
fi
fi
LogInfo "Synchronisation delay (minutes): ${synchronisation_delay}"
LogInfo "Set EXIF date/time: ${set_exif_datetime:=false}"
LogInfo "Auto delete: ${auto_delete:=false}"
LogInfo "Delete after download: ${delete_after_download:=false}"
if [ "${auto_delete}" != false -a "${delete_after_download}" != false ]; then
LogError "The variables auto_delete and delete_after_download cannot both be configured at the same time. Please choose one or the other - exiting"
sleep 120
exit 1
fi
LogInfo "Photo size: ${photo_size:=original}"
LogInfo "Single pass mode: ${single_pass:=false}"
if [ "${single_pass}" = true ]; then
LogDebug "Single pass mode enabled. Disabling download check"
skip_check=true
fi
LogInfo "Skip download check: ${skip_check:=false}"
LogInfo "Skip live photos: ${skip_live_photos:=false}"
if [ "${recent_only}" ]; then
LogInfo "Number of most recently added photos to download: ${recent_only}"
else
LogInfo "Number of most recently added photos to download: Download All Photos"
fi
if [ "${photo_album}" ]; then
LogInfo "Downloading photos from album(s): ${photo_album}"
# elif [ "${photo_library}" ]; then
# LogInfo "Downloading photos from library: ${photo_library}"
else
LogInfo "Downloading photos from album: Download All Photos"
fi
if [ "${until_found}" ]; then
LogInfo "Stop downloading when prexisiting files count is: ${until_found}"
else
LogInfo "Stop downloading when prexisiting files count is: Download All Photos"
fi
if [ "${skip_live_photos}" = false ]; then
LogInfo "Live photo size: ${live_photo_size:=original}"
fi
LogInfo "Skip videos: ${skip_videos:=false}"
if [ "${command_line_options}" ]; then
LogWarning "Additional command line options supplied: ${command_line_options}"
LogWarning "Additional command line options are no longer supported and will be ignored. Please specify all options using the dedicated variables."
fi
LogInfo "Convert HEIC to JPEG: ${convert_heic_to_jpeg:=false}"
if [ "${convert_heic_to_jpeg}" = true ]; then
LogDebug "JPEG conversion quality: ${jpeg_quality:=90}"
fi
if [ "${jpeg_path}" ]; then
LogInfo "Converted JPEGs path: ${jpeg_path}"
fi
if [ "${delete_accompanying:=false}" = true -a -z "${warnings_acknowledged}" ]; then
LogInfo "Delete accompanying files (.JPG/.HEIC.MOV)"
LogWarning "This feature deletes files from your local disk. Please use with caution. I am not responsible for any data loss."
LogWarning "This feature cannot be used if the 'folder_structure' variable is set to 'none' and also, 'set_exif_datetime' must be 'False'"
LogWarning "These two settings will increase the chances of de-duplication happening, which could result in the wrong files being removed. Continuing in 2 minutes."
if [ "${warnings_acknowledged:=false}" = true ]; then
LogInfo "File deletion warning accepted"
else
sleep 120
fi
fi
if [ "${notification_type}" ]; then
ConfigureNotifications
fi
LogInfo "Downloading from: ${icloud_domain}"
if [ "${icloud_china}" = true ]; then
if [ "${auth_china:=false}" = true ]; then
auth_domain="cn"
else
LogWarning "You have the icloud_china variable set, but auth_china is false. Are you sure this is correct?"
fi
fi
LogInfo "Authentication domain: ${auth_domain:=com}"
if [ "${trigger_nextlcoudcli_synchronisation}" ]; then
LogDebug "Nextcloud synchronisation trigger: Enabled"
else
LogDebug "Nextcloud synchronisation trigger: Disabled"
fi
if [ "${nextcloud_upload}" = true ]; then
if [ "${nextcloud_url}" -a "${nextcloud_username}" -a "${nextcloud_password}" ]; then
LogInfo "Nextcloud upload: Enabled"
LogInfo "Nextcloud URL: ${nextcloud_url}"
LogInfo "Nextcloud username: ${nextcloud_username}"
else
LogError "Nextcloud upload: Missing mandatory variables. Disabling."
unset nextlcoud_upload
fi
else
LogDebug "Nextcloud upload: Disabled"
fi
if [ "${synology_ignore_path}" = true ]; then
LogInfo "Ignore Synology extended attribute directories: Enabled"
ignore_path="*/@eaDir*"
else
LogInfo "Ignore Synology extended attribute directories: Disabled"
ignore_path=""
fi
if [ ! -d "/home/${user}/.local/share/" ]; then
LogDebug "Creating directory: /home/${user}/.local/share/"
mkdir --parents "/home/${user}/.local/share/"
fi
if [ ! -d "${config_dir}/python_keyring/" ]; then
LogDebug "Creating directory: ${config_dir}/python_keyring/"
mkdir --parents "${config_dir}/python_keyring/"
fi
if [ ! -L "/home/${user}/.local/share/python_keyring" ]; then
LogDebug "Creating symbolic link: /home/${user}/.local/share/python_keyring/ to: ${config_dir}/python_keyring/ directory"
ln --symbolic --force "${config_dir}/python_keyring/" "/home/${user}/.local/share/"
fi
}
LogInfo(){
local log_message
log_message="${1}"
echo "$(date '+%Y-%m-%d %H:%M:%S') INFO ${log_message}"
}
LogInfoN(){
local log_message
log_message="${1}"
echo -n "$(date '+%Y-%m-%d %H:%M:%S') INFO ${log_message}... "
}
LogWarning(){
local log_message
log_message="${1}"
echo "$(date '+%Y-%m-%d %H:%M:%S') WARNING ${log_message}"
}
LogError(){
local log_message
log_message="${1}"
echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR ${log_message}"
}
LogDebug(){
if [ "${debug_logging}" = true ]; then
local log_message
log_message="${1}"
echo "$(date '+%Y-%m-%d %H:%M:%S') DEBUG ${log_message}"
fi
}
CleanNotificationTitle(){
if [ "${notification_title}" ]; then
notification_title="${notification_title//[^a-zA-Z0-9_ ]/}"
LogDebug "Cleaned notification title: ${notification_title}"
else
LogDebug "Notification title: ${notification_title:=boredazfcuk/iCloudPD}"
fi
}
ConfigureNotifications(){
if [ -z "${prowl_api_key}" -a -z "${pushover_token}" -a -z "${telegram_token}" -a -z "${webhook_id}" -a -z "${dingtalk_token}" -a -z "${discord_token}" -a -z "${iyuu_token}" -a -z "${wecom_secret}" -a -z "${gotify_app_token}" -a -z "${bark_device_key}" ]; then
LogWarning "${notification_type} notifications enabled, but API key/token/secret not set - disabling notifications"
unset notification_type
else
if [ "${notification_type}" = "Prowl" -a "${prowl_api_key}" ]; then
LogInfo "${notification_type} notifications enabled"
CleanNotificationTitle
if [ "${debug_logging}" = true ]; then
LogDebug "${notification_type} api key: (hidden)"
else
LogInfo "${notification_type} api key: ${prowl_api_key}"
fi
notification_url="https://api.prowlapp.com/publicapi/add"
elif [ "${notification_type}" = "Pushover" -a "${pushover_user}" -a "${pushover_token}" ]; then
LogInfo "${notification_type} notifications enabled"
CleanNotificationTitle
if [ "${debug_logging}" = true ]; then
LogDebug "${notification_type} user: (hidden)"
LogDebug "${notification_type} token: (hidden)"
else
LogInfo "${notification_type} user: ${pushover_user}"
LogInfo "${notification_type} token: ${pushover_token}"
fi
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)
LogDebug "${notification_type} sound: ${pushover_sound}"
;;
*)
LogDebug "${notification_type} sound not recognised. Using default"
unset pushover_sound
esac
fi
notification_url="https://api.pushover.net/1/messages.json"
elif [ "${notification_type}" = "Telegram" -a "${telegram_token}" -a "${telegram_chat_id}" ]; then
if [ "${telegram_server}" ] ; then
notification_url="https://${telegram_server}/bot${telegram_token}/sendMessage"
else
notification_url="https://api.telegram.org/bot${telegram_token}/sendMessage"
fi
LogInfo "${notification_type} notifications enabled"
CleanNotificationTitle
if [ "${debug_logging}" = true ]; then
LogDebug "${notification_type} token: (hidden)"
LogDebug "${notification_type} chat id: (hidden)"
LogDebug "${notification_type} polling: ${telegram_polling}"
LogDebug "${notification_type} notification URL: (hidden)"
else
LogInfo "${notification_type} token: ${telegram_token}"
LogInfo "${notification_type} chat id: ${telegram_chat_id}"
LogInfo "${notification_type} polling: ${telegram_polling}"
LogInfo "${notification_type} notification URL: ${notification_url}"
fi
if [ "${telegram_polling}" = true ]; then
telegram_update_id_offset_file="${config_dir}/telegram_update_id.num"
if [ ! -f "${telegram_update_id_offset_file}" ]; then
echo -n 0 > "${telegram_update_id_offset_file}"
fi
LogInfo "Check Telegram bot initialised..."
bot_check="$(curl --silent -X POST "https://api.telegram.org/bot${telegram_token}/getUpdates" | jq .ok)"
if [ "${bot_check}" ]; then
LogInfo " - Bot has been initialised."
else
LogInfo " - Bot has not been initialised or needs reinitialising. Please send a message to the bot from your iDevice and restart the container. Disabling remote wake"
sleep 10
telegram_polling=false
fi
telegram_update_id_offset="$(head -1 ${telegram_update_id_offset_file})"
LogInfo "Latest update id: ${telegram_update_id_offset}"
fi
if [ "${telegram_silent_file_notifications}" ]; then telegram_silent_file_notifications=true; fi
LogDebug "${notification_type} silent file notifications: ${telegram_silent_file_notifications:=false}"
elif [ "${notification_type}" = "openhab" -a "${webhook_server}" -a "${webhook_id}" ]; then
if [ "${webhook_https}" = true ]; then
webhook_scheme="https"
else
webhook_scheme="http"
fi
LogInfo "${notification_type} notifications enabled"
LogDebug "${notification_type} server: ${webhook_server}"
LogDebug "${notification_type} port: ${webhook_port:=8123}"
LogDebug "${notification_type} path: ${webhook_path:=/rest/items/}"
LogDebug "${notification_type} ID: ${webhook_id}"
notification_url="${webhook_scheme}://${webhook_server}:${webhook_port}${webhook_path}${webhook_id}"
LogDebug "${notification_type} notification URL: ${notification_url}"
elif [ "${notification_type}" = "Webhook" -a "${webhook_server}" -a "${webhook_id}" ]; then
if [ "${webhook_https}" = true ]; then
webhook_scheme="https"
else
webhook_scheme="http"
fi
LogInfo "${notification_type} notifications enabled"
CleanNotificationTitle
LogDebug "${notification_type} server: ${webhook_server}"
LogDebug "${notification_type} port: ${webhook_port:=8123}"
LogDebug "${notification_type} path: ${webhook_path:=/api/webhook/}"
LogDebug "${notification_type} ID: ${webhook_id}"
notification_url="${webhook_scheme}://${webhook_server}:${webhook_port}${webhook_path}${webhook_id}"
LogDebug "${notification_type} notification URL: ${notification_url}"
LogDebug "${notification_type} body keyword: ${webhook_body:=data}"
elif [ "${notification_type}" = "Discord" -a "${discord_id}" -a "${discord_token}" ]; then
LogInfo "${notification_type} notifications enabled"
CleanNotificationTitle
if [ "${debug_logging}" = true ]; then
LogDebug "${notification_type} Discord ID: (hidden)"
LogDebug "${notification_type} Discord token: (hidden)"
notification_url="https://discord.com/api/webhooks/${discord_id}/${discord_token}"
LogDebug "${notification_type} notification URL: (hidden)"
else
LogInfo "${notification_type} Discord ID: ${discord_id}"
LogInfo "${notification_type} Discord token: ${discord_token}"
notification_url="https://discord.com/api/webhooks/${discord_id}/${discord_token}"
LogInfo "${notification_type} notification URL: ${notification_url}"
fi
elif [ "${notification_type}" = "Dingtalk" -a "${dingtalk_token}" ]; then
notification_url="https://oapi.dingtalk.com/robot/send?access_token=${dingtalk_token}"
LogInfo "${notification_type} notifications enabled"
if [ "${debug_logging}" = true ]; then
LogDebug "${notification_type} token: (hidden)"
LogDebug "${notification_type} notification URL: (hidden)"
else
LogInfo "${notification_type} token: ${dingtalk_token}"
LogInfo "${notification_type} notification URL: ${notification_url}"
fi
elif [ "${notification_type}" = "IYUU" -a "${iyuu_token}" ]; then
notification_url="http://iyuu.cn/${iyuu_token}.send?"
LogInfo "${notification_type} notifications enabled"
if [ "${debug_logging}" = true ]; then
LogDebug "${notification_type} token: (hidden)"
LogDebug "${notification_type} notification URL: (hidden)"
else
LogInfo "${notification_type} token: ${iyuu_token}"
LogInfo "${notification_type} notification URL: ${notification_url}"
fi
elif [ "${notification_type}" = "WeCom" -a "${wecom_id}" -a "${wecom_secret}" ]; then
wecom_base_url="https://qyapi.weixin.qq.com"
if [ "${wecom_proxy}" ]; then
wecom_base_url="${wecom_proxy}"
LogDebug "${notification_type} notifications proxy enabled : ${wecom_proxy}"
fi
wecom_token_url="${wecom_base_url}/cgi-bin/gettoken?corpid=${wecom_id}&corpsecret=${wecom_secret}"
wecom_token="$(/usr/bin/curl -s -G "${wecom_token_url}" | awk -F\" '{print $10}')"
wecom_token_expiry="$(date --date='2 hour')"
notification_url="${wecom_base_url}/cgi-bin/message/send?access_token=${wecom_token}"
LogInfo "${notification_type} notifications enabled"
if [ "${debug_logging}" = true ]; then
LogDebug "${notification_type} token: (hidden)"
LogDebug "${notification_type} token expiry time: $(date -d "${wecom_token_expiry}")"
LogDebug "${notification_type} notification URL: (hidden)"
else
LogInfo "${notification_type} token: ${wecom_token}"
LogInfo "${notification_type} token expiry time: $(date -d "${wecom_token_expiry}")"
LogInfo "${notification_type} notification URL: ${notification_url}"
fi
elif [ "${notification_type}" = "Gotify" -a "${gotify_app_token}" -a "${gotify_server_url}" ]; then
LogInfo "${notification_type} notifications enabled"
CleanNotificationTitle
if [ "${debug_logging}" = true ]; then
LogDebug "${notification_type} token: (hidden)"
LogDebug "${notification_type} server URL: (hidden)"
else
LogInfo "${notification_type} token: ${gotify_app_token}"
LogInfo "${notification_type} server URL: ${gotify_server_url}"
fi
notification_url="https://${gotify_server_url}/message?token=${gotify_app_token}"
elif [ "${notification_type}" = "Bark" -a "${bark_device_key}" -a "${bark_server}" ]; then
LogInfo "${notification_type} notifications enabled"
CleanNotificationTitle
if [ "${debug_logging}" = true ]; then
LogDebug "${notification_type} device key: (hidden)"
LogDebug "${notification_type} server: (hidden)"
else
LogInfo "${notification_type} device key: ${bark_device_key}"
LogInfo "${notification_type} server: ${bark_server}"
fi
notification_url="http://${bark_server}/push"
else
LogWarning "$(date '+%Y-%m-%d %H:%M:%S') WARINING ${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_server_url bark_device_key bark_server
fi
if [ "${icloud_china}" = false ]; then
Notify "startup" "iCloudPD container started" "0" "iCloudPD container now starting for Apple ID: ${apple_id}"
else
Notify "startup" "iCloudPD container started" "0" "启动成功,开始同步当前 Apple ID 中的照片" "" "" "" "开始同步 ${name} 的 iCloud 图库" "Apple ID: ${apple_id}"
fi
if [ "${download_notifications:=true}" = true ]; then
LogDebug "Download notifications: Enabled"
else
LogDebug "Download notifications: Disabled"
unset download_notifications
fi
if [ "${delete_notifications:=true}" = true ]; then
LogDebug "Delete notifications: Enabled"
else
LogDebug "Delete notifications: Disabled"
unset delete_notifications
fi
fi
}
CreateGroup(){
if [ "$(grep -c "^${group}:x:${group_id}:" "/etc/group")" -eq 1 ]; then
LogDebug "Group, ${group}:${group_id}, already created"
else
if [ "$(grep -c "^${group}:" "/etc/group")" -eq 1 ]; then
LogDebug "Group name, ${group}, already in use - exiting"
sleep 120
exit 1
elif [ "$(grep -c ":x:${group_id}:" "/etc/group")" -eq 1 ]; then
if [ "${force_gid}" = true ]; then
group="$(grep ":x:${group_id}:" /etc/group | awk -F: '{print $1}')"
LogWarning "Group id, ${group_id}, already in use by the group: ${group} - continuing as force_gid variable has been set. Group name to use: ${group}"
else
LogError "Group id, ${group_id}, already in use by the group: ${group} - exiting. If you must to add your user to this pre-existing system group, please set the force_gid variable to True"
sleep 120
exit 1
fi
else
LogDebug "Creating group ${group}:${group_id}"
groupadd --gid "${group_id}" "${group}"
fi
fi
}
CreateUser(){
if [ "$(grep -c "^${user}:x:${user_id}:${group_id}" "/etc/passwd")" -eq 1 ]; then
LogDebug "User, ${user}:${user_id}, already created"
else
if [ "$(grep -c "^${user}:" "/etc/passwd")" -eq 1 ]; then
LogError "User name, ${user}, already in use - exiting"
sleep 120
exit 1
elif [ "$(grep -c ":x:${user_id}:$" "/etc/passwd")" -eq 1 ]; then
LogError "User id, ${user_id}, already in use - exiting"
sleep 120
exit 1
else
LogDebug "Creating user ${user}:${user_id}"
useradd --shell /bin/ash --gid "${group_id}" --uid "${user_id}" "${user}" --home-dir "/home/${user}" --badname
fi
fi
}
# ListLibraries(){
# LogInfo "Shared libraries available:"
# source /opt/icloudpd_latest/bin/activate
# LogDebug "Switched to icloudpd: $(icloudpd --version | awk '{print $3}')"
# shared_libraries="$(su "${user}" -c 'icloudpd --username "${0}" --cookie-directory "${1}" --domain "${2}" --directory /dev/null --list-libraries | sed "1d"' -- "${apple_id}" "${config_dir}" "${auth_domain}")"
# deactivate
# for library in ${shared_libraries}; do
# LogInfo " - ${library}"
# done
# }
ListAlbums(){
if [ "${authentication_type}" = "MFA" ]; then
CheckMFACookie
else
CheckWebCookie
fi
IFS=$'\n'
source /opt/icloudpd_latest/bin/activate
LogDebug "Switched to icloudpd: $(icloudpd --version | awk '{print $3}')"
if [ "${skip_download}" = false ]; then
available_albums="$(su "${user}" -c 'icloudpd --username "${0}" --cookie-directory "${1}" --domain "${2}" --directory /dev/null --list-albums | sed "1d"' -- "${apple_id}" "${config_dir}" "${auth_domain}")"
fi
deactivate
LogInfo "Albums available:"
for available_album in ${available_albums}; do
LogInfo " - ${available_album}"
done
IFS="${save_ifs}"
}
DeletePassword(){
if [ -f "${config_dir}/python_keyring/keyring_pass.cfg" ]; then
LogWarning "Keyring file ${config_dir}/python_keyring/keyring_pass.cfg exists, but --RemoveKeyring command line switch has been invoked. Removing in 30 seconds"
if [ -z "${warnings_acknowledged}" ]; then
sleep 30
else
LogInfo "Warnings acknowledged, removing immediately"
fi
rm "${config_dir}/python_keyring/keyring_pass.cfg"
else
LogError "Keyring file does not exist"
fi
}
ConfigurePassword(){
LogDebug "Configure password"
if [ -f "${config_dir}/python_keyring/keyring_pass.cfg" ]; then
if [ "$(grep -c "=" "${config_dir}/python_keyring/keyring_pass.cfg")" -eq 0 ]; then
LogDebug "Keyring file ${config_dir}/python_keyring/keyring_pass.cfg exists, but does not contain any credentials. Removing"
rm "${config_dir}/python_keyring/keyring_pass.cfg"
fi
fi
if [ ! -f "/home/${user}/.local/share/python_keyring/keyring_pass.cfg" ]; then
if [ "${initialise_container}" ]; then
LogDebug "Adding password to keyring file: ${config_dir}/python_keyring/keyring_pass.cfg"
if [ "${icloud_china}" = true ]; then
source /opt/icloudpd_v1.7.2_china/bin/activate
else
source /opt/icloudpd_latest/bin/activate
fi
LogDebug "Switched to icloudpd: $(icloudpd --version | awk '{print $3}')"
su "${user}" -c 'icloud --username "${0}"' -- "${apple_id}"
deactivate
else
LogError "Keyring file ${config_dir}/python_keyring/keyring_pass.cfg does not exist"
LogError " - Please add the your password to the system keyring using the --Initialise script command line option"
LogError " - Syntax: docker exec -it <container name> sync-icloud.sh --Initialise"
LogError " - Example: docker exec -it icloudpd sync-icloud.sh --Initialise"
LogError "Waiting for keyring file to be created..."
local counter
counter="${counter:=0}"
while [ ! -f "/home/${user}/.local/share/python_keyring/keyring_pass.cfg" ]; do
sleep 5
counter=$((counter + 1))
if [ "${counter}" -eq 360 ]; then
LogError "Keyring file has not appeared within 30 minutes. Restarting container..."
exit 1
fi
done
LogDebug "Keyring file exists, continuing"
fi
else
LogDebug "Using password stored in keyring file: ${config_dir}/python_keyring/keyring_pass.cfg"
fi
if [ ! -f "/home/${user}/.local/share/python_keyring/keyring_pass.cfg" ]; then
LogError "Keyring file does not exist. Please try again."
sleep 120
exit 1
fi
}
GenerateCookie(){
LogDebug "$(date '+%Y-%m-%d %H:%M:%S') INFO Correct owner on config directory, if required"
find "${config_dir}" ! -user "${user}" -exec chown "${user_id}" {} +
LogDebug "$(date '+%Y-%m-%d %H:%M:%S') INFO Correct group on config directory, if required"
find "${config_dir}" ! -group "${group}" -exec chgrp "${group_id}" {} +
if [ -f "${config_dir}/${cookie_file}" ]; then
mv "${config_dir}/${cookie_file}" "${config_dir}/${cookie_file}.bak"
fi
LogDebug "Generate ${authentication_type} cookie using password stored in keyring file"
if [ "${icloud_china}" = true ]; then
source /opt/icloudpd_v1.7.2_china/bin/activate
else
source /opt/icloudpd_latest/bin/activate
fi
LogDebug "Switched to icloudpd: $(icloudpd --version | awk '{print $3}')"
su "${user}" -c 'icloudpd --username "${0}" --cookie-directory "${1}" --directory /dev/null --only-print-filenames --recent 0' -- "${apple_id}" "${config_dir}"
deactivate
if [ "${authentication_type}" = "MFA" ]; then
if [ "$(grep -c "X-APPLE-WEBAUTH-HSA-TRUST" "${config_dir}/${cookie_file}")" -eq 1 ]; then
LogInfo "Two factor authentication cookie generated. Sync should now be successful"
else
LogError "Multi-factor authentication information missing from cookie. Authentication has failed"
LogError " - Was the correct password entered?"
LogError " - Was the multi-factor authentication code mistyped?"
LogError " - Can you log into ${icloud_domain} without receiving pop-up notifications?"
if [ "${icloud_china}" = true ]; then
LogError " - Are you based in China? You will need to set the icloud_china variable"
fi
fi
else
LogDebug "Web cookie generated. Sync should now be successful"
fi
}
CheckMount(){
LogInfo "Check download directory mounted correctly..."
if [ ! -f "${download_path}/.mounted" ]; then
LogWarning "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
LogError "Failsafe file has not appeared within 30 minutes. Restarting container..."
exit 1
fi
done
LogInfo "Failsafe file ${download_path}/.mounted exists, continuing"
}
SetOwnerAndPermissionsConfig(){
LogDebug "Set owner and group on icloudpd temp directory"
chown -R "${user_id}:${group_id}" "/tmp/icloudpd"
LogDebug "Set owner and group on config directory"
chown -R "${user_id}:${group_id}" "${config_dir}"
LogDebug "Set owner and group on keyring directory"
chown -R "${user_id}:${group_id}" "/home/${user}/.local"
if [ "$(su "${user}" -c "test -w \"${config_dir}/python_keyring/\"; echo $?")" -eq 0 ]; then
LogInfo "Directory is writable: ${config_dir}/python_keyring/"
else
LogError "Directory is not writable: ${config_dir}/python_keyring/"
sleep 120
exit 1
fi
if [ "$(su "${user}" -c "test -w \"/home/${user}/.local/share/\"; echo $?")" -eq 0 ]; then
LogInfo "Directory is writable: /home/${user}/.local/share/"
else
LogError "Directory is not writable: /home/${user}/.local/share/"
sleep 120
exit 1
fi
}
SetOwnerAndPermissionsDownloads(){
LogDebug "Set owner on iCloud directory, if required"
find "${download_path}" ! -type l ! -user "${user_id}" ! -path "${ignore_path}" -exec chown "${user_id}" {} +
LogDebug "Set group on iCloud directory, if required"
find "${download_path}" ! -type l ! -group "${group_id}" ! -path "${ignore_path}" -exec chgrp "${group_id}" {} +
LogDebug "Set ${directory_permissions} permissions on iCloud directories, if required"
find "${download_path}" -type d ! -perm "${directory_permissions}" ! -path "${ignore_path}" -exec chmod "${directory_permissions}" '{}' +
LogDebug "Set ${file_permissions} permissions on iCloud files, if required"
find "${download_path}" -type f ! -perm "${file_permissions}" ! -path "${ignore_path}" -exec chmod "${file_permissions}" '{}' +
}
CheckKeyringExists(){
if [ -f "${config_dir}/python_keyring/keyring_pass.cfg" ]; then
LogInfo "Keyring file exists, continuing"
else
LogError "Keyring does not exist"
LogError " - Please add your password to the system keyring by using the --Initialise script command line option"
LogError " - Syntax: docker exec -it <container name> sync-icloud.sh --Initialise"
LogError " - Example: docker exec -it icloudpd sync-icloud.sh --Initialise"
LogError "Waiting for keyring file to be created..."
local counter
counter="${counter:=0}"
while [ ! -f "${config_dir}/python_keyring/keyring_pass.cfg" ]; do
sleep 5
counter=$((counter + 1))
if [ "${counter}" -eq 360 ]; then
LogError "Keyring file has not appeared within 30 minutes. Restarting container..."
exit 1
fi
done
LogInfo "Keyring file exists, continuing"
fi
}
WaitForCookie(){
if [ "${1}" = "DisplayMessage" ]; then
LogError "Waiting for valid cookie file to be created..."
LogError " - Please create your cookie using the --Initialise script command line option"
LogError " - Syntax: docker exec -it <container name> sync-icloud.sh --Initialise"
LogError " - Example: docker exec -it icloudpd sync-icloud.sh --Initialise"
fi
local counter
counter="${counter:=0}"
while [ ! -f "${config_dir}/${cookie_file}" ]; do
sleep 5
counter=$((counter + 1))
if [ "${counter}" -eq 360 ]; then
LogError "Valid cookie file has not appeared within 30 minutes. Restarting container..."
exit 1
fi
done
}
WaitForAuthentication(){
local counter
counter="${counter:=0}"
while [ "$(grep -c "X-APPLE-WEBAUTH-HSA-TRUST" "${config_dir}/${cookie_file}")" -eq 0 ]; do
sleep 5
counter=$((counter + 1))
if [ "${counter}" -eq 360 ]; then
LogError "Valid cookie file has not appeared within 30 minutes. Restarting container..."
exit 1
fi
done
}
CheckWebCookie(){
if [ -f "${config_dir}/${cookie_file}" ]; then
LogDebug "Web cookie exists."
web_cookie_expire_date="$(grep "X_APPLE_WEB_KB" "${config_dir}/${cookie_file}" | sed -e 's#.*expires="\(.*\)Z"; HttpOnly.*#\1#')"
else
LogError "Web cookie does not exist"
WaitForCookie DisplayMessage
LogInfo "Cookie file exists, continuing"
fi
}
CheckMFACookie(){
if [ -f "${config_dir}/${cookie_file}" ]; then
LogDebug "Multi-factor authentication cookie exists."
else
LogError "Multi-factor authentication cookie does not exist"
WaitForCookie DisplayMessage
LogDebug "Multi-factor authentication cookie file exists, checking validity..."
fi
if [ "$(grep -c "X-APPLE-DS-WEB-SESSION-TOKEN" "${config_dir}/${cookie_file}")" -eq 1 -a "$(grep -c "X-APPLE-WEBAUTH-HSA-TRUST" "${config_dir}/${cookie_file}")" -eq 0 ]; then
LogDebug "Multi-factor authentication cookie exists, but not autenticated. Waiting for authentication to complete..."
WaitForAuthentication
LogDebug "Multi-factor authentication authentication complete, checking expiry date..."
fi
if [ "$(grep -c "X-APPLE-WEBAUTH-HSA-TRUST" "${config_dir}/${cookie_file}")" -eq 1 ]; then
mfa_expire_date="$(grep "X-APPLE-WEBAUTH-HSA-TRUST" "${config_dir}/${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_dir}/DAYS_REMAINING"
if [ "${days_remaining}" -gt 0 ]; then
valid_mfa_cookie=true
LogDebug "Valid two factor authentication cookie found. Days until expiration: ${days_remaining}"
else
rm -f "${config_dir}/${cookie_file}"
LogError "Cookie expired at: ${mfa_expire_date}"
LogError "Expired cookie file has been removed. Restarting container in 5 minutes"
sleep 300
exit 1
fi
else
rm -f "${config_dir}/${cookie_file}"
LogError "Cookie is not multi-factor authentication capable, authentication type may have changed"
LogError "Invalid cookie file has been removed. Restarting container in 5 minutes"
sleep 300
exit 1
fi
}
DisplayMFAExpiry(){
local error_message
LogInfo "Two factor authentication cookie expires: ${mfa_expire_date/ / @ }"
LogInfo "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 two factor 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 two factor authentication cookie expires for Apple ID: ${apple_id} - Please reinitialise"
else
error_message="${days_remaining} 天后 ${name} 的 Apple ID 两步验证将到期 - 请立即重新初始化"
fi
fi
LogWarning "${error_message}"
if [ "${synchronisation_time:=$(date +%s -d '+15 minutes')}" -gt "${next_notification_time:=$(date +%s)}" ]; then
if [ "${icloud_china}" = false ]; then
Notify "${cookie_status}" "Multi-Factor Authentication Cookie Expiration" "2" "${error_message}"
else
Notify "${cookie_status}" "Multi-Factor Authentication Cookie Expiration" "2" "${error_message}" "" "" "" "${days_remaining} 天后,${name} 的身份验证到期" "${error_message}"
fi
next_notification_time="$(date +%s -d "+24 hour")"
LogDebug "Next notification not before: $(date +%H:%M:%S -d "${next_notification_time} seconds")"