1
- from requests import JSONDecodeError
2
- from wallabag2readwise .models import ReadwiseBook , WallabagAnnotation
3
- from wallabag2readwise .readwise import ReadwiseConnector , new_highlights
4
- from wallabag2readwise .wallabag import WallabagConnector
5
1
from datetime import datetime
2
+ from time import sleep
3
+
4
+ from readwise import Readwise
5
+ from readwise .models import ReadwiseBook
6
+ from requests import JSONDecodeError
7
+
6
8
from wallabag2readwise .logging import logger
9
+ from wallabag2readwise .models import WallabagAnnotation , WallabagEntry
7
10
from wallabag2readwise .output import console
8
- from time import sleep
11
+ from wallabag2readwise . wallabag import WallabagConnector
9
12
10
13
11
14
def get_readwise_articles_with_retries (
12
- readwise : ReadwiseConnector , retries : int = 15 , timeout : int = 5
15
+ readwise : Readwise , retries : int = 15 , timeout : int = 5
13
16
) -> list [ReadwiseBook ]:
14
17
"""We try to circumvent readwise JSONDecodeError with retries."""
15
18
maximum = retries
@@ -31,7 +34,7 @@ def get_readwise_articles_with_retries(
31
34
sleep (timeout )
32
35
33
36
34
- def push_annotations (wallabag : WallabagConnector , readwise : ReadwiseConnector ):
37
+ def push_annotations (wallabag : WallabagConnector , readwise : Readwise ):
35
38
readwise_articles = get_readwise_articles_with_retries (readwise )
36
39
for wallabag_entry in wallabag .get_entries ():
37
40
if len (wallabag_entry .annotations ) > 0 :
@@ -52,7 +55,8 @@ def push_annotations(wallabag: WallabagConnector, readwise: ReadwiseConnector):
52
55
if readwise_article .title == wallabag_entry .title :
53
56
highlights = list (readwise .get_book_highlights (readwise_article .id ))
54
57
console .print (
55
- f'=> Found { len (highlights )} wallabag highlights for "{ wallabag_entry .title } "'
58
+ f'=> Found { len (highlights )} wallabag highlights '
59
+ f'for "{ wallabag_entry .title } "'
56
60
)
57
61
for annotation in annotations :
58
62
if annotation .quote not in [i .text for i in highlights ]:
@@ -63,19 +67,23 @@ def push_annotations(wallabag: WallabagConnector, readwise: ReadwiseConnector):
63
67
readwise_article_tags = list (
64
68
readwise .get_book_tags (readwise_article .id )
65
69
)
66
- for tag in wallabag_entry .tags :
67
- if tag .label not in [i .name for i in readwise_article_tags ]:
70
+ for wallabag_tag in wallabag_entry .tags :
71
+ if wallabag_tag .label not in [
72
+ i .name for i in readwise_article_tags
73
+ ]:
68
74
console .print (
69
- f'==> Adding tag "{ tag .label } " to Readwise article'
75
+ f'==> Adding tag "{ wallabag_tag .label } " to Readwise article'
70
76
)
71
- readwise .add_tag (readwise_article .id , tag .label )
77
+ readwise .add_tag (readwise_article .id , wallabag_tag .label )
72
78
73
- for tag in readwise_article_tags :
74
- if tag .name not in [i .label for i in wallabag_entry .tags ]:
79
+ for readwise_tag in readwise_article_tags :
80
+ if readwise_tag .name not in [
81
+ i .label for i in wallabag_entry .tags
82
+ ]:
75
83
console .print (
76
- f'==> Deleting tag "{ tag .name } " from Readwise article'
84
+ f'==> Deleting tag "{ readwise_tag .name } " from Readwise article'
77
85
)
78
- readwise .delete_tag (readwise_article .id , tag .id )
86
+ readwise .delete_tag (readwise_article .id , readwise_tag .id )
79
87
80
88
break
81
89
else :
@@ -86,9 +94,26 @@ def push_annotations(wallabag: WallabagConnector, readwise: ReadwiseConnector):
86
94
new_highlights (readwise , wallabag_entry , annotations )
87
95
for new_articles in get_readwise_articles_with_retries (readwise ):
88
96
if new_articles .title == wallabag_entry .title :
89
- for tag in wallabag_entry .tags :
97
+ for wallabag_tag in wallabag_entry .tags :
90
98
console .print (
91
- f'==> Adding tag "{ tag .label } " to Readwise article'
99
+ f'==> Adding tag "{ wallabag_tag .label } " to Readwise article'
92
100
)
93
- readwise .add_tag (new_articles .id , tag .label )
101
+ readwise .add_tag (new_articles .id , wallabag_tag .label )
94
102
break
103
+
104
+
105
+ def new_highlights (
106
+ readwise : Readwise ,
107
+ entry : WallabagEntry ,
108
+ annotations : list [WallabagAnnotation ],
109
+ ):
110
+ for item in annotations :
111
+ console .print ('==> Adding highlight to Readwise' )
112
+ readwise .create_highlight (
113
+ item .quote ,
114
+ entry .title ,
115
+ highlighted_at = item .created_at ,
116
+ source_url = entry .url ,
117
+ note = item .text ,
118
+ category = 'articles' ,
119
+ )
0 commit comments