Skip to content

Commit 5277ef2

Browse files
authored
Merge pull request #47 from rwxd/pyreadwise
fix: changed to pyreadwise
2 parents b4e3c7a + c1a234d commit 5277ef2

File tree

8 files changed

+134
-284
lines changed

8 files changed

+134
-284
lines changed

.pre-commit-config.yaml

+57-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,63 @@
1+
---
12
repos:
23
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v2.3.0
4+
rev: "v4.4.0"
45
hooks:
56
- id: check-yaml
7+
- id: check-json
68
- id: end-of-file-fixer
79
- id: trailing-whitespace
10+
- id: check-added-large-files
11+
- id: check-symlinks
12+
- id: no-commit-to-branch
13+
- id: trailing-whitespace
14+
- id: debug-statements
15+
- id: double-quote-string-fixer
16+
- id: requirements-txt-fixer
17+
18+
- repo: https://github.com/psf/black
19+
rev: "23.1.0"
20+
hooks:
21+
- id: black
22+
args:
23+
- "--skip-string-normalization"
24+
25+
- repo: https://github.com/asottile/pyupgrade
26+
rev: "v3.3.1"
27+
hooks:
28+
- id: pyupgrade
29+
30+
- repo: https://github.com/pre-commit/mirrors-mypy
31+
rev: "v1.1.1"
32+
hooks:
33+
- id: mypy
34+
args:
35+
- "--ignore-missing-imports"
36+
additional_dependencies:
37+
- types-requests
38+
- types-pyyaml
39+
40+
- repo: https://github.com/PyCQA/flake8
41+
rev: 6.0.0
42+
hooks:
43+
- id: flake8
44+
args:
45+
- "--max-line-length=100"
46+
47+
- repo: https://github.com/pycqa/isort
48+
rev: 5.12.0
49+
hooks:
50+
- id: isort
51+
name: isort (python)
52+
args:
53+
- "--profile"
54+
- "black"
55+
- "--filter-files"
56+
57+
- repo: https://github.com/PyCQA/autoflake
58+
rev: v2.0.2
59+
hooks:
60+
- id: autoflake
61+
args:
62+
- "--remove-all-unused-imports"
63+
- "--ignore-init-module-imports"

requirements.txt

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
typer==0.7.0
2-
rich==12.6.0
1+
readwise==1.0.13
32
requests==2.28.1
4-
ratelimit==2.2.1
5-
backoff==2.2.1
3+
rich==12.6.0
4+
typer==0.7.0

setup.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from setuptools import setup
21
from os import path
32
from subprocess import check_output
43

5-
with open("./requirements.txt") as f:
4+
from setuptools import setup
5+
6+
with open('./requirements.txt') as f:
67
required = f.read().splitlines()
78

89
this_directory = path.abspath(path.dirname(__file__))
@@ -15,7 +16,7 @@
1516
version = (
1617
check_output(['git', 'describe', '--tags']).strip().decode().replace('v', '')
1718
)
18-
except:
19+
except Exception:
1920
pass
2021

