-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
1164 lines (858 loc) · 31.8 KB
/
utils.py
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
from collections import defaultdict
from datetime import datetime
import lovely_logger as logging # type: ignore
import streamlit as st
from unpywall import Unpywall # type: ignore
from unpywall.utils import UnpywallCredentials # type: ignore
def unpywall_credits(mail):
"""Setup credidentials for unpaywall
:param mail: valid email-adress
:type mail: str
:returns:
"""
try:
UnpywallCredentials.validate_email(mail)
except ValueError:
st.error(f"mail-adress {mail} is not valid.")
st.stop()
UnpywallCredentials(mail)
DATE_FMT = "%Y-%m-%dT%XZ"
def yt_icon():
return "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAB2klEQVRIS8WWSy8DURTHOxMJbQgJmz5Sn4AF0qXUK76DJbGz8YiVEGzVxraWfAMrbdQSkQhfQJg+oo0FqUqjU7/TdBgyk2lrGpPcnnvPPef/v+fcc++t4ql/gUDAp6rqkK7rPYauFQnGKxh3mUzmTfwV+fH7/bOKohzR7W8F1MKngG4OklNFVs7gwUVwg69ANGElFApFCOnCpZX/gAE3IgRROmftICCCiX8jKBJRio1/Rpaq1eoLsmKKUmWuV8bMddP6GEcZyn5+fXYRVDAeS6fTN82kjWIZwf6Kphp+dgQJymumGXDDFpIE/SkngjgEi78JcA6jf5Ss2JFjE2duwYkgBtCaBcEeqRunrWualrIi4cDGmF9xItiBYMuKAN1qXX9CaS/lcrl7sx0RbDPe/CuBRtXsZrPZQ4DMleVplMAyRcFgcAPgMpVxQIpKNinaJ0XLThFYbnIjVUUEEtW8E0GSPZhuBNBin5LoJp0IdFIRIcfXzZBQQaOk5xIfx4MmuO+0cxyekHJVlJFF5IdMou9A+JCdVFMXUt4RWbnXvKh/veyayY6trRFB2x8cL3mUJ3PAlWV/g+SJYLD26FO/cnseu0iSB0se/USNQD6eTqmAYTf+toBza5z2T0qH/Q2OKb2sAAAAAElFTkSuQmCC"
def manual():
return st.markdown(
"""
**Tag library items:**
- Items with duplicate pdf-files: `duplicate_pdf`
- Items *without* pdf-files: `nopdf`
- Items with *libraryCatalog* \"Zotero\": `todo_catalog`
- Duplicate items: `duplicate_item`
- Open-access journals: `open-access`
:red_circle: **NOTE**: These options need to calculate some lists
in case these are still empty, e.g.,
the list of items with duplicate pdf attachments.
---
**Merge duplicates:**
We have duplicate `Items`, sorted with respect to the added date
(oldest first):
|Item|Number|Attachments |
:---: | :---: | :---: |
| $I_1$ | 1 | $PDF_1$ |
| $I_2$ | 3 | $NOTE_2$, $PDF_2$, $OTHER_2$ |
| $I_3$ | 2 | $NOTE_3$, $PDF_3$ |
:red_circle: **NOTE:**
Duplicate items are identified based on their DOI and/or ISBN.
Description:
- Sort the Items with respect to added time (oldest first)
- Keep the oldest `Item` (first added), i.e., $I_1$
- Move all attachments of the newest `Item` to $I_1$
- Delete other items, including their attachments ($I_2$ and $I_3$)
The result of the actions described above is:
- $I_1$ having 3 attachments
- $PDF_1$, $NOTE_3$, $PDF_3$
---
**Remove duplicate pdf files**
Some items have duplicate pdf files, e.g. `[file1.pdf, file1.pdf]`.
In this case (all attachments are pdf files with the same name)
only one attachment will be kept.
:red_circle: **NOTE:**
Nothing will be deleted if the pdf files have different names.
This might be the case of items, for example,
supplementary materials nebst the actual pdf file.
"""
)
def howto():
return st.markdown(
"""
**Config file**
The following parameters should be defined, before using this app:
- the `library_id`: This can be found by opening the
[group](https://www.zotero.org/groups)’s page
and hovering over the group settings link.
- the `api_key`: Can be found from
[here](https://www.zotero.org/settings/keys/new).
- `library_type`:
- own Zotero library --> user (*no tested*)
- shared library --> group (*recommended*)
You can define all this necessary information in a config file.
:point_right:
[Here](https://github.com/chraibi/maintain-zotero/blob/main/config_template.cfg)
is an example to use.
---
:warning: **Before changing the library**
If you intend to change the Zotero library with
this app, then
- **Backup first.**
See How to
[locate, back up, and restore](https://www.zotero.org/support/zotero_data)
your `Zotero` library data.
- Disable sync in your desktop client before using it.
You can sync the group manually by right-clicking
on it in your Zotero client.
"""
)
def about():
msg = st.markdown(
""" This app offers several functionalities
to ease the maintenance of Zotero libraries.
Some functionalities are **read-only** from an online Zotero library
and produce reports.
Others, however, **change** the online Zotero library.
These can not be executed if the library is not in sync.
"""
)
return msg
def date_added(_item):
return datetime.strptime(_item["data"]["dateAdded"], DATE_FMT)
def get_item(key, _items):
"""Get item by key
:param key: key of item
:type key: str
:param _items: Zotero items
:type _Items: list of dicts
:returns: dict
"""
for item in _items:
if item["key"] == key:
return item
def attachment_is_pdf(_child):
"""
True if item is pdf
Criteria according to this url
# https://www.zotero.org/support/dev/web_api/v3/file_upload
:param _child:
:type _child:
:return: True is pdf
"""
return (
_child["data"]["itemType"] == "attachment"
and _child["data"]["contentType"] == "application/pdf"
and _child["data"]["linkMode"]
in ["imported_file", "linked_file", "imported_url"]
)
def get_suspecious_items(_items):
"""Items with libraryCatalog==Zotero
These items are suspecious, cause they were imported from
pdf files and maybe Zotero did not import the metadata properly.
:param _items: Zotero library items
:type _items: list containing dicts
:returns: list containing dicts
"""
list_catalog_zotero = []
for item in _items:
if "libraryCatalog" in item["data"]:
catalog = item["data"]["libraryCatalog"]
if catalog == "Zotero":
list_catalog_zotero.append(item)
return list_catalog_zotero
def get_items_with_duplicate_pdf(_zot, _items):
"""Items having several identical pdf files and their pdf files
:param _zot: A Zotero instance
:type _zot: pyzotero.zotero.Zotero
:param _items: Zotero library items
:type _items: list containing dicts
:returns: (list containing dicts, dict containing lists)
"""
_items_duplicate_attach = []
_pdf_attachments = defaultdict(list)
for _item in _items:
if is_standalone(_item) or is_file(_item):
continue
key = _item["key"]
cs = st.session_state.children[key]
for c in cs:
if attachment_is_pdf(c):
if "filename" in c["data"]:
_pdf_attachments[key].append(c["data"]["filename"])
else:
item_type = _item["data"]["itemType"]
cdata = c["data"]
logging.info("----- DEBUGISSUE -----")
logging.info(f"c: {c}")
logging.warning(f"cdata {cdata}")
logging.warning(f"type: {item_type}")
if len(_pdf_attachments[key]) > 1:
_items_duplicate_attach.append(_item)
return _items_duplicate_attach, _pdf_attachments
def get_items_with_no_pdf_attachments2(_items):
"""Items with no pdf file
:type _items: list containing dicts
:returns: list containing dicts
"""
_items_without_attach = []
for item in _items:
if is_standalone(item) or is_file(item):
continue
if "attachment" in item["links"]:
attach_type = item["links"]["attachment"]["attachmentType"]
if attach_type != "application/pdf":
_items_without_attach.append(item)
else:
_items_without_attach.append(item)
return _items_without_attach
# def get_items_with_no_pdf_attachments(_zot, _items):
# """Single Items with no attachments
# :param _zot: A Zotero instance
# :type _zot: pyzotero.zotero.Zotero
# :param _items: Zotero library items
# :type _items: list containing dicts
# :returns: list containing dicts
# """
# _items_without_attach = []
# for _item in _items:
# has_attach = False
# if is_standalone(_item):
# continue
# key = _item["key"]
# cs = st.session_state.children[key]
# for c in cs:
# if attachment_is_pdf(c):
# has_attach = True
# break
# if not has_attach:
# _items_without_attach.append(_item)
# return _items_without_attach
def get_standalone_items(_items):
"""Standalone items with no metadata (notes, pdfs, etc)
:param _items: Zotero library items
:type _items: list containing dicts
:returns: list of dicts
"""
standalone = []
for _item in _items:
if is_standalone(_item):
standalone.append(_item)
return standalone
def is_file(_item):
"""Definition of a file item
:param _item: Zotero library item
:type _item: dict
:returns: True if file
"""
return _item["data"]["itemType"] in ["note", "attachment", "annotation"]
# zot.item_types()
def is_book(_item):
"""item is book
:param _item: Zotero library item
:type _item: dict
:returns: True of book or bookSection
"""
return _item["data"]["itemType"] in ["book", "bookSection"]
def is_misc(_item):
"""item is report, thesis or document
:param _item: Zotero library item
:type _item: dict
:returns: Bool
"""
return _item["data"]["itemType"] in ["thesis", "report", "document"]
def is_article(_item):
"""Item is an article
:param _item: Zotero library item
:type _item: dict
:returns: True if conf, encyArt or journalArt
"""
return _item["data"]["itemType"] in [
"conferencePaper",
"encyclopediaArticle",
"journalArticle",
]
# TODO is_Standalone is often used with is_file.
# probably replace is_standalone with is_file.
def is_standalone(_item):
"""Definition of a standalone item
:param _item: Zotero library item
:type _item: dict
:returns: True if standalone
"""
# Zotero 6 write annotations in pdfs as a standalone item with parent being
# the pdf file!
return is_file(_item) and "parentItem" not in _item["data"]
def retrieve_data(_zot, _num_items):
"""Retrieve <num_items> top-level Zotero library items.
:param _zot: A Zotero instance
:type _zot: pyzotero.zotero.Zotero
:param _num_items: Number if items to retrieve
:type _num_items: int
:returns: list of dicts
"""
msg = st.empty()
logging.info(f"retrieve_data. trying to get {_num_items} items")
limit = 100 # determined by the API
start = 0
lib_items = []
count = 1
step = int(limit * 100 / _num_items)
read_items = 0
my_bar = st.progress(0)
while read_items < _num_items:
progress_by = (count + 1) * step
count += 1
try:
lib_items.extend(_zot.items(limit=limit, start=start))
except Exception as e:
logging.error(f"Could not retrive data with error {str(e)}")
st.stop()
read_items += limit
start = start + limit
if _num_items - read_items < limit:
progress_by = 100
logging.info(f"read {len(lib_items)} / {_num_items} items")
msg.info(f"read {len(lib_items)} / {_num_items} items")
my_bar.progress(progress_by)
return lib_items
def trash_is_empty(_zot):
"""Is trash empty?
:todo: Maybe return len
:param _zot: A Zotero instance
:type _zot: pyzotero.zotero.Zotero
:returns: True if empty
"""
return len(_zot.trash()) == 0
def get_time(t):
"""Time in min sec
:param t: Run time
:type t: float
:returns: str
"""
minutes = t // 60
seconds = t % 60
return f"""{minutes:.0f} min:{seconds:.0f} sec"""
def duplicates_by_title(_items):
"""Duplicate items by Title
Some items do not have DOI not ISBN.
This functions compares items by title
:todo: Similar to duplicates_by_doi.
:todo: Return items not titles
:param _items: Zotero library items
:type _items: list containing dicts
:returns: list of str (titles)
"""
duplicate_items_by_title = defaultdict(list)
duplicates = []
for item in _items:
if is_standalone(item) or is_file(item):
continue
iType = item["data"]["itemType"]
Title = item["data"]["title"]
duplicate_items_by_title[iType].append(Title.capitalize())
for _type, _titles in duplicate_items_by_title.items():
num_duplicates_items = len(_titles) - len(set(_titles))
if num_duplicates_items:
duplicates = {x for x in _titles if _titles.count(x) > 1}
return duplicates
def duplicates_by_doi(_items):
"""Duplicate items by DOI/ISBN
Items are sorted by DOI/ISBN and then duplicates
are returned.
Similar to duplicates_by_title().
:param _items: Zotero library items
:type _items: list containing dicts
:returns: list of dicts
"""
by_doi = get_items_by_doi_or_isbn(_items)
result = []
for _, items in by_doi.items():
if len(items) > 1:
for i in items:
result.append(i)
return result
# @todo: separate doi from isbn
# doi and issn for papers
# isbn for books and book sections
def _get_books_without_isbn(_items):
# item['data']['itemType']
# = book, bookSection
# item['data']['title']
return
def get_items_by_isbn(_items):
return
def duplicates_by_isbn(_items):
return
def doi_to_item(dois):
"""return items to a list of dois
:param dois: DOI numbers
:type dois: list of str
:returns: items with DOIS
"""
items = []
for doi in dois:
for _item in st.session_state.zot_items:
if is_standalone(_item) or is_file(_item):
continue
if "DOI" in _item["data"]:
doi_item = _item["data"]["DOI"]
if doi_item.lower() == doi.lower():
items.append(_item)
continue
return items
def get_items_by_doi(_items):
"""Items having a DOI
:param _items: Zotero library items
:returns: dict of lists
"""
_items_by_doi = defaultdict(list)
for _item in _items:
if is_standalone(_item) or is_file(_item):
continue
if "DOI" in _item["data"]:
doi = _item["data"]["DOI"]
if doi:
_items_by_doi[doi].append(_item)
return _items_by_doi
def get_items_by_doi_or_isbn(_items):
"""Items having a DOI and/or ISBN
:param _items: Zotero library items
:returns: dict of lists
"""
_items_by_doi_isbn = defaultdict(list)
for _item in _items:
if is_standalone(_item) or is_file(_item):
continue
if "DOI" in _item["data"]:
doi = _item["data"]["DOI"]
if doi:
_items_by_doi_isbn[doi].append(_item)
elif "ISBN" in _item["data"]:
isbn = _item["data"]["ISBN"]
if isbn:
_items_by_doi_isbn[isbn].append(_item)
return _items_by_doi_isbn
def get_items_with_empty_doi_or_isbn(_items):
"""
Articles with no DOI. Books with no ISBN.
:param _items: Zotero library items
:type _items: list containing dicts
:return: list of dicts
"""
empty_items = []
for _item in _items:
if is_standalone(_item) or is_file(_item):
continue
if is_article(_item):
_field = "DOI"
elif is_book(_item):
_field = "ISBN"
elif is_misc(_item):
logging.warning(f"Misc {_item['data']['itemType']}")
continue
else:
logging.warning(f"Type of item not known {_item['data']['itemType']}")
st.warning(f"Type of item not known {_item['data']['itemType']}")
continue
if _field in _item["data"]:
if not _item["data"][_field]:
empty_items.append(_item)
return empty_items
def get_items_with_empty_doi_and_isbn(_items, _fields):
"""
Items with no DOI and no ISBN.
:param _items: Zotero library items
:type _items: list containing dicts
:param _fields: DOI and ISBN
:type _fields: list of str
:return: list of dict
"""
empty = []
for _item in _items:
result = []
if is_standalone(_item) or is_file(_item):
continue
for field in _fields:
if field in _item["data"]:
f = _item["data"][field]
if not f:
result.append(False)
else:
result.append(True)
if not any(result):
empty.append(_item)
return empty
def delete_pdf_attachments(_children, pl2):
"""Delete pdf attachments of an item and keep only one.
This functions changes the online Zotero library!
:param _children: Children items of a specific item
:type _children: list of dicts
:param pl2: placeholder to print messages
:type pl2: st.empty()
:returns: True if an attachment has been deleted.
"""
deleted_attachment = False
zot = st.session_state.zot
for child in _children[1:]:
if not attachment_is_pdf(child):
continue # only for pdf files.
pl2.warning(f"deleting {child['data']['filename']}")
logging.warning(f"deleting {child['data']['filename']}")
zot.delete_item(child)
deleted_attachment = True
return deleted_attachment
def log_title(_item):
"""Log title of an item
:param _item: Zotero library item
:type _item: dict
:returns: st.code
"""
if is_standalone(_item):
ttt = f"Standalone item of type: <{_item['data']['itemType']}>"
if "filename" in _item["data"]:
ttt += f" ({_item['data']['filename']})"
else:
ttt = f"{_item['data']['title']}"
st.code(f"{ttt}")
logging.info(f"{ttt}")
def set_new_tag(z, n, m, d, o, mail=""):
"""Prepare list of tags to be added to items
We have to add the tags to items at once.
Otherwise, we will have to update the library!
This function may update the session_state of some lists
(if session_state lists are empty)
These lists are:
- suspecious_items
- multpdf_items
- nopdf_items
- doi_dupl_items
:param z: Suspecious items
:type z: Bool
:param n: Items with no pdf
:type n: Bool
:param m: Items with multiple pdf
:type m: Bool
:param d: Duplicate items
:type d: Bool
:param o: open-access articles
:type o: Bool
:returns: dict of lists
"""
new_tags = defaultdict(list)
if z:
update_suspecious_state()
zotero_items = st.session_state.suspecious_items
for item in zotero_items:
new_tags[item["data"]["key"]].append("todo_catalog")
if m:
update_duplicate_attach_state()
items_duplicate_attach = st.session_state.multpdf_items
for item in items_duplicate_attach:
new_tags[item["data"]["key"]].append("duplicate_pdf")
if n:
update_without_pdf_state()
items_without_pdf = st.session_state.nopdf_items
for item in items_without_pdf:
new_tags[item["data"]["key"]].append("nopdf")
if d:
update_duplicate_items_state()
duplicate_items = st.session_state.doi_dupl_items
for item in duplicate_items:
new_tags[item["data"]["key"]].append("duplicate_item")
if o:
unpywall_credits(mail)
items_by_doi = get_items_by_doi(st.session_state.zot_items)
pl = st.empty()
with st.spinner("Initializing ..."):
OA_items, _ = get_oa_ca(items_by_doi, pl)
items = doi_to_item(OA_items)
for item in items:
new_tags[item["data"]["key"]].append("open-access")
return new_tags
def add_tag(tags_to_add, _zot, _item):
"""Add tags to Zotero items
This function changes the online library
:todo: use zot from session_state
:param tags_to_add: dict prepared in set_new_tag()
:type tags_to_add: dict of lists
:param _zot: A Zotero instance
:type _zot: pyzotero.zotero.Zotero
:param _item: Zotero library item
:type _item: dict
:returns: True if changes have been made
"""
logging.info(
f"Enter add_tag with {tags_to_add}. standalone: {is_standalone(_item)}. is_file: {is_file(_item)}"
)
if not tags_to_add or is_standalone(_item) and is_file(_item):
return False
title = _item["data"]["title"]
item_tags = [t["tag"] for t in _item["data"]["tags"]]
new_tags = [t for t in tags_to_add if t not in item_tags]
logging.info(f"new tags: {new_tags}")
if not new_tags:
return False
logging.info(f"add tags {new_tags} to {title}")
_zot.add_tags(_item, *new_tags)
return True
def update_suspecious_state():
"""update suspecious_items"""
if not st.session_state.suspecious_items:
items = get_suspecious_items(st.session_state.zot_items)
st.session_state.suspecious_items = items
# @todo check if state variable need to be used as input for functions
def update_duplicate_attach_state():
"""First update of lists related to multiple pdfs
- multpdf_items
- pdfs
- init_multpdf_items
"""
if not st.session_state.init_multpdf_items:
items, pdfs = get_items_with_duplicate_pdf(
st.session_state.zot, st.session_state.zot_items
)
st.session_state.multpdf_items = items
st.session_state.pdfs = pdfs
st.session_state.init_multpdf_items = True
def force_update_duplicate_attach_state():
"""Update of lists related to multiple pdfsrelated to multiple pdfs
- multpdf_items
- pdfs
- init_multpdf_items
"""
items, pdfs = get_items_with_duplicate_pdf(
st.session_state.zot, st.session_state.zot_items
)
st.session_state.multpdf_items = items
st.session_state.pdfs = pdfs
st.session_state.init_multpdf_items = True
def update_duplicate_items_state():
"""First update of duplicate items by doi"""
if not st.session_state.init_doi_dupl_items:
duplicates = duplicates_by_doi(st.session_state.zot_items)
st.session_state.doi_dupl_items = duplicates
st.session_state.init_doi_dupl_items = True
def force_update_duplicate_items_state():
"""First update of duplicate items by doi"""
duplicates = duplicates_by_doi(st.session_state.zot_items)
st.session_state.doi_dupl_items = duplicates
st.session_state.init_doi_dupl_items = True
def update_without_pdf_state():
"""First update of items without pdf"""
if not st.session_state.nopdf_items:
items = get_items_with_no_pdf_attachments2(st.session_state.zot_items)
st.session_state.nopdf_items = items
def uptodate():
"""Check if library is up to date
every change in Zotero-Library increments
the version number by one.
For example, trash being emptied --> +1
or note's content changed --> +1
:return: True if up-to-date
"""
actual_st_version = st.session_state.zot_version
last_modified_version = st.session_state.zot.last_modified_version()
return actual_st_version == last_modified_version
def items_uptodate():
"""Check if items are outdated
If outdated, don't execute any update-functions
:todo: not used
:return: True if all up-to-date
"""
zot = st.session_state.zot
vs = zot.item_versions()
vc = {}
for item in st.session_state.zot_items:
vc[item["key"]] = item["data"]["version"]
vs_reduced = {k: vs[k] for k, _ in vc.items()}
logging.info("----")
logging.info(f"vc: {len(vc)}, {vc}")
logging.info(f"vs: {len(vs)}, {vs}")
logging.info(f"vs_reduced: {len(vs_reduced)}, {vs_reduced}")
logging.info(vc == vs_reduced)
return vc == vs_reduced
def update_tags(
pl2, update_tags_z, update_tags_n, update_tags_m, update_tags_d, update_tags_o, mail
):
"""
A wrapper function of add_tag()
:param pl2: placeholder to print messages
:type pl2: st.empty()
:param update_tags_z: Suspecious items
:type update_tags_z: Bool
:param update_tags_n: Items with no pdf
:type update_tags_n: Bool
:param update_tags_m: Items with multiple pdf
:type update_tags_m: Bool
:param update_tags_d: Duplicte items
:type update_tags_d: Bool
:param update_tags_o: Open-access articles
:type update_tags_: Bool
:param mail: mail necessary to fetch open-access articles
"""
new_tags = set_new_tag(
update_tags_z, update_tags_n, update_tags_m, update_tags_d, update_tags_o, mail
)
changed = []
msg = st.empty()
if not new_tags:
pl2.info(":heavy_check_mark: Tags of the library are not changed.")
else:
pl2.warning(":red_circle: Updating tags ...")
my_bar = st.progress(0)
retrieved_items = len(st.session_state.zot_items)
for i, item in enumerate(st.session_state.zot_items):
msg.info(f"process {i} / {retrieved_items}")
logging.info(f"process {i} / {retrieved_items}")
progress_by = (i + 1) / retrieved_items
my_bar.progress(progress_by)
ch = add_tag(
new_tags[item["data"]["key"]],
st.session_state.zot,
item,
)
changed.append(ch)
if any(changed):
pl2.warning(
""":warning: Library updated.
You may want to sync!"""
)
else:
pl2.info(":heavy_check_mark: Tags of the library are not changed.")
def init_update_delete_lists():
"""Initialize Items to update/delete
Prepare some lists of items to be updated and deleted
Note:
- Duplicates without DOI not ISBN numbers are going to be ignored!
- Duplicates with different DOI or ISBN will be missed as well!
(e.g. ISBN=0968-090X and ISBN=0968090X)
:returns: (update list, delete list)
"""
DELETE_OWN_ATTACHMENTS = False # @todo add option to ui
lib_items = st.session_state.zot_items
by_doi = get_items_by_doi_or_isbn(lib_items)
delete_items = []
update_items = []
for _, items in by_doi.items():
if len(items) == 1:
continue
# sort by age. oldest first
items.sort(key=date_added)
# keep oldest item
keep = items[0]
# keep latest attachments
keep_cs = st.session_state.children[keep["key"]]
duplicates_have_pdf = False
for item in items[-1:0:-1]: