Skip to content

Commit

Permalink
Add color filter support
Browse files Browse the repository at this point in the history
This allows a workflow where only certain annotations get imported.

Closes Only sync pre-specified color(s)? #25
  • Loading branch information
noeleont committed Oct 10, 2023
1 parent 142f8eb commit 5413f9b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
8 changes: 8 additions & 0 deletions zotero2readwise/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
default="n",
help="Include Zotero notes | Options: 'y'/'yes' (default), 'n'/'no'",
)
parser.add_argument(
"--filter_color",
choices=['#ffd400', '#ff6666', '#5fb236', '#2ea8e5', '#a28ae5', '#e56eee', '#f19837', '#aaaaaa'],
action="append",
default=[],
help="Filter Zotero annotations by given color | Options: '#ffd400' (yellow), '#ff6666' (red), '#5fb236' (green), '#2ea8e5' (blue), '#a28ae5' (purple), '#e56eee' (magenta), '#f19837' (orange), '#aaaaaa' (gray)"
)

args = vars(parser.parse_args())

Expand All @@ -52,5 +59,6 @@
zotero_library_type=args["library_type"],
include_annotations=args["include_annotations"],
include_notes=args["include_notes"],
filter_colors=args["filter_color"]
)
zt2rw.run()
6 changes: 4 additions & 2 deletions zotero2readwise/zotero.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,12 @@ def get_zotero_client(


class ZoteroAnnotationsNotes:
def __init__(self, zotero_client: Zotero):
def __init__(self, zotero_client: Zotero, filter_colors: List[str]):
self.zot = zotero_client
self.failed_items: List[Dict] = []
self._cache: Dict = {}
self._parent_mapping: Dict = {}
self.filter_colors: List[str] = filter_colors

def get_item_metadata(self, annot: Dict) -> Dict:
data = annot["data"]
Expand Down Expand Up @@ -201,7 +202,8 @@ def format_items(self, annots: List[Dict]) -> List[ZoteroItem]:
)
for annot in annots:
try:
formatted_annots.append(self.format_item(annot))
if len(self.filter_colors) == 0 or annot["data"]["annotationColor"] in self.filter_colors:
formatted_annots.append(self.format_item(annot))
except:
self.failed_items.append(annot)
continue
Expand Down
3 changes: 2 additions & 1 deletion zotero2readwise/zt2rw.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ def __init__(
zotero_library_type: str = "user",
include_annotations: bool = True,
include_notes: bool = False,
filter_colors: List[str] = []
):
self.readwise = Readwise(readwise_token)
self.zotero_client = get_zotero_client(
library_id=zotero_library_id,
library_type=zotero_library_type,
api_key=zotero_key,
)
self.zotero = ZoteroAnnotationsNotes(self.zotero_client)
self.zotero = ZoteroAnnotationsNotes(self.zotero_client, filter_colors)
self.include_annots = include_annotations
self.include_notes = include_notes

Expand Down

0 comments on commit 5413f9b

Please sign in to comment.