2122
setup(
@@ -26,12 +27,12 @@
2627
long_description_content_type='text/markdown',
2728
author='rwxd',
2829
author_email='[email protected]',
29-
url="https://github.com/rwxd/wallabag2readwise",
30+
url='https://github.com/rwxd/wallabag2readwise',
3031
license='MIT',
3132
packages=['wallabag2readwise'],
3233
install_requires=required,
3334
entry_points={
34-
"console_scripts": ["wallabag2readwise = wallabag2readwise.__main__:main"]
35+
'console_scripts': ['wallabag2readwise = wallabag2readwise.__main__:main']
3536
},
3637
classifiers=[
3738
'Programming Language :: Python :: 3.6',

wallabag2readwise/cli.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
import importlib.metadata
2+
from time import sleep
3+
14
import typer
2-
from wallabag2readwise.misc import push_annotations
3-
from wallabag2readwise.readwise import ReadwiseConnector, ReadwiseReaderConnector
5+
from readwise import Readwise, ReadwiseReader
46

5-
from wallabag2readwise.wallabag import WallabagConnector
6-
from time import sleep
7+
from wallabag2readwise.misc import push_annotations
78
from wallabag2readwise.output import console
8-
import importlib.metadata
9-
9+
from wallabag2readwise.wallabag import WallabagConnector
1010

1111
app = typer.Typer()
1212

@@ -29,7 +29,7 @@ def push(
2929
wallabag_client_id,
3030
wallabag_client_secret,
3131
)
32-
readwise = ReadwiseConnector(readwise_token)
32+
readwise = Readwise(readwise_token)
3333
push_annotations(wallabag, readwise)
3434

3535

@@ -47,7 +47,6 @@ def daemon(
4747
60, help='time to wait between runs in minutes', envvar='WAIT_TIME'
4848
),
4949
) -> None:
50-
5150
console.print(f'> Starting daemon with {wait_time} minutes wait time')
5251
while True:
5352
wallabag = WallabagConnector(
@@ -57,7 +56,7 @@ def daemon(
5756
wallabag_client_id,
5857
wallabag_client_secret,
5958
)
60-
readwise = ReadwiseConnector(readwise_token)
59+
readwise = Readwise(readwise_token)
6160
push_annotations(wallabag, readwise)
6261

6362
console.print(f'=> Waiting {wait_time} minutes')
@@ -75,21 +74,21 @@ def reader(
7574
wallabag_client_secret: str = typer.Option(..., envvar='WALLABAG_CLIENT_SECRET'),
7675
readwise_token: str = typer.Option(..., envvar='READWISE_TOKEN', prompt=True),
7776
):
78-
console.print(f'> Starting readwise reader import')
77+
console.print('> Starting readwise reader import')
7978
wallabag = WallabagConnector(
8079
wallabag_url,
8180
wallabag_user,
8281
wallabag_password,
8382
wallabag_client_id,
8483
wallabag_client_secret,
8584
)
86-
readwise = ReadwiseReaderConnector(readwise_token)
85+
readwise = ReadwiseReader(readwise_token)
8786
wallabag_entries = wallabag.get_entries()
8887
for entry in wallabag_entries:
8988
console.print(f'=> Importing {entry.title}')
9089
location = 'archive' if entry.archived else 'new'
9190
try:
92-
readwise.create(
91+
readwise.create_document(
9392
entry.url, tags=[t.label for t in entry.tags], location=location
9493
)
9594
except Exception as e:

wallabag2readwise/misc.py

+44-19
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
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
51
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+
68
from wallabag2readwise.logging import logger
9+
from wallabag2readwise.models import WallabagAnnotation, WallabagEntry
710
from wallabag2readwise.output import console
8-
from time import sleep
11+
from wallabag2readwise.wallabag import WallabagConnector
912

1013

1114
def get_readwise_articles_with_retries(
12-
readwise: ReadwiseConnector, retries: int = 15, timeout: int = 5
15+
readwise: Readwise, retries: int = 15, timeout: int = 5
1316
) -> list[ReadwiseBook]:
1417
"""We try to circumvent readwise JSONDecodeError with retries."""
1518
maximum = retries
@@ -31,7 +34,7 @@ def get_readwise_articles_with_retries(
3134
sleep(timeout)
3235

3336

34-
def push_annotations(wallabag: WallabagConnector, readwise: ReadwiseConnector):
37+
def push_annotations(wallabag: WallabagConnector, readwise: Readwise):
3538
readwise_articles = get_readwise_articles_with_retries(readwise)
3639
for wallabag_entry in wallabag.get_entries():
3740
if len(wallabag_entry.annotations) > 0:
@@ -52,7 +55,8 @@ def push_annotations(wallabag: WallabagConnector, readwise: ReadwiseConnector):
5255
if readwise_article.title == wallabag_entry.title:
5356
highlights = list(readwise.get_book_highlights(readwise_article.id))
5457
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}"'
5660
)
5761
for annotation in annotations:
5862
if annotation.quote not in [i.text for i in highlights]:
@@ -63,19 +67,23 @@ def push_annotations(wallabag: WallabagConnector, readwise: ReadwiseConnector):
6367
readwise_article_tags = list(
6468
readwise.get_book_tags(readwise_article.id)
6569
)
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+
]:
6874
console.print(
69-
f'==> Adding tag "{tag.label}" to Readwise article'
75+
f'==> Adding tag "{wallabag_tag.label}" to Readwise article'
7076
)
71-
readwise.add_tag(readwise_article.id, tag.label)
77+
readwise.add_tag(readwise_article.id, wallabag_tag.label)
7278

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+
]:
7583
console.print(
76-
f'==> Deleting tag "{tag.name}" from Readwise article'
84+
f'==> Deleting tag "{readwise_tag.name}" from Readwise article'
7785
)
78-
readwise.delete_tag(readwise_article.id, tag.id)
86+
readwise.delete_tag(readwise_article.id, readwise_tag.id)
7987

8088
break
8189
else:
@@ -86,9 +94,26 @@ def push_annotations(wallabag: WallabagConnector, readwise: ReadwiseConnector):
8694
new_highlights(readwise, wallabag_entry, annotations)
8795
for new_articles in get_readwise_articles_with_retries(readwise):
8896
if new_articles.title == wallabag_entry.title:
89-
for tag in wallabag_entry.tags:
97+
for wallabag_tag in wallabag_entry.tags:
9098
console.print(
91-
f'==> Adding tag "{tag.label}" to Readwise article'
99+
f'==> Adding tag "{wallabag_tag.label}" to Readwise article'
92100
)
93-
readwise.add_tag(new_articles.id, tag.label)
101+
readwise.add_tag(new_articles.id, wallabag_tag.label)
94102
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

Comments
 (0